-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add support to infer the quote style for import code fix #17750
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
Conversation
@@ -999,6 +999,7 @@ namespace ts { | |||
export interface StringLiteral extends LiteralExpression { | |||
kind: SyntaxKind.StringLiteral; | |||
/* @internal */ textSourceNode?: Identifier | StringLiteral | NumericLiteral; // Allows a StringLiteral to get its text from another node (used by transforms). | |||
/* @internal */ singleQuote?: boolean; |
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.
Are backtick strings uninteresting? Would it make sense to store a quote character?
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.
A StringLiteral
can't be quoted using backtick. If it is quoted using backtick in source, then it would be a NoSubstitutionTemplateLiteral
return (<ExportDeclaration>node).moduleSpecifier; | ||
} | ||
}); | ||
if (firstModuleSpecifier && isStringLiteral(firstModuleSpecifier)) { |
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.
Keep looking if it's not a string literal?
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.
Hmm. I did this check because we might parse an invalid module specifier but we treat it as a grammar error rather than a parse error.
} | ||
}); | ||
if (firstModuleSpecifier && isStringLiteral(firstModuleSpecifier)) { | ||
return sourceFile.text.charCodeAt(skipTrivia(sourceFile.text, firstModuleSpecifier.pos)) === CharacterCodes.singleQuote; |
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 skipTrivia(sourceFile.text, firstModuleSpecifier.pos)
the same as firstModuleSpecifier.getStart()
?
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.
I suppose. I'm not generally used to working in services :)
//// export var v2 = 6; | ||
|
||
verify.importFixAtPosition([ | ||
`import { v2 } from './module2'; |
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.
Would it be interesting to add a pair of tests where two existing imports have different quote types and, in each case, the first one wins?
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.
Sure
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.
Thanks!
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.
LGTM, modulo minor comments
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.
LGTM
Does this also apply the |
@felixfbecker indeed, the tooltip seems to always use the double quotes. Not a big problem, but I guess it would be nice to fix it too. |
This adds the ability to determine whether to use single or double quotes for new imports added via code fixes. When a new import is added, we scan the top-most statements of the source file for existing import or export declarations with module specifiers. We then use the quote style of the first one we find. If there are no existing imports in the file we fall back to using double quotes.
Fixes #13270