Skip to content

Commit

Permalink
Missing import codefix: Take scoped packages (@foo/bar) into consider…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
minestarks committed Jul 31, 2017
1 parent 6565025 commit 16112c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -573,6 +573,7 @@ namespace ts.codefix {
const enum States {
BeforeNodeModules,
NodeModules,
Scope,
PackageContent
}

Expand All @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/importNameCodeFixNewImportNodeModules8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

//// [|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('');`
]);

0 comments on commit 16112c3

Please sign in to comment.