Merge pull request #17536 from minestarks/fix15223

Missing import codefix: Take scoped packages (@foo/bar) into consideration
This commit is contained in:
Mine Starks 2017-07-31 14:09:37 -07:00 committed by GitHub
commit 84c579586c
2 changed files with 37 additions and 5 deletions

View File

@ -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) {

View File

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