-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments on types are stripped #2964
Comments
Seems like the comment is on type alias and the type node is removed. |
@kwonoj Any thoughts? Do you think we should preserve the comment on type nodes? |
TS has a compiler flag, removeComments, to control the behavior |
I think removal of comment is expected, because the comment is on type definition. |
@cdaringe That doesn't apply for this case, what @kdy1 says is correct. Even if you set Locally verified it as well. My take is still same, I'd consider this as improvement if we want to support. |
It's worth mentioning that currently the behavior is not 1:1, take a look at @kwonoj's example but with a newline: https://www.typescriptlang.org/play?&removeComments=false#code/PQKgBAlgzgLghgOwEYFcA2kDmCD2AnAUzAQIA8YwRgAoamATwAciAtMAXmJQFskC8A3NTQEKpDmACMAoA This emits the comment as expected, however https://play.swc.rs/?version=1.2.115&code=H4sIAAAAAAAAA9PXUsgsLknMSyrNUchMz8svSlXIS60oUdDS5%2BIqqSxIVYhSsFXIK81NSi2y5spJLVGoAPINrQFh8abFNwAAAA%3D%3D&config=H4sIAAAAAAAAA6tWys3My0yrVLJKS8wpTtVRyipOVrKqVipILCpOLQKxiivzShIrlKyUSioLUouTizILSpRqa2sBrzFAMDkAAAA%3D does not. |
Yes, that's known - while issue is different, this comments explains it: microsoft/TypeScript#26079 (comment)
While SWC doesn't strictly distinguish comment attachment in this case. But also, if desire is let comment directive to work for given statement for this specific case, since actual line's already strippted not sure what those comment supposed to do here. |
Just to provide a visual to @kwonoj 's comment. Please see this TS Playground. let y = 3;
/* istanbul ignore next */
type Z = number;
let x = 1; is compiled into: "use strict";
let y = 3;
let x = 1; The comment above the type statement is stripped. |
Has anyone found a workaround for this? Is there a setting on .swcrc config or .tsconfig that can prevent We're trying to migrate from ts-jest to @swc/jest and we have 100% coverage requirement... 🙏 Any help much appreciated. |
@dpchamps wrote a jest plugin that tests for the comment, calls into this swc/jest, then reinjects the comment before handing off jest. it's imperfect, but gets us most of the way there |
workaround// wrap-swc-transformer.js
const { createTransformer: createSwcTransformer } = require("@swc/jest");
const FILE_LEVEL_IGNORE = "\n/* istanbul ignore file */";
const istanbulFileLevelMatcher = /(istanbul ignore) (?!.*(next|branch|if|else))/gm;
/**
* search the contents of the file for a file-level istanbul-ignore
* e.g. `istanbul ignore file`
* if found, append the ignore text to the bottom of the file
**/
const wrapTransformer = (swcOptions) => {
const swcTransformer = createSwcTransformer(swcOptions);
return {
...swcTransformer,
process: (src, filename, jestOptions) => {
const result = swcTransformer.process(src, filename, jestOptions);
if( src.match(istanbulFileLevelMatcher) ){
result.code += FILE_LEVEL_IGNORE;
console.log(result.code)
}
return result;
},
};
};
module.exports = {
createTransformer: wrapTransformer
}
//jest config
transform: {
"^.+\\.[tj]sx?$": [
"./wrap-swc-transformer.js",
{
sourceMaps: true,
jsc: {
parser: {
syntax: "typescript",
},
transform: {},
target: "es5",
},
},
],
} This is heuristic based on the observation that this issue only impacts comments attached to type nodes (and import nodes depending on what you're compiling for). Assuming that @kdy1 I think that assumption is robust enough that you might consider adding something like this into the transformer. Especially if the proper fix will take time to realize. Side note: The creator of |
Special handling of |
Thanks for the quick response. I think as long as comments can be preserved then there's a workaround for this type of situation. |
@kdy1 yep thats fair. It's a total hack, but this will help you @joshyudeep in the interim |
Thanks for the suggestion.. Looks like your suggested wrapper only deals with Much appreciate the quick response 👍 |
@kdy1 I'm assuming there's currently not a config option as a workaround for this (disabling removing of comments). Do you have a rough estimate as to when this functionality would be worked on / released? Trying to see whether it'd be worth the time to come up with my own custom solution in the interim. Thanks! Also, much appreciate all that you (swc contributors) have put into this project, our test suites are running 5x faster with @swc/jest. |
I'd suggest to go with workaround. We'll eventually have this but there's no promised timeline or eta to provide. |
@joshyudeep, sorry my explanation probably wasn't clear 😅 : Comments are only dropped when they precede types and import statements. Types and import statements don't impact coverage, therefore That leaves you with one remaining case: file-level istanbul directives. That's what this workaround solves. |
@dpchamps thanks for the follow up -- I don't think I completely follow cause I don't think the above assumption is true. Check out this playground where all comments are stripped out except the last comment that's at the EOF. It looks like you're solution that you suggested basically just checks for Is there something I'm missing? |
@joshyudeep Yes that would be prettyheavy handed 😂. But:
note the negative look ahead :) So, thats to say "if one file global directive is found, then the whole file will ignored regardless of individual comments". That's inline with expectations. also, compiler settings matter lmk if that still isn't clear happy to help |
Comments are not removed. Those are simply not printed because the node which the comments are for is not printed as it's removed. |
Also, I don't have any estimate and I don't think I will work on this. |
@kdy1 , yes -- it's clear to me why comments are sometimes not emitted in the final output. Which I'll summarize here. Please correct me if I'm wrong:
I actually have some interest in working on a holistic solution for this. Would you be open to a patch? I have some PTO coming up, and am already pretty familiar with the SWC codebase. |
I'm open to patch, but please add a flag to enable moving of comments. I think preserving comments of dropped nodes is counter-intuitive |
would it be possible to just capture comments as a list of |
@longlho No |
is that a technical limitation or resourcing limitation? |
What's resourcing limitation? |
It's technical, |
To be clear I'm not specifically asking for emitting/preserving comment nodes, but rather parsing them into AST so plugins can use them for static analysis/directives. |
It's not possible and I'll reject PRs doing so |
Can you elaborate? I'm reading through the thread and wasn't able to see the technical limitations other than lack of spec |
Adding a field for comment to all nodes is simply not acceptable. |
hmm no that's not what I meant. I meant like https://astexplorer.net/#/gist/5523e56fe73439101ec0d38e43734dd7/9d2d9e5dea8587085ada67d1a731785783eb7cd4 where |
It will make writing transforms much harder, so it's no anyway. |
Can you explain that point? Assuming comment transformation/emit is still unsupported what's the complexity in just parsing? |
I mean lifetime will be headachr if comments are stored in Module type. |
Can you clarify that? That's how babel does it so there is 1 reference implementation. |
Would be amazing to have comments in Here is example of {
"type": "File",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"errors": [],
"program": {
"type": "Program",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start": 9,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 18,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 14
},
"identifierName": "world"
},
"name": "world"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 3,
"column": 1
}
},
"body": [],
"directives": []
},
"leadingComments": [
{
"type": "CommentLine",
"value": " hello",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " hello",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
} And {
"type": "Module",
"span": {
"start": 0,
"end": 25,
"ctxt": 0
},
"body": [
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 9,
"end": 14,
"ctxt": 0
},
"value": "world",
"optional": false
},
"declare": false,
"params": [],
"decorators": [],
"span": {
"start": 0,
"end": 25,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 17,
"end": 25,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
}
],
"interpreter": null
} Almost everything can be converted, but where should I get |
swc to babel already exists. |
Currently there's no js api to get comments |
Nice :)! How can I use it from |
OK, but is it possible to use it (event if without comments) from const {parseSync} = require('@swc/core');
const ast = parseSync(`const hello = 'world'`);
const babelAst = swcToBabel(ast); Or even: const {parseSync, swcToBabel} = require('@swc/core');
const ast = parseSync(`const hello = 'world'`, {
babel: true,
});
Why does it called The thing is they are quite different and that's why |
It can emit acorn ast/babel ast |
That’s awesome :)!
|
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
Comments are stripped due to unknown reasons, against expectation.
Input code
Config
Playground link
https://play.swc.rs/?version=1.2.117&code=H4sIAAAAAAAAAzXMOwrAIBBF0d5VvFoCklqykXQahjigY4hjPruPTdpz4ToLbhok9gzepZ4EoUdhnXEO%2Bh6EFQukl0inx7AuWy2FRKGJGzILTbiDbmkA4Y8h1otwBeGWzDMWszcfPlBa620AAAA%3D&config=H4sIAAAAAAAAA6tWys3My0yrVLJKS8wpTtVRyipOVrKqVipILCpOLQKxiivzShIrlKyUSioLUouTizILSpRqa2sBrzFAMDkAAAA%3D
Expected behavior
bad.ts
should emit:but does emit:
dropping the istanbul comment
Version
1.2.117
Additional context
No response
The text was updated successfully, but these errors were encountered: