Skip to content
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

getEditsForFileRename: Avoid changing import specifier ending #26177

Merged
8 commits merged into from
Aug 28, 2018

Conversation

ghost
Copy link

@ghost ghost commented Aug 3, 2018

Fixes #26028

@ghost ghost force-pushed the getEditsForFileRename_preservePathEnding branch 2 times, most recently from 22f77c9 to d40df30 Compare August 3, 2018 01:12
@@ -1,22 +0,0 @@
/// <reference path="fourslash.ts" />
Copy link
Author

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 on typeRoots because that will then be a compile error. paths should be used instead (which we already have a test for in importNameCodeFix_fromPathMapping.ts).
(Ref: microsoft/TypeScript-Handbook#692)

Copy link
Member

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)

Copy link
Author

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.

@@ -271,37 +310,14 @@ namespace ts.moduleSpecifiers {
return removeFileExtension(relativePath);
}

function tryGetModuleNameFromTypeRoots(
Copy link
Author

Choose a reason for hiding this comment

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

See below comment about typeRoots. node_modules/@types is always used for module resolution even if it's not in typeRoots; that's now handled in tryGetModuleNameAsNodeModule.

@ghost ghost force-pushed the getEditsForFileRename_preservePathEnding branch from d40df30 to 502fdbe Compare August 3, 2018 01:17
@ghost ghost requested review from weswigham and sheetalkamat August 3, 2018 02:35
return {
relativePreference: isExternalModuleNameRelative(oldImportSpecifier) ? RelativePreference.Relative : RelativePreference.NonRelative,
ending: fileExtensionIs(oldImportSpecifier, Extension.Js) ? Ending.JsExtension
: getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeJs || endsWith(oldImportSpecifier, "index") || endsWith(oldImportSpecifier, "index.js") ? Ending.Index : Ending.Minimal,
Copy link
Member

Choose a reason for hiding this comment

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

Shpuldnt this case be always false here since you already checked for extendsion to be js
endsWith(oldImportSpecifier, "index.js")

function getPreferencesForUpdate(_: ModuleSpecifierPreferences, compilerOptions: CompilerOptions, oldImportSpecifier: string): Preferences {
return {
relativePreference: isExternalModuleNameRelative(oldImportSpecifier) ? RelativePreference.Relative : RelativePreference.NonRelative,
ending: fileExtensionIs(oldImportSpecifier, Extension.Js) ? Ending.JsExtension
Copy link
Member

Choose a reason for hiding this comment

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

Need to handle js and jsx both since that's what tryResolve(Extensions.Javascript) does.

function getPreferences({ importModuleSpecifierPreference }: ModuleSpecifierPreferences, compilerOptions: CompilerOptions, importingSourceFile: SourceFile): Preferences {
return {
relativePreference: importModuleSpecifierPreference === "relative" ? RelativePreference.Relative : importModuleSpecifierPreference === "non-relative" ? RelativePreference.NonRelative : RelativePreference.Auto,
ending: usesJsExtensionOnImports(importingSourceFile) ? Ending.JsExtension
Copy link
Member

Choose a reason for hiding this comment

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

I haven't looked further to see use but don't you need to handle Json extension as well?

@@ -1,22 +0,0 @@
/// <reference path="fourslash.ts" />
Copy link
Member

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)

@ghost
Copy link
Author

ghost commented Aug 6, 2018

Latest commit exposes the ending option in UserPreferences, fixing #24779 (and doing the same task as #25073). Thanks @Kingwl for getting this started.

@ghost
Copy link
Author

ghost commented Aug 23, 2018

@sheetalkamat Please re-review

@ghost ghost merged commit b94061c into master Aug 28, 2018
@ghost ghost deleted the getEditsForFileRename_preservePathEnding branch August 28, 2018 20:03
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant