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

Missing import codefix: Take scoped packages (@foo/bar) into consideration #17536

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('');`
]);