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

Code fix for missing imports #11768

Merged
merged 18 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ namespace ts {
isImplementationOfOverload,
getAliasedSymbol: resolveAlias,
getEmitResolver,
getExportsOfModule: getExportsOfModuleAsArray,
getExportsOfModuleAsArray,
Copy link
Contributor

Choose a reason for hiding this comment

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

we can not rename it as this is a published API and ppl are using it already. consider adding a new method instead getExportOfModule(moduleSymbol, exportName): Symbol

getExportsOfModule,
getAmbientModules,

getJsxElementAttributesType,
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,14 @@ namespace ts {
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS;
}

export function getEmitModuleResolutionKind(compilerOptions: CompilerOptions) {
let moduleResolution = compilerOptions.moduleResolution;
if (moduleResolution === undefined) {
moduleResolution = getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS ? ModuleResolutionKind.NodeJs : ModuleResolutionKind.Classic;
}
return moduleResolution;
}

/* @internal */
export function hasZeroOrOneAsteriskCharacter(str: string): boolean {
let seenAsterisk = false;
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3162,5 +3162,17 @@
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
"category": "Error",
"code": 90009
},
"Import {0} from {1}": {
"category": "Message",
"code": 90010
},
"Change {0} to {1}": {
"category": "Message",
"code": 90011
},
"Add {0} to existing import declaration from {1}": {
"category": "Message",
"code": 90012
}
}
2 changes: 1 addition & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace ts {
return { resolvedModule: resolved && resolvedModuleFromResolved(resolved, isExternalLibraryImport), failedLookupLocations };
}

function moduleHasNonRelativeName(moduleName: string): boolean {
export function moduleHasNonRelativeName(moduleName: string): boolean {
return !(isRootedDiskPath(moduleName) || isExternalModuleNameRelative(moduleName));
}

Expand Down
5 changes: 3 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,8 @@ namespace ts {
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
getAliasedSymbol(symbol: Symbol): Symbol;
getExportsOfModule(moduleSymbol: Symbol): Symbol[];
getExportsOfModuleAsArray(moduleSymbol: Symbol): Symbol[];
getExportsOfModule(moduleSymbol: Symbol): Map<Symbol> | undefined;

getJsxElementAttributesType(elementNode: JsxOpeningLikeElement): Type;
getJsxIntrinsicTagNames(): Symbol[];
Expand Down Expand Up @@ -3097,7 +3098,7 @@ namespace ts {
target?: ScriptTarget;
traceResolution?: boolean;
types?: string[];
/** Paths used to used to compute primary types search locations */
/** Paths used to compute primary types search locations */
typeRoots?: string[];
/*@internal*/ version?: boolean;
/*@internal*/ watch?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ ${code}
resetLocalData();
}

currentFileName = basePath + "/" + value;
currentFileName = ts.isRootedDiskPath(value) ? value : basePath + "/" + value;
currentFileOptions[key] = value;
}
else {
Expand Down
4 changes: 3 additions & 1 deletion src/services/codefixes/codeFixProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @internal */
/* @internal */
namespace ts {
export interface CodeFix {
errorCodes: number[];
Expand All @@ -11,6 +11,8 @@ namespace ts {
span: TextSpan;
program: Program;
newLineCharacter: string;
host: LanguageServiceHost;
cancellationToken: CancellationToken;
}

export namespace codefix {
Expand Down
1 change: 1 addition & 0 deletions src/services/codefixes/fixes.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
///<reference path='superFixes.ts' />
///<reference path='importFixes.ts' />
393 changes: 393 additions & 0 deletions src/services/codefixes/importFixes.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ namespace ts.Completions {

if (symbol && symbol.flags & SymbolFlags.HasExports) {
// Extract module or enum members
const exportedSymbols = typeChecker.getExportsOfModule(symbol);
const exportedSymbols = typeChecker.getExportsOfModuleAsArray(symbol);
forEach(exportedSymbols, symbol => {
if (typeChecker.isValidPropertyAccess(<PropertyAccessExpression>(node.parent), symbol.name)) {
symbols.push(symbol);
Expand Down Expand Up @@ -1320,7 +1320,7 @@ namespace ts.Completions {
let exports: Symbol[];
const moduleSpecifierSymbol = typeChecker.getSymbolAtLocation(importOrExportDeclaration.moduleSpecifier);
if (moduleSpecifierSymbol) {
exports = typeChecker.getExportsOfModule(moduleSpecifierSymbol);
exports = typeChecker.getExportsOfModuleAsArray(moduleSpecifierSymbol);
}

symbols = exports ? filterNamedImportOrExportCompletionItems(exports, namedImportsOrExports.elements) : emptyArray;
Expand Down
6 changes: 4 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="..\compiler\program.ts"/>
/// <reference path="..\compiler\program.ts"/>
/// <reference path="..\compiler\commandLineParser.ts"/>

/// <reference path='types.ts' />
Expand Down Expand Up @@ -1676,7 +1676,9 @@ namespace ts {
sourceFile: sourceFile,
span: span,
program: program,
newLineCharacter: newLineChar
newLineCharacter: newLineChar,
host: host,
cancellationToken: cancellationToken
};

const fixes = codefix.getFixes(context);
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

//// import [|{ v1 }|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;

verify.codeFixAtPosition(`{ v1, f1 }`);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />

//// import d, [|{ v1 }|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export default var d1 = 6;

verify.codeFixAtPosition(`{ v1, f1 }`);
21 changes: 21 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

//// import [|{
//// v1,
//// v2
//// }|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export var v2 = 5;
//// export var v3 = 5;

verify.codeFixAtPosition(
`{
v1,
v2,
f1
}`
);
20 changes: 20 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />

//// import [|{
//// v1, v2,
//// v3
//// }|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export var v2 = 5;
//// export var v3 = 5;

verify.codeFixAtPosition(
`{
v1, v2,
v3, f1
}`
);
12 changes: 12 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

//// import [|{}|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export var v2 = 5;
//// export var v3 = 5;

verify.codeFixAtPosition(`{ f1 }`);
10 changes: 10 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

//// import * as ns from "./module";
//// [|f1|]/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;

verify.codeFixAtPosition(`ns.f1`);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />

//// import d, * as ns from "./module";
//// [|f1|]/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export default var d1 = 6;

verify.codeFixAtPosition(`ns.f1`);
13 changes: 13 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

//// [|import d from "./module";
//// f1/*0*/();|]

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export default var d1 = 6;

verify.codeFixAtPosition(`import d from "./module";
import { f1 } from "./module";
f1();`);
12 changes: 12 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

//// [|import "./module";
//// f1/*0*/();|]

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;

verify.codeFixAtPosition(`import "./module";
import { f1 } from "./module";
f1();`);
13 changes: 13 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

//// import [|{ v1 }|] from "fake-module";
//// f1/*0*/();

// @Filename: ../package.json
//// { "dependencies": { "fake-module": "latest" } }

// @Filename: ../node_modules/fake-module/index.ts
//// export var v1 = 5;
//// export function f1();

verify.codeFixAtPosition(`{ v1, f1 }`);
10 changes: 10 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

//// import [|{ v1 }|] from "../other_dir/module";
//// f1/*0*/();

// @Filename: ../other_dir/module.ts
//// export var v1 = 5;
//// export function f1();

verify.codeFixAtPosition(`{ v1, f1 }`);
12 changes: 12 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

//// import [|{v1, v2, v3,}|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;
//// export var v2 = 5;
//// export var v3 = 5;

verify.codeFixAtPosition(`{v1, v2, v3, f1,}`);
17 changes: 17 additions & 0 deletions tests/cases/fourslash/importNameCodeFixExistingImport9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

//// import [|{
//// v1
//// }|] from "./module";
//// f1/*0*/();

// @Filename: module.ts
//// export function f1() {}
//// export var v1 = 5;

verify.codeFixAtPosition(
`{
v1,
f1
}`
);
14 changes: 14 additions & 0 deletions tests/cases/fourslash/importNameCodeFixNewImportAmbient0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// [|f1/*0*/();|]

// @Filename: ambientModule.ts
//// declare module "ambient-module" {
//// export function f1();
//// export var v1;
//// }

verify.codeFixAtPosition(
`import { f1 } from "ambient-module";
f1();`
);
28 changes: 28 additions & 0 deletions tests/cases/fourslash/importNameCodeFixNewImportAmbient1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />

//// import d from "other-ambient-module";
//// [|import * as ns from "yet-another-ambient-module";
//// var x = v1/*0*/ + 5;|]

// @Filename: ambientModule.ts
//// declare module "ambient-module" {
//// export function f1();
//// export var v1;
//// }

// @Filename: otherAmbientModule.ts
//// declare module "other-ambient-module" {
//// export default function f2();
//// }

// @Filename: yetAnotherAmbientModule.ts
//// declare module "yet-another-ambient-module" {
//// export function f3();
//// export var v3;
//// }

verify.codeFixAtPosition(
`import * as ns from "yet-another-ambient-module";
import { v1 } from "ambient-module";
var x = v1 + 5;`
);
20 changes: 20 additions & 0 deletions tests/cases/fourslash/importNameCodeFixNewImportAmbient2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />

//// [|/*
//// * I'm a license or something
//// */
//// f1/*0*/();|]

// @Filename: ambientModule.ts
//// declare module "ambient-module" {
//// export function f1();
//// export var v1;
//// }

verify.codeFixAtPosition(
`/*
* I'm a license or something
*/
import { f1 } from "ambient-module";
f1();`
);
Loading