Skip to content

Fix import addition location#17327

Merged
weswigham merged 11 commits into
microsoft:masterfrom
weswigham:fix-import-addition-location
Aug 9, 2017
Merged

Fix import addition location#17327
weswigham merged 11 commits into
microsoft:masterfrom
weswigham:fix-import-addition-location

Conversation

@weswigham
Copy link
Copy Markdown
Member

Fixes #17318

We now distinguish between pinned and jsdoc comments (and triple slash references) when looking for the start of the file for this quickfix. Namely, we not assume that jsdoc comments are meant to be after the import unless pinned.

Comment thread src/compiler/utilities.ts Outdated
}
// As well as any triple slash references
for (const range of ranges) {
if (range.kind === SyntaxKind.SingleLineCommentTrivia && (node as SourceFile).text.substring(range.pos, range.pos + 3) === "///") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we have an isDirectiveComment or something

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll search around for it later. I think its private to one of the emitters right now.

@weswigham
Copy link
Copy Markdown
Member Author

@DanielRosenwasser would you update your review status?

Copy link
Copy Markdown
Contributor

@paulvanbrenk paulvanbrenk left a comment

Choose a reason for hiding this comment

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

What happens when there is no whitespace between the pinned comment, and the attached comment? (And/Or a /// reference)?

@weswigham
Copy link
Copy Markdown
Member Author

@paulvanbrenk It should only skip triple-slash and pinned comments now, but lemme add a test for a file with all 3.

Also, with isRecognizedTripleSlashComment now patched to recognize all the semantically meaningful triple slash comments we use, there's also a few changes in our baselines - since prior to this they were (unintentionally) skipped in our JS emit.

Comment thread src/compiler/utilities.ts Outdated
/**
* Copy with new flags via RegExp constructor is ES6+, so this needs to be manually synced with the above
*/
const defaultLibReferenceRegExGIM = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/gim;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

new RegExp(defaultLibReferenceRegEx.source, "gim") is valid in ES5.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should I use that within getFileReferenceFromReferencePath, instead, then? That aligns with the intent, so it seems good.

Comment thread src/compiler/utilities.ts Outdated
export function getFileReferenceFromReferencePath(comment: string, commentRange: CommentRange): ReferencePathMatchResult {
const simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim;
const isNoDefaultLibRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/gim;
const isNoDefaultLibRegEx = new RegExp(defaultLibReferenceRegExGIM);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could also do defaultLibReferenceRegExGIM.lastIndex = 0;

Comment thread src/compiler/utilities.ts
export let fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
export let fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*<reference\s+types\s*=\s*)('|")(.+?)\2.*?\/>/;
export let fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
export let defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this need to be exported?

Copy link
Copy Markdown
Member Author

@weswigham weswigham Aug 8, 2017

Choose a reason for hiding this comment

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

We export the regexes for the rest of our matched triple-slash references, so I would assume so. Our triple-slash recognition functions are also currently all internal (prior to this PR anyway), so people need to replicate these regexes themselves if they're trying to emulate our behavior.

@weswigham weswigham merged commit 6221d70 into microsoft:master Aug 9, 2017
@weswigham weswigham deleted the fix-import-addition-location branch August 9, 2017 21:03
uniqueiniquity pushed a commit to uniqueiniquity/TypeScript that referenced this pull request Aug 9, 2017
* Add test with bug

* Fix for import placement

* Consolidate comment recognition functions into utilities

* Add another test with all 3 kinds

* Recognize path directives as part of triple slash directives

* Also handle no-default-lib triple-slash comments

* Test for all the triple-slash kinds

* Keep import-placement logic in the quickfix, since its not really a node start; accept new baselines

* Work in not-ES6, use a real no-lib comment

* Remove no default lib triple slash comment, it disables checking and thereby quick fixes

* Copy regex rather than have a regex copy
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
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.

QuickFix adds import below comments which should be attached to declaration

5 participants