-
Notifications
You must be signed in to change notification settings - Fork 12.8k
getEditsForFileRename: Avoid changing import specifier ending #26177
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
502fdbe
getEditsForFileRename: Avoid changing import specifier ending
644568f
Support .json and .jsx extensions
caab6fb
Restore typeRoots tests
e44b33a
Fix json test
a3af5e9
When --jsx preserve is set, import ".tsx" file with ".jsx" extension
b57ecfb
Support ending preference in UserPreferences
1baedfb
Merge branch 'master' into getEditsForFileRename_preservePathEnding
9d49cc7
Merge branch 'master' into getEditsForFileRename_preservePathEnding
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/cases/fourslash/getEditsForFileRename_preservePathEnding.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowJs: true | ||
// @checkJs: true | ||
// @strict: true | ||
// @jsx: preserve | ||
// @resolveJsonModule: true | ||
|
||
// @Filename: /index.js | ||
////export const x = 0; | ||
|
||
// @Filename: /jsx.jsx | ||
////export const y = 0; | ||
|
||
// @Filename: /j.jonah.json | ||
////{ "j": 0 } | ||
|
||
// @Filename: /a.js | ||
////import { x as x0 } from "."; | ||
////import { x as x1 } from "./index"; | ||
////import { x as x2 } from "./index.js"; | ||
////import { y } from "./jsx.jsx"; | ||
////import { j } from "./j.jonah.json"; | ||
|
||
verify.noErrors(); | ||
|
||
verify.getEditsForFileRename({ | ||
oldPath: "/a.js", | ||
newPath: "/b.js", | ||
newFileContents: {}, // No change | ||
}); | ||
|
||
verify.getEditsForFileRename({ | ||
oldPath: "/b.js", | ||
newPath: "/src/b.js", | ||
newFileContents: { | ||
"/b.js": | ||
`import { x as x0 } from ".."; | ||
import { x as x1 } from "../index"; | ||
import { x as x2 } from "../index.js"; | ||
import { y } from "../jsx.jsx"; | ||
import { j } from "../j.jonah.json";`, | ||
}, | ||
}); |
45 changes: 23 additions & 22 deletions
45
tests/cases/fourslash/importNameCodeFixNewImportTypeRoots0.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @Filename: a/f1.ts | ||
//// [|foo/*0*/();|] | ||
|
||
// @Filename: types/random/index.ts | ||
//// export function foo() {}; | ||
|
||
// @Filename: tsconfig.json | ||
//// { | ||
//// "compilerOptions": { | ||
//// "typeRoots": [ | ||
//// "./types" | ||
//// ] | ||
//// } | ||
//// } | ||
|
||
verify.importFixAtPosition([ | ||
`import { foo } from "random"; | ||
|
||
foo();` | ||
]); | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @Filename: a/f1.ts | ||
//// [|foo/*0*/();|] | ||
|
||
// @Filename: types/random/index.ts | ||
//// export function foo() {}; | ||
|
||
// @Filename: tsconfig.json | ||
//// { | ||
//// "compilerOptions": { | ||
//// "typeRoots": [ | ||
//// "./types" | ||
//// ] | ||
//// } | ||
//// } | ||
|
||
// "typeRoots" does not affect module resolution. Importing from "random" would be a compile error. | ||
verify.importFixAtPosition([ | ||
`import { foo } from "../types/random"; | ||
|
||
foo();` | ||
]); |
50 changes: 27 additions & 23 deletions
50
tests/cases/fourslash/importNameCodeFixNewImportTypeRoots1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @Filename: a/f1.ts | ||
//// [|foo/*0*/();|] | ||
|
||
// @Filename: types/random/index.ts | ||
//// export function foo() {}; | ||
|
||
// @Filename: tsconfig.json | ||
//// { | ||
//// "compilerOptions": { | ||
//// "baseUrl": ".", | ||
//// "typeRoots": [ | ||
//// "./types" | ||
//// ] | ||
//// } | ||
//// } | ||
|
||
verify.importFixAtPosition([ | ||
`import { foo } from "random"; | ||
|
||
foo();` | ||
]); | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @Filename: a/f1.ts | ||
//// [|foo/*0*/();|] | ||
|
||
// @Filename: types/random/index.ts | ||
//// export function foo() {}; | ||
|
||
// @Filename: tsconfig.json | ||
//// { | ||
//// "compilerOptions": { | ||
//// "baseUrl": ".", | ||
//// "typeRoots": [ | ||
//// "./types" | ||
//// ] | ||
//// } | ||
//// } | ||
|
||
// "typeRoots" does not affect module resolution. Importing from "random" would be a compile error. | ||
verify.importFixAtPosition([ | ||
`import { foo } from "types/random"; | ||
|
||
foo();`, | ||
`import { foo } from "../types/random"; | ||
|
||
foo();` | ||
]); |
26 changes: 26 additions & 0 deletions
26
tests/cases/fourslash/importNameCodeFix_endingPreference.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @moduleResolution: node | ||
|
||
// @Filename: /foo/index.ts | ||
////export const foo = 0; | ||
|
||
// @Filename: /a.ts | ||
////foo; | ||
|
||
// @Filename: /b.ts | ||
////foo; | ||
|
||
// @Filename: /c.ts | ||
////foo; | ||
|
||
const tests: ReadonlyArray<[string, FourSlashInterface.UserPreferences["importModuleSpecifierEnding"], string]> = [ | ||
["/a.ts", "js", "./foo/index.js"], | ||
["/b.ts", "index", "./foo/index"], | ||
["/c.ts", "minimal", "./foo"], | ||
]; | ||
|
||
for (const [fileName, importModuleSpecifierEnding, specifier] of tests) { | ||
goTo.file(fileName); | ||
verify.importFixAtPosition([`import { foo } from "${specifier}";\n\nfoo;`,], /*errorCode*/ undefined, { importModuleSpecifierEnding }); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
typeRoots
doesn't affect module resolution. It affects what directories are searched for automatically adding global definitions to the project. So it doesn't make sense to synthesize a specifier based ontypeRoots
because that will then be a compile error.paths
should be used instead (which we already have a test for inimportNameCodeFix_fromPathMapping.ts
).(Ref: microsoft/TypeScript-Handbook#692)
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 dont agree with this, many times the typeRoots will add ambient module definitions to program and getting import module fix would be good. Adding @DanielRosenwasser please comment on this. (look at vscode code i believe it adds typeRoots to add additional typings)
In anycase instead of deleting this test case (where we are using typeroots, add negative test to help catch any future regressions)
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.
If there is an ambient module definition we should already be handling that without typeRoots. See
importNameCodeFixNewImportAmbient0.ts
.