From 16112c358d8a41db164e9b6b3d1a3027ec3f2169 Mon Sep 17 00:00:00 2001 From: Mine Starks Date: Fri, 28 Jul 2017 16:27:26 -0700 Subject: [PATCH] Missing import codefix: Take scoped packages (@foo/bar) into consideration --- src/services/codefixes/importFixes.ts | 17 +++++++++---- .../importNameCodeFixNewImportNodeModules8.ts | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/cases/fourslash/importNameCodeFixNewImportNodeModules8.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index ac477d4945503..d6fb8de9259b2 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -174,7 +174,7 @@ namespace ts.codefix { if (localSymbol && localSymbol.escapedName === name && checkSymbolHasMeaning(localSymbol, currentTokenMeaning)) { // check if this symbol is already used const symbolId = getUniqueSymbolId(localSymbol); - symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name, /*isDefault*/ true)); + symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name, /*isNamespaceImport*/ true)); } } @@ -562,8 +562,8 @@ namespace ts.codefix { function getNodeModulePathParts(fullPath: string) { // If fullPath can't be valid module file within node_modules, returns undefined. - // Example of expected pattern: /base/path/node_modules/[otherpackage/node_modules/]package/[subdirectory/]file.js - // Returns indices: ^ ^ ^ ^ + // Example of expected pattern: /base/path/node_modules/[@scope/otherpackage/@otherscope/node_modules/]package/[subdirectory/]file.js + // Returns indices: ^ ^ ^ ^ let topLevelNodeModulesIndex = 0; let topLevelPackageNameIndex = 0; @@ -573,6 +573,7 @@ namespace ts.codefix { const enum States { BeforeNodeModules, NodeModules, + Scope, PackageContent } @@ -592,8 +593,14 @@ namespace ts.codefix { } break; case States.NodeModules: - packageRootIndex = partEnd; - state = States.PackageContent; + case States.Scope: + if (state === States.NodeModules && fullPath.charAt(partStart + 1) === "@") { + state = States.Scope; + } + else { + packageRootIndex = partEnd; + state = States.PackageContent; + } break; case States.PackageContent: if (fullPath.indexOf("/node_modules/", partStart) === partStart) { diff --git a/tests/cases/fourslash/importNameCodeFixNewImportNodeModules8.ts b/tests/cases/fourslash/importNameCodeFixNewImportNodeModules8.ts new file mode 100644 index 0000000000000..f048f0d30d253 --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixNewImportNodeModules8.ts @@ -0,0 +1,25 @@ +/// + +//// [|f1/*0*/('');|] + +// @Filename: package.json +//// { "dependencies": { "package-name": "latest" } } + +// @Filename: node_modules/@scope/package-name/bin/lib/index.d.ts +//// export function f1(text: string): string; + +// @Filename: node_modules/@scope/package-name/bin/lib/index.js +//// function f1(text) { } +//// exports.f1 = f1; + +// @Filename: node_modules/@scope/package-name/package.json +//// { +//// "main": "bin/lib/index.js", +//// "types": "bin/lib/index.d.ts" +//// } + +verify.importFixAtPosition([ +`import { f1 } from "@scope/package-name"; + +f1('');` +]);