Skip to content

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

Merged
merged 2 commits into from
Aug 11, 2017

Conversation

rbuckton
Copy link
Contributor

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

@@ -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;
Copy link
Member

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?

Copy link
Contributor Author

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)) {
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Member

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()?

Copy link
Contributor Author

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';
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Member

@amcasey amcasey left a 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

@rbuckton rbuckton merged commit 95ee9ae into master Aug 11, 2017
@rbuckton rbuckton deleted the importFixInferQuote branch August 11, 2017 22:52
Copy link
Contributor

@aozgaa aozgaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@felixfbecker
Copy link
Contributor

Does this also apply the import snippet suggestion? That currently always uses double quotes

@Maximaximum
Copy link

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make the quote symbol configurable for import codefix
6 participants