-
-
Notifications
You must be signed in to change notification settings - Fork 75
Fix: Remove all tokens inside comments from tokens array (fixes #422) #423
Conversation
Looks good! Can you delete |
7f39fe4
to
5ca72f0
Compare
@j-f1 Whoops! Not sure how that got in there. Thanks. |
572ec04
to
f33bbe2
Compare
I realized I was overthinking this and that it can can actually be simplified immensely by just not walking comment tokens. This way, comment tokens and their children will never be converted or added to the token list. My only concern now is whether my comment checking algorithm is correct or not, since I'm still not very familiar with the AST or tokens generated by TypeScript. |
f33bbe2
to
2a8fd38
Compare
lib/node-utils.js
Outdated
* @returns {boolean} is commment | ||
*/ | ||
function isComment(token) { | ||
return token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the correct check for comment tokens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeScript themselves use this within their private utilities:
function isComment(kind: SyntaxKind): boolean {
return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia;
}
I feel like we should split the JSDoc checks out into a separate function.
The TypeScript team has these (again not in their public API, but useful utils):
// JSDoc
/** True if node is of some JSDoc syntax kind. */
/* @internal */
export function isJSDocNode(node: Node): boolean {
return node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode;
}
/** True if node is of a kind that may contain comment text. */
export function isJSDocCommentContainingNode(node: Node): boolean {
return node.kind === SyntaxKind.JSDocComment || isJSDocTag(node);
}
// TODO: determine what this does before making it public.
/* @internal */
export function isJSDocTag(node: Node): boolean {
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
}
Friendly ping @JamesHenry @soda0289 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you add a unit test that would be affected by these changes? Your same source from the integration test should do the trick!
lib/node-utils.js
Outdated
* @returns {boolean} is commment | ||
*/ | ||
function isComment(token) { | ||
return token.kind === SyntaxKind.SingleLineCommentTrivia || token.kind === SyntaxKind.MultiLineCommentTrivia || (token.kind >= SyntaxKind.JSDocTypeExpression && token.kind <= SyntaxKind.JSDocTypeLiteral); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeScript themselves use this within their private utilities:
function isComment(kind: SyntaxKind): boolean {
return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia;
}
I feel like we should split the JSDoc checks out into a separate function.
The TypeScript team has these (again not in their public API, but useful utils):
// JSDoc
/** True if node is of some JSDoc syntax kind. */
/* @internal */
export function isJSDocNode(node: Node): boolean {
return node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode;
}
/** True if node is of a kind that may contain comment text. */
export function isJSDocCommentContainingNode(node: Node): boolean {
return node.kind === SyntaxKind.JSDocComment || isJSDocTag(node);
}
// TODO: determine what this does before making it public.
/* @internal */
export function isJSDocTag(node: Node): boolean {
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
}
2a8fd38
to
af4d3d7
Compare
Updated! Two questions: JSDoc CheckFor the JSDoc check, it looks to me like Does that make sense or am I missing something? Do you think we should still include those utils? Or are there other downstream consumers who might want to opt into having those tokens available to them (I don't know if Prettier would ever want to get into the business of formatting JSDoc comments, for example)? Other methodsDo Thanks for the review :) |
a91306c
to
aa41652
Compare
aa41652
to
26a0d26
Compare
Friendly ping! |
I think this is great for now, thanks! I'm going to change it to be a |
Thanks! |
This fixes an issue where TypeScript generates tokens for
{Type}
(for both the brackets and the IdentifierType
), causing the indent rule in core to have some false positives. I'm assuming these tokens are generated as part of TypeScript's JS file checking and type hinting from doc blocks.@eslint/eslint-team As an aside, could we potentially leverage this behavior for our own JSDoc parsing and validation in ESLint core? Would be nice if we could rely on a well-maintained project like TypeScript instead of doctrine, since we've been struggling to maintain that.
Maybe @JamesHenry has some idea of the feasibility of this, given your experience!
This is my first time working with this codebase or really anything TypeScript related, so please do let me know if there are better ways to achieve this or if any assumptions I've made are incorrect.
To illustrate the change, here's a before and after of the tokens and comments arrays generated for the following code:
Before this patch:
After this patch: