diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4f144f3ff9b21..c5894a2a7ee2c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -314,6 +314,7 @@ namespace ts { const jsObjectLiteralIndexInfo = createIndexInfo(anyType, /*isReadonly*/ false); const globals = createSymbolTable(); + let ambientModulesCache: Symbol[] | undefined; /** * List of every ambient module with a "*" wildcard. * Unlike other ambient modules, these can't be stored in `globals` because symbol tables only deal with exact matches. @@ -25585,13 +25586,16 @@ namespace ts { } function getAmbientModules(): Symbol[] { - const result: Symbol[] = []; - globals.forEach((global, sym) => { - if (ambientModuleSymbolRegex.test(unescapeLeadingUnderscores(sym))) { - result.push(global); - } - }); - return result; + if (!ambientModulesCache) { + ambientModulesCache = []; + globals.forEach((global, sym) => { + // No need to `unescapeLeadingUnderscores`, an escaped symbol is never an ambient module. + if (ambientModuleSymbolRegex.test(sym as string)) { + ambientModulesCache.push(global); + } + }); + } + return ambientModulesCache; } function checkGrammarImportCallExpression(node: ImportCall): boolean { diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index af0697a12d914..2eca9dca05c4a 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1183,8 +1183,8 @@ namespace ts { } } - function isDoubleQuotedString(node: Node) { - return node.kind === SyntaxKind.StringLiteral && getSourceTextOfNodeFromSourceFile(sourceFile, node).charCodeAt(0) === CharacterCodes.doubleQuote; + function isDoubleQuotedString(node: Node): boolean { + return isStringLiteral(node) && isStringDoubleQuoted(node, sourceFile); } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index f838d6abfef22..542435b84f901 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -191,6 +191,18 @@ namespace ts { } return undefined; } + + /** Like `forEach`, but suitable for use with numbers and strings (which may be falsy). */ + export function firstDefined(array: ReadonlyArray | undefined, callback: (element: T, index: number) => U | undefined): U | undefined { + for (let i = 0; i < array.length; i++) { + const result = callback(array[i], i); + if (result !== undefined) { + return result; + } + } + return undefined; + } + /** * Iterates through the parent chain of a node and performs the callback on each parent until the callback * returns a truthy value, then returns that value. @@ -261,6 +273,16 @@ namespace ts { return undefined; } + export function findLast(array: ReadonlyArray, predicate: (element: T, index: number) => boolean): T | undefined { + for (let i = array.length - 1; i >= 0; i--) { + const value = array[i]; + if (predicate(value, i)) { + return value; + } + } + return undefined; + } + /** Works like Array.prototype.findIndex, returning `-1` if no element satisfying the predicate is found. */ export function findIndex(array: ReadonlyArray, predicate: (element: T, index: number) => boolean): number { for (let i = 0; i < array.length; i++) { @@ -1147,6 +1169,14 @@ namespace ts { return result; } + export function arrayToNumericMap(array: ReadonlyArray, makeKey: (value: T) => number): T[] { + const result: T[] = []; + for (const value of array) { + result[makeKey(value)] = value; + } + return result; + } + /** * Creates a set from the elements of an array. * diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 8cd5088049c65..f33cc4449f936 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3661,7 +3661,7 @@ "category": "Error", "code": 90010 }, - "Import {0} from {1}.": { + "Import '{0}' from \"{1}\".": { "category": "Message", "code": 90013 }, @@ -3669,7 +3669,7 @@ "category": "Message", "code": 90014 }, - "Add {0} to existing import declaration from {1}.": { + "Add '{0}' to existing import declaration from \"{1}\".": { "category": "Message", "code": 90015 }, diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index ac83dd4131195..370d02d5c935e 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -124,7 +124,11 @@ namespace ts { } } - export function getEffectiveTypeRoots(options: CompilerOptions, host: { directoryExists?: (directoryName: string) => boolean, getCurrentDirectory?: () => string }): string[] | undefined { + export interface GetEffectiveTypeRootsHost { + directoryExists?(directoryName: string): boolean; + getCurrentDirectory?(): string; + } + export function getEffectiveTypeRoots(options: CompilerOptions, host: GetEffectiveTypeRootsHost): string[] | undefined { if (options.typeRoots) { return options.typeRoots; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 42e8292b13bd8..6226e4f98c723 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1055,6 +1055,7 @@ namespace ts { export interface StringLiteral extends LiteralExpression { kind: SyntaxKind.StringLiteral; /* @internal */ textSourceNode?: Identifier | StringLiteral | NumericLiteral; // Allows a StringLiteral to get its text from another node (used by transforms). + /** Note: this is only set when synthesizing a node, not during parsing. */ /* @internal */ singleQuote?: boolean; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index c4d3de453ba5c..f907cce85b1f3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -520,6 +520,17 @@ namespace ts { } } + /* @internal */ + export function isAnyImportSyntax(node: Node): node is AnyImportSyntax { + switch (node.kind) { + case SyntaxKind.ImportDeclaration: + case SyntaxKind.ImportEqualsDeclaration: + return true; + default: + return false; + } + } + // Gets the nearest enclosing block scope container that has the provided node // as a descendant, that is not the provided node. export function getEnclosingBlockScopeContainer(node: Node): Node { @@ -1375,6 +1386,10 @@ namespace ts { return charCode === CharacterCodes.singleQuote || charCode === CharacterCodes.doubleQuote; } + export function isStringDoubleQuoted(string: StringLiteral, sourceFile: SourceFile): boolean { + return getSourceTextOfNodeFromSourceFile(sourceFile, string).charCodeAt(0) === CharacterCodes.doubleQuote; + } + /** * Returns true if the node is a variable declaration whose initializer is a function expression. * This function does not test if the node is in a JavaScript file or not. diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index de6d92eda05ed..493b0187b57ad 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -783,10 +783,10 @@ namespace FourSlash { }); } - public verifyCompletionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number) { + public verifyCompletionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean) { const completions = this.getCompletionListAtCaret(); if (completions) { - this.assertItemInCompletionList(completions.entries, symbol, text, documentation, kind, spanIndex); + this.assertItemInCompletionList(completions.entries, symbol, text, documentation, kind, spanIndex, hasAction); } else { this.raiseError(`No completions at position '${this.currentCaretPosition}' when looking for '${symbol}'.`); @@ -1127,7 +1127,7 @@ Actual: ${stringify(fullActual)}`); } private getCompletionEntryDetails(entryName: string) { - return this.languageService.getCompletionEntryDetails(this.activeFile.fileName, this.currentCaretPosition, entryName); + return this.languageService.getCompletionEntryDetails(this.activeFile.fileName, this.currentCaretPosition, entryName, this.formatCodeSettings); } private getReferencesAtCaret() { @@ -2289,6 +2289,29 @@ Actual: ${stringify(fullActual)}`); this.applyCodeActions(this.getCodeFixActions(fileName, errorCode), index); } + public applyCodeActionFromCompletion(markerName: string, options: FourSlashInterface.VerifyCompletionActionOptions) { + this.goToMarker(markerName); + + const actualCompletion = this.getCompletionListAtCaret().entries.find(e => e.name === options.name); + + if (!actualCompletion.hasAction) { + this.raiseError(`Completion for ${options.name} does not have an associated action.`); + } + + const details = this.getCompletionEntryDetails(options.name); + if (details.codeActions.length !== 1) { + this.raiseError(`Expected one code action, got ${details.codeActions.length}`); + } + + if (details.codeActions[0].description !== options.description) { + this.raiseError(`Expected description to be:\n${options.description}\ngot:\n${details.codeActions[0].description}`); + } + + this.applyCodeActions(details.codeActions); + + this.verifyNewContent(options); + } + public verifyRangeIs(expectedText: string, includeWhiteSpace?: boolean) { const ranges = this.getRanges(); if (ranges.length !== 1) { @@ -2360,6 +2383,10 @@ Actual: ${stringify(fullActual)}`); this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false); } + this.verifyNewContent(options); + } + + private verifyNewContent(options: FourSlashInterface.NewContentOptions) { if (options.newFileContent) { assert(!options.newRangeContent); this.verifyCurrentFileContent(options.newFileContent); @@ -2933,7 +2960,15 @@ Actual: ${stringify(fullActual)}`); return text.substring(startPos, endPos); } - private assertItemInCompletionList(items: ts.CompletionEntry[], name: string, text?: string, documentation?: string, kind?: string, spanIndex?: number) { + private assertItemInCompletionList( + items: ts.CompletionEntry[], + name: string, + text: string | undefined, + documentation: string | undefined, + kind: string | undefined, + spanIndex: number | undefined, + hasAction: boolean | undefined, + ) { for (const item of items) { if (item.name === name) { if (documentation !== undefined || text !== undefined) { @@ -2956,6 +2991,8 @@ Actual: ${stringify(fullActual)}`); assert.isTrue(TestState.textSpansEqual(span, item.replacementSpan), this.assertionMessageAtLastKnownMarker(stringify(span) + " does not equal " + stringify(item.replacementSpan) + " replacement span for " + name)); } + assert.equal(item.hasAction, hasAction); + return; } } @@ -3669,12 +3706,12 @@ namespace FourSlashInterface { // Verifies the completion list contains the specified symbol. The // completion list is brought up if necessary - public completionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number) { + public completionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean) { if (this.negative) { this.state.verifyCompletionListDoesNotContain(symbol, text, documentation, kind, spanIndex); } else { - this.state.verifyCompletionListContains(symbol, text, documentation, kind, spanIndex); + this.state.verifyCompletionListContains(symbol, text, documentation, kind, spanIndex, hasAction); } } @@ -3999,6 +4036,10 @@ namespace FourSlashInterface { this.state.getAndApplyCodeActions(errorCode, index); } + public applyCodeActionFromCompletion(markerName: string, options: VerifyCompletionActionOptions): void { + this.state.applyCodeActionFromCompletion(markerName, options); + } + public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void { this.state.verifyImportFixAtPosition(expectedTextArray, errorCode); } @@ -4396,12 +4437,20 @@ namespace FourSlashInterface { isNewIdentifierLocation?: boolean; } - export interface VerifyCodeFixOptions { - description: string; - // One of these should be defined. + export interface NewContentOptions { + // Exactly one of these should be defined. newFileContent?: string; newRangeContent?: string; + } + + export interface VerifyCodeFixOptions extends NewContentOptions { + description: string; errorCode?: number; index?: number; } + + export interface VerifyCompletionActionOptions extends NewContentOptions { + name: string; + description: string; + } } diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index ad79c96d833f8..527824ee145aa 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -405,8 +405,8 @@ namespace Harness.LanguageService { getCompletionsAtPosition(fileName: string, position: number): ts.CompletionInfo { return unwrapJSONCallResult(this.shim.getCompletionsAtPosition(fileName, position)); } - getCompletionEntryDetails(fileName: string, position: number, entryName: string): ts.CompletionEntryDetails { - return unwrapJSONCallResult(this.shim.getCompletionEntryDetails(fileName, position, entryName)); + getCompletionEntryDetails(fileName: string, position: number, entryName: string, options: ts.FormatCodeOptions): ts.CompletionEntryDetails { + return unwrapJSONCallResult(this.shim.getCompletionEntryDetails(fileName, position, entryName, JSON.stringify(options))); } getCompletionEntrySymbol(): ts.Symbol { throw new Error("getCompletionEntrySymbol not implemented across the shim layer."); diff --git a/src/server/client.ts b/src/server/client.ts index d08d1e13d2e66..39e30848e3e52 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -198,7 +198,9 @@ namespace ts.server { const request = this.processRequest(CommandNames.CompletionDetails, args); const response = this.processResponse(request); Debug.assert(response.body.length === 1, "Unexpected length of completion details response body."); - return response.body[0]; + + const convertedCodeActions = map(response.body[0].codeActions, codeAction => this.convertCodeActions(codeAction, fileName)); + return { ...response.body[0], codeActions: convertedCodeActions }; } getCompletionEntrySymbol(_fileName: string, _position: number, _entryName: string): Symbol { diff --git a/src/server/protocol.ts b/src/server/protocol.ts index 3d07392bbe69f..7b9e9fe80969a 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -1658,6 +1658,11 @@ namespace ts.server.protocol { * this span should be used instead of the default one. */ replacementSpan?: TextSpan; + /** + * Indicates whether commiting this completion entry will require additional code actions to be + * made to avoid errors. The CompletionEntryDetails will have these actions. + */ + hasAction?: true; } /** @@ -1690,6 +1695,11 @@ namespace ts.server.protocol { * JSDoc tags for the symbol. */ tags: JSDocTagInfo[]; + + /** + * The associated code actions for this entry + */ + codeActions?: CodeAction[]; } export interface CompletionsResponse extends Response { diff --git a/src/server/session.ts b/src/server/session.ts index 800d09ff6c283..d10daecc92d1e 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1178,11 +1178,12 @@ namespace ts.server { const completions = project.getLanguageService().getCompletionsAtPosition(file, position); if (simplifiedResult) { - return mapDefined(completions && completions.entries, entry => { + return mapDefined(completions && completions.entries, entry => { if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) { - const { name, kind, kindModifiers, sortText, replacementSpan } = entry; + const { name, kind, kindModifiers, sortText, replacementSpan, hasAction } = entry; const convertedSpan = replacementSpan ? this.decorateSpan(replacementSpan, scriptInfo) : undefined; - return { name, kind, kindModifiers, sortText, replacementSpan: convertedSpan }; + // Use `hasAction || undefined` to avoid serializing `false`. + return { name, kind, kindModifiers, sortText, replacementSpan: convertedSpan, hasAction: hasAction || undefined }; } }).sort((a, b) => compareStrings(a.name, b.name)); } @@ -1193,10 +1194,20 @@ namespace ts.server { private getCompletionEntryDetails(args: protocol.CompletionDetailsRequestArgs): ReadonlyArray { const { file, project } = this.getFileAndProject(args); - const position = this.getPositionInFile(args, file); + const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file); + const position = this.getPosition(args, scriptInfo); + const formattingOptions = project.projectService.getFormatCodeOptions(file); - return mapDefined(args.entryNames, entryName => - project.getLanguageService().getCompletionEntryDetails(file, position, entryName)); + return mapDefined(args.entryNames, entryName => { + const details = project.getLanguageService().getCompletionEntryDetails(file, position, entryName, formattingOptions); + if (details) { + const mappedCodeActions = map(details.codeActions, action => this.mapCodeAction(action, scriptInfo)); + return { ...details, codeActions: mappedCodeActions }; + } + else { + return undefined; + } + }); } private getCompileOnSaveAffectedFileList(args: protocol.FileRequestArgs): ReadonlyArray { diff --git a/src/services/codeFixProvider.ts b/src/services/codeFixProvider.ts index 13e11ed4674f3..ad9ab520dabcf 100644 --- a/src/services/codeFixProvider.ts +++ b/src/services/codeFixProvider.ts @@ -5,15 +5,13 @@ namespace ts { getCodeActions(context: CodeFixContext): CodeAction[] | undefined; } - export interface CodeFixContext { + export interface CodeFixContext extends textChanges.TextChangesContext { errorCode: number; sourceFile: SourceFile; span: TextSpan; program: Program; - newLineCharacter: string; host: LanguageServiceHost; cancellationToken: CancellationToken; - rulesProvider: formatting.RulesProvider; } export namespace codefix { diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 9b54543231dd4..3e4e0f9e82246 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -1,5 +1,7 @@ /* @internal */ namespace ts.codefix { + import ChangeTracker = textChanges.ChangeTracker; + registerCodeFix({ errorCodes: [ Diagnostics.Cannot_find_name_0.code, @@ -11,11 +13,35 @@ namespace ts.codefix { }); type ImportCodeActionKind = "CodeChange" | "InsertingIntoExistingImport" | "NewImport"; + // Map from module Id to an array of import declarations in that module. + type ImportDeclarationMap = AnyImportSyntax[][]; + interface ImportCodeAction extends CodeAction { kind: ImportCodeActionKind; moduleSpecifier?: string; } + interface SymbolContext extends textChanges.TextChangesContext { + sourceFile: SourceFile; + symbolName: string; + } + + interface SymbolAndTokenContext extends SymbolContext { + symbolToken: Node | undefined; + } + + interface ImportCodeFixContext extends SymbolAndTokenContext { + host: LanguageServiceHost; + checker: TypeChecker; + compilerOptions: CompilerOptions; + getCanonicalFileName(fileName: string): string; + cachedImportDeclarations?: ImportDeclarationMap; + } + + export interface ImportCodeFixOptions extends ImportCodeFixContext { + kind: ImportKind; + } + const enum ModuleSpecifierComparison { Better, Equal, @@ -118,561 +144,550 @@ namespace ts.codefix { } } - function getImportCodeActions(context: CodeFixContext): ImportCodeAction[] { - const sourceFile = context.sourceFile; - const checker = context.program.getTypeChecker(); - const allSourceFiles = context.program.getSourceFiles(); + function createCodeAction( + description: DiagnosticMessage, + diagnosticArgs: string[], + changes: FileTextChanges[], + kind: ImportCodeActionKind, + moduleSpecifier: string | undefined, + ): ImportCodeAction { + return { + description: formatMessage.apply(undefined, [undefined, description].concat(diagnosticArgs)), + changes, + kind, + moduleSpecifier + }; + } + + function convertToImportCodeFixContext(context: CodeFixContext): ImportCodeFixContext { const useCaseSensitiveFileNames = context.host.useCaseSensitiveFileNames ? context.host.useCaseSensitiveFileNames() : false; + const checker = context.program.getTypeChecker(); + const symbolToken = getTokenAtPosition(context.sourceFile, context.span.start, /*includeJsDocComment*/ false); + return { + host: context.host, + newLineCharacter: context.newLineCharacter, + rulesProvider: context.rulesProvider, + sourceFile: context.sourceFile, + checker, + compilerOptions: context.program.getCompilerOptions(), + cachedImportDeclarations: [], + getCanonicalFileName: createGetCanonicalFileName(useCaseSensitiveFileNames), + symbolName: symbolToken.getText(), + symbolToken, + }; + } - const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false); - const name = token.getText(); - const symbolIdActionMap = new ImportCodeActionMap(); + export const enum ImportKind { + Named, + Default, + Namespace, + } - // this is a module id -> module import declaration map - const cachedImportDeclarations: AnyImportSyntax[][] = []; - let lastImportDeclaration: Node; - - const currentTokenMeaning = getMeaningFromLocation(token); - if (context.errorCode === Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead.code) { - const umdSymbol = checker.getSymbolAtLocation(token); - let symbol: ts.Symbol; - let symbolName: string; - if (umdSymbol.flags & ts.SymbolFlags.Alias) { - symbol = checker.getAliasedSymbol(umdSymbol); - symbolName = name; - } - else if (isJsxOpeningLikeElement(token.parent) && token.parent.tagName === token) { - // The error wasn't for the symbolAtLocation, it was for the JSX tag itself, which needs access to e.g. `React`. - symbol = checker.getAliasedSymbol(checker.resolveName(checker.getJsxNamespace(), token.parent.tagName, SymbolFlags.Value)); - symbolName = symbol.name; - } - else { - Debug.fail("Either the symbol or the JSX namespace should be a UMD global if we got here"); + export function getCodeActionForImport(moduleSymbol: Symbol, context: ImportCodeFixOptions): ImportCodeAction[] { + const declarations = getImportDeclarations(moduleSymbol, context.checker, context.sourceFile, context.cachedImportDeclarations); + const actions: ImportCodeAction[] = []; + if (context.symbolToken) { + // It is possible that multiple import statements with the same specifier exist in the file. + // e.g. + // + // import * as ns from "foo"; + // import { member1, member2 } from "foo"; + // + // member3/**/ <-- cusor here + // + // in this case we should provie 2 actions: + // 1. change "member3" to "ns.member3" + // 2. add "member3" to the second import statement's import list + // and it is up to the user to decide which one fits best. + for (const declaration of declarations) { + const namespace = getNamespaceImportName(declaration); + if (namespace) { + actions.push(getCodeActionForUseExistingNamespaceImport(namespace.text, context, context.symbolToken)); + } } + } + actions.push(getCodeActionForAddImport(moduleSymbol, context, declarations)); + return actions; + } - return getCodeActionForImport(symbol, symbolName, /*isDefault*/ false, /*isNamespaceImport*/ true); + function getNamespaceImportName(declaration: AnyImportSyntax): Identifier { + if (declaration.kind === SyntaxKind.ImportDeclaration) { + const namedBindings = declaration.importClause && declaration.importClause.namedBindings; + return namedBindings && namedBindings.kind === SyntaxKind.NamespaceImport ? namedBindings.name : undefined; + } + else { + return declaration.name; } + } - const candidateModules = checker.getAmbientModules(); - for (const otherSourceFile of allSourceFiles) { - if (otherSourceFile !== sourceFile && isExternalOrCommonJsModule(otherSourceFile)) { - candidateModules.push(otherSourceFile.symbol); - } + // TODO(anhans): This doesn't seem important to cache... just use an iterator instead of creating a new array? + function getImportDeclarations(moduleSymbol: Symbol, checker: TypeChecker, { imports }: SourceFile, cachedImportDeclarations: ImportDeclarationMap = []): ReadonlyArray { + const moduleSymbolId = getUniqueSymbolId(moduleSymbol, checker); + let cached = cachedImportDeclarations[moduleSymbolId]; + if (!cached) { + cached = cachedImportDeclarations[moduleSymbolId] = mapDefined(imports, importModuleSpecifier => + checker.getSymbolAtLocation(importModuleSpecifier) === moduleSymbol ? getImportDeclaration(importModuleSpecifier) : undefined); } + return cached; + } - for (const moduleSymbol of candidateModules) { - context.cancellationToken.throwIfCancellationRequested(); + function getImportDeclaration({ parent }: LiteralExpression): AnyImportSyntax | undefined { + switch (parent.kind) { + case SyntaxKind.ImportDeclaration: + return parent as ImportDeclaration; + case SyntaxKind.ExternalModuleReference: + return (parent as ExternalModuleReference).parent; + default: + Debug.assert(parent.kind === SyntaxKind.ExportDeclaration); + // Ignore these, can't add imports to them. + return undefined; + } + } - // check the default export - const defaultExport = checker.tryGetMemberInModuleExports("default", moduleSymbol); - if (defaultExport) { - const localSymbol = getLocalSymbolForExportDefault(defaultExport); - 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, /*isNamespaceImport*/ true)); - } + function getCodeActionForNewImport(context: SymbolContext & { kind: ImportKind }, moduleSpecifier: string): ImportCodeAction { + const { kind, sourceFile, newLineCharacter, symbolName } = context; + const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax); + + const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier); + const importDecl = createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createImportClauseOfKind(kind, symbolName), createStringLiteralWithQuoteStyle(sourceFile, moduleSpecifierWithoutQuotes)); + const changes = ChangeTracker.with(context, changeTracker => { + if (lastImportDeclaration) { + changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl, { suffix: newLineCharacter }); } + else { + changeTracker.insertNodeAt(sourceFile, getSourceFileImportLocation(sourceFile), importDecl, { suffix: `${newLineCharacter}${newLineCharacter}` }); + } + }); + + // if this file doesn't have any import statements, insert an import statement and then insert a new line + // between the only import statement and user code. Otherwise just insert the statement because chances + // are there are already a new line seperating code and import statements. + return createCodeAction( + Diagnostics.Import_0_from_1, + [symbolName, moduleSpecifierWithoutQuotes], + changes, + "NewImport", + moduleSpecifierWithoutQuotes, + ); + } - // "default" is a keyword and not a legal identifier for the import, so we don't expect it here - Debug.assert(name !== "default"); + function createStringLiteralWithQuoteStyle(sourceFile: SourceFile, text: string): StringLiteral { + const literal = createLiteral(text); + const firstModuleSpecifier = firstOrUndefined(sourceFile.imports); + literal.singleQuote = !!firstModuleSpecifier && !isStringDoubleQuoted(firstModuleSpecifier, sourceFile); + return literal; + } - // check exports with the same name - const exportSymbolWithIdenticalName = checker.tryGetMemberInModuleExportsAndProperties(name, moduleSymbol); - if (exportSymbolWithIdenticalName && checkSymbolHasMeaning(exportSymbolWithIdenticalName, currentTokenMeaning)) { - const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName); - symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name)); - } + function createImportClauseOfKind(kind: ImportKind, symbolName: string) { + switch (kind) { + case ImportKind.Default: + return createImportClause(createIdentifier(symbolName), /*namedBindings*/ undefined); + case ImportKind.Namespace: + return createImportClause(/*name*/ undefined, createNamespaceImport(createIdentifier(symbolName))); + case ImportKind.Named: + return createImportClause(/*name*/ undefined, createNamedImports([createImportSpecifier(/*propertyName*/ undefined, createIdentifier(symbolName))])); + default: + Debug.assertNever(kind); } + } - return symbolIdActionMap.getAllActions(); - - function getImportDeclarations(moduleSymbol: Symbol) { - const moduleSymbolId = getUniqueSymbolId(moduleSymbol); + function getModuleSpecifierForNewImport(sourceFile: SourceFile, moduleSymbol: Symbol, options: CompilerOptions, getCanonicalFileName: (file: string) => string, host: LanguageServiceHost): string | undefined { + const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().fileName; + const sourceDirectory = getDirectoryPath(sourceFile.fileName); - const cached = cachedImportDeclarations[moduleSymbolId]; - if (cached) { - return cached; - } + return tryGetModuleNameFromAmbientModule(moduleSymbol) || + tryGetModuleNameFromTypeRoots(options, host, getCanonicalFileName, moduleFileName) || + tryGetModuleNameAsNodeModule(options, moduleFileName, host, getCanonicalFileName, sourceDirectory) || + tryGetModuleNameFromBaseUrl(options, moduleFileName, getCanonicalFileName) || + options.rootDirs && tryGetModuleNameFromRootDirs(options.rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName) || + removeFileExtension(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName)); + } - const existingDeclarations = mapDefined(sourceFile.imports, importModuleSpecifier => - checker.getSymbolAtLocation(importModuleSpecifier) === moduleSymbol ? getImportDeclaration(importModuleSpecifier) : undefined); - cachedImportDeclarations[moduleSymbolId] = existingDeclarations; - return existingDeclarations; - - function getImportDeclaration({ parent }: LiteralExpression): AnyImportSyntax { - switch (parent.kind) { - case SyntaxKind.ImportDeclaration: - return parent as ImportDeclaration; - case SyntaxKind.ExternalModuleReference: - return (parent as ExternalModuleReference).parent; - default: - return undefined; - } - } + function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol): string | undefined { + const decl = moduleSymbol.valueDeclaration; + if (isModuleDeclaration(decl) && isStringLiteral(decl.name)) { + return decl.name.text; } + } - function getUniqueSymbolId(symbol: Symbol) { - return getSymbolId(skipAlias(symbol, checker)); + function tryGetModuleNameFromBaseUrl(options: CompilerOptions, moduleFileName: string, getCanonicalFileName: (file: string) => string): string | undefined { + if (!options.baseUrl) { + return undefined; } - function checkSymbolHasMeaning(symbol: Symbol, meaning: SemanticMeaning) { - const declarations = symbol.getDeclarations(); - return declarations ? some(symbol.declarations, decl => !!(getMeaningFromDeclaration(decl) & meaning)) : false; + let relativeName = getRelativePathIfInDirectory(moduleFileName, options.baseUrl, getCanonicalFileName); + if (!relativeName) { + return undefined; } - function getCodeActionForImport(moduleSymbol: Symbol, symbolName: string, isDefault?: boolean, isNamespaceImport?: boolean): ImportCodeAction[] { - const existingDeclarations = getImportDeclarations(moduleSymbol); - if (existingDeclarations.length > 0) { - // With an existing import statement, there are more than one actions the user can do. - return getCodeActionsForExistingImport(existingDeclarations); - } - else { - return [getCodeActionForNewImport()]; - } + const relativeNameWithIndex = removeFileExtension(relativeName); + relativeName = removeExtensionAndIndexPostFix(relativeName); - function getCodeActionsForExistingImport(declarations: (ImportDeclaration | ImportEqualsDeclaration)[]): ImportCodeAction[] { - const actions: ImportCodeAction[] = []; - - // It is possible that multiple import statements with the same specifier exist in the file. - // e.g. - // - // import * as ns from "foo"; - // import { member1, member2 } from "foo"; - // - // member3/**/ <-- cusor here - // - // in this case we should provie 2 actions: - // 1. change "member3" to "ns.member3" - // 2. add "member3" to the second import statement's import list - // and it is up to the user to decide which one fits best. - let namespaceImportDeclaration: ImportDeclaration | ImportEqualsDeclaration; - let namedImportDeclaration: ImportDeclaration; - let existingModuleSpecifier: string; - for (const declaration of declarations) { - if (declaration.kind === SyntaxKind.ImportDeclaration) { - const namedBindings = declaration.importClause && declaration.importClause.namedBindings; - if (namedBindings && namedBindings.kind === SyntaxKind.NamespaceImport) { - // case: - // import * as ns from "foo" - namespaceImportDeclaration = declaration; - } - else { - // cases: - // import default from "foo" - // import { bar } from "foo" or combination with the first one - // import "foo" - namedImportDeclaration = declaration; + if (options.paths) { + for (const key in options.paths) { + for (const pattern of options.paths[key]) { + const indexOfStar = pattern.indexOf("*"); + if (indexOfStar === 0 && pattern.length === 1) { + continue; + } + else if (indexOfStar !== -1) { + const prefix = pattern.substr(0, indexOfStar); + const suffix = pattern.substr(indexOfStar + 1); + if (relativeName.length >= prefix.length + suffix.length && + startsWith(relativeName, prefix) && + endsWith(relativeName, suffix)) { + const matchedStar = relativeName.substr(prefix.length, relativeName.length - suffix.length); + return key.replace("\*", matchedStar); } - existingModuleSpecifier = declaration.moduleSpecifier.getText(); } - else { - // case: - // import foo = require("foo") - namespaceImportDeclaration = declaration; - existingModuleSpecifier = getModuleSpecifierFromImportEqualsDeclaration(declaration); + else if (pattern === relativeName || pattern === relativeNameWithIndex) { + return key; } } + } + } - if (namespaceImportDeclaration) { - actions.push(getCodeActionForNamespaceImport(namespaceImportDeclaration)); - } - - if (!isNamespaceImport && namedImportDeclaration && namedImportDeclaration.importClause && - (namedImportDeclaration.importClause.name || namedImportDeclaration.importClause.namedBindings)) { - /** - * If the existing import declaration already has a named import list, just - * insert the identifier into that list. - */ - const fileTextChanges = getTextChangeForImportClause(namedImportDeclaration.importClause); - const moduleSpecifierWithoutQuotes = stripQuotes(namedImportDeclaration.moduleSpecifier.getText()); - actions.push(createCodeAction( - Diagnostics.Add_0_to_existing_import_declaration_from_1, - [name, moduleSpecifierWithoutQuotes], - fileTextChanges, - "InsertingIntoExistingImport", - moduleSpecifierWithoutQuotes - )); - } - else { - // we need to create a new import statement, but the existing module specifier can be reused. - actions.push(getCodeActionForNewImport(existingModuleSpecifier)); - } - return actions; - - function getModuleSpecifierFromImportEqualsDeclaration(declaration: ImportEqualsDeclaration) { - if (declaration.moduleReference && declaration.moduleReference.kind === SyntaxKind.ExternalModuleReference) { - return declaration.moduleReference.expression.getText(); - } - return declaration.moduleReference.getText(); - } + return relativeName; + } - function getTextChangeForImportClause(importClause: ImportClause): FileTextChanges[] { - const importList = importClause.namedBindings; - const newImportSpecifier = createImportSpecifier(/*propertyName*/ undefined, createIdentifier(name)); - // case 1: - // original text: import default from "module" - // change to: import default, { name } from "module" - // case 2: - // original text: import {} from "module" - // change to: import { name } from "module" - if (!importList || importList.elements.length === 0) { - const newImportClause = createImportClause(importClause.name, createNamedImports([newImportSpecifier])); - return createChangeTracker().replaceNode(sourceFile, importClause, newImportClause).getChanges(); - } + function tryGetModuleNameFromRootDirs(rootDirs: ReadonlyArray, moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string): string | undefined { + const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName); + if (normalizedTargetPath === undefined) { + return undefined; + } - /** - * If the import list has one import per line, preserve that. Otherwise, insert on same line as last element - * import { - * foo - * } from "./module"; - */ - return createChangeTracker().insertNodeInListAfter( - sourceFile, - importList.elements[importList.elements.length - 1], - newImportSpecifier).getChanges(); - } + const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, rootDirs, getCanonicalFileName); + const relativePath = normalizedSourcePath !== undefined ? getRelativePath(normalizedTargetPath, normalizedSourcePath, getCanonicalFileName) : normalizedTargetPath; + return removeFileExtension(relativePath); + } - function getCodeActionForNamespaceImport(declaration: ImportDeclaration | ImportEqualsDeclaration): ImportCodeAction { - let namespacePrefix: string; - if (declaration.kind === SyntaxKind.ImportDeclaration) { - namespacePrefix = (declaration.importClause.namedBindings).name.getText(); - } - else { - namespacePrefix = declaration.name.getText(); - } - namespacePrefix = stripQuotes(namespacePrefix); - - /** - * Cases: - * import * as ns from "mod" - * import default, * as ns from "mod" - * import ns = require("mod") - * - * Because there is no import list, we alter the reference to include the - * namespace instead of altering the import declaration. For example, "foo" would - * become "ns.foo" - */ - return createCodeAction( - Diagnostics.Change_0_to_1, - [name, `${namespacePrefix}.${name}`], - createChangeTracker().replaceNode(sourceFile, token, createPropertyAccess(createIdentifier(namespacePrefix), name)).getChanges(), - "CodeChange" - ); - } + function tryGetModuleNameFromTypeRoots( + options: CompilerOptions, + host: GetEffectiveTypeRootsHost, + getCanonicalFileName: (file: string) => string, + moduleFileName: string, + ): string | undefined { + return firstDefined(getEffectiveTypeRoots(options, host), unNormalizedTypeRoot => { + const typeRoot = toPath(unNormalizedTypeRoot, /*basePath*/ undefined, getCanonicalFileName); + if (startsWith(moduleFileName, typeRoot)) { + return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1)); } + }); + } - function getCodeActionForNewImport(moduleSpecifier?: string): ImportCodeAction { - if (!lastImportDeclaration) { - // insert after any existing imports - for (let i = sourceFile.statements.length - 1; i >= 0; i--) { - const statement = sourceFile.statements[i]; - if (statement.kind === SyntaxKind.ImportEqualsDeclaration || statement.kind === SyntaxKind.ImportDeclaration) { - lastImportDeclaration = statement; - break; - } - } - } + function tryGetModuleNameAsNodeModule( + options: CompilerOptions, + moduleFileName: string, + host: LanguageServiceHost, + getCanonicalFileName: (file: string) => string, + sourceDirectory: string, + ): string | undefined { + if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) { + // nothing to do here + return undefined; + } - const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames); - const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier || getModuleSpecifierForNewImport()); - const changeTracker = createChangeTracker(); - const importClause = isDefault - ? createImportClause(createIdentifier(symbolName), /*namedBindings*/ undefined) - : isNamespaceImport - ? createImportClause(/*name*/ undefined, createNamespaceImport(createIdentifier(symbolName))) - : createImportClause(/*name*/ undefined, createNamedImports([createImportSpecifier(/*propertyName*/ undefined, createIdentifier(symbolName))])); - const moduleSpecifierLiteral = createLiteral(moduleSpecifierWithoutQuotes); - moduleSpecifierLiteral.singleQuote = getSingleQuoteStyleFromExistingImports(); - const importDecl = createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, importClause, moduleSpecifierLiteral); - if (!lastImportDeclaration) { - changeTracker.insertNodeAt(sourceFile, getSourceFileImportLocation(sourceFile), importDecl, { suffix: `${context.newLineCharacter}${context.newLineCharacter}` }); - } - else { - changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl, { suffix: context.newLineCharacter }); - } + const parts = getNodeModulePathParts(moduleFileName); - // if this file doesn't have any import statements, insert an import statement and then insert a new line - // between the only import statement and user code. Otherwise just insert the statement because chances - // are there are already a new line seperating code and import statements. - return createCodeAction( - Diagnostics.Import_0_from_1, - [symbolName, `"${moduleSpecifierWithoutQuotes}"`], - changeTracker.getChanges(), - "NewImport", - moduleSpecifierWithoutQuotes - ); - - function getSingleQuoteStyleFromExistingImports() { - const firstModuleSpecifier = forEach(sourceFile.statements, node => { - if (isImportDeclaration(node) || isExportDeclaration(node)) { - if (node.moduleSpecifier && isStringLiteral(node.moduleSpecifier)) { - return node.moduleSpecifier; - } - } - else if (isImportEqualsDeclaration(node)) { - if (isExternalModuleReference(node.moduleReference) && isStringLiteral(node.moduleReference.expression)) { - return node.moduleReference.expression; - } + if (!parts) { + return undefined; + } + + // Simplify the full file path to something that can be resolved by Node. + + // If the module could be imported by a directory name, use that directory's name + let moduleSpecifier = getDirectoryOrExtensionlessFileName(moduleFileName); + // Get a path that's relative to node_modules or the importing file's path + moduleSpecifier = getNodeResolvablePath(moduleSpecifier); + // If the module was found in @types, get the actual Node package name + return getPackageNameFromAtTypesDirectory(moduleSpecifier); + + function getDirectoryOrExtensionlessFileName(path: string): string { + // If the file is the main module, it can be imported by the package name + const packageRootPath = path.substring(0, parts.packageRootIndex); + const packageJsonPath = combinePaths(packageRootPath, "package.json"); + if (host.fileExists(packageJsonPath)) { + const packageJsonContent = JSON.parse(host.readFile(packageJsonPath)); + if (packageJsonContent) { + const mainFileRelative = packageJsonContent.typings || packageJsonContent.types || packageJsonContent.main; + if (mainFileRelative) { + const mainExportFile = toPath(mainFileRelative, packageRootPath, getCanonicalFileName); + if (mainExportFile === getCanonicalFileName(path)) { + return packageRootPath; } - }); - if (firstModuleSpecifier) { - return sourceFile.text.charCodeAt(firstModuleSpecifier.getStart()) === CharacterCodes.singleQuote; } } + } - function getModuleSpecifierForNewImport() { - const fileName = sourceFile.fileName; - const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().fileName; - const sourceDirectory = getDirectoryPath(fileName); - const options = context.program.getCompilerOptions(); - - return tryGetModuleNameFromAmbientModule() || - tryGetModuleNameFromTypeRoots() || - tryGetModuleNameAsNodeModule() || - tryGetModuleNameFromBaseUrl() || - tryGetModuleNameFromRootDirs() || - removeFileExtension(getRelativePath(moduleFileName, sourceDirectory)); - - function tryGetModuleNameFromAmbientModule(): string { - const decl = moduleSymbol.valueDeclaration; - if (isModuleDeclaration(decl) && isStringLiteral(decl.name)) { - return decl.name.text; - } - } + // We still have a file name - remove the extension + const fullModulePathWithoutExtension = removeFileExtension(path); - function tryGetModuleNameFromBaseUrl() { - if (!options.baseUrl) { - return undefined; - } + // If the file is /index, it can be imported by its directory name + if (getCanonicalFileName(fullModulePathWithoutExtension.substring(parts.fileNameIndex)) === "/index") { + return fullModulePathWithoutExtension.substring(0, parts.fileNameIndex); + } - let relativeName = getRelativePathIfInDirectory(moduleFileName, options.baseUrl); - if (!relativeName) { - return undefined; - } + return fullModulePathWithoutExtension; + } - const relativeNameWithIndex = removeFileExtension(relativeName); - relativeName = removeExtensionAndIndexPostFix(relativeName); - - if (options.paths) { - for (const key in options.paths) { - for (const pattern of options.paths[key]) { - const indexOfStar = pattern.indexOf("*"); - if (indexOfStar === 0 && pattern.length === 1) { - continue; - } - else if (indexOfStar !== -1) { - const prefix = pattern.substr(0, indexOfStar); - const suffix = pattern.substr(indexOfStar + 1); - if (relativeName.length >= prefix.length + suffix.length && - startsWith(relativeName, prefix) && - endsWith(relativeName, suffix)) { - const matchedStar = relativeName.substr(prefix.length, relativeName.length - suffix.length); - return key.replace("\*", matchedStar); - } - } - else if (pattern === relativeName || pattern === relativeNameWithIndex) { - return key; - } - } - } - } + function getNodeResolvablePath(path: string): string { + const basePath = path.substring(0, parts.topLevelNodeModulesIndex); + if (sourceDirectory.indexOf(basePath) === 0) { + // if node_modules folder is in this folder or any of its parent folders, no need to keep it. + return path.substring(parts.topLevelPackageNameIndex + 1); + } + else { + return getRelativePath(path, sourceDirectory, getCanonicalFileName); + } + } + } - return relativeName; - } + 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/[@scope/otherpackage/@otherscope/node_modules/]package/[subdirectory/]file.js + // Returns indices: ^ ^ ^ ^ + + let topLevelNodeModulesIndex = 0; + let topLevelPackageNameIndex = 0; + let packageRootIndex = 0; + let fileNameIndex = 0; + + const enum States { + BeforeNodeModules, + NodeModules, + Scope, + PackageContent + } - function tryGetModuleNameFromRootDirs() { - if (options.rootDirs) { - const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, options.rootDirs); - const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, options.rootDirs); - if (normalizedTargetPath !== undefined) { - const relativePath = normalizedSourcePath !== undefined ? getRelativePath(normalizedTargetPath, normalizedSourcePath) : normalizedTargetPath; - return removeFileExtension(relativePath); - } - } - return undefined; + let partStart = 0; + let partEnd = 0; + let state = States.BeforeNodeModules; + + while (partEnd >= 0) { + partStart = partEnd; + partEnd = fullPath.indexOf("/", partStart + 1); + switch (state) { + case States.BeforeNodeModules: + if (fullPath.indexOf("/node_modules/", partStart) === partStart) { + topLevelNodeModulesIndex = partStart; + topLevelPackageNameIndex = partEnd; + state = States.NodeModules; } - - function tryGetModuleNameFromTypeRoots() { - const typeRoots = getEffectiveTypeRoots(options, context.host); - if (typeRoots) { - const normalizedTypeRoots = map(typeRoots, typeRoot => toPath(typeRoot, /*basePath*/ undefined, getCanonicalFileName)); - for (const typeRoot of normalizedTypeRoots) { - if (startsWith(moduleFileName, typeRoot)) { - const relativeFileName = moduleFileName.substring(typeRoot.length + 1); - return removeExtensionAndIndexPostFix(relativeFileName); - } - } - } + break; + case States.NodeModules: + 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) { + state = States.NodeModules; + } + else { + state = States.PackageContent; + } + break; + } + } - function tryGetModuleNameAsNodeModule() { - if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) { - // nothing to do here - return undefined; - } - - const parts = getNodeModulePathParts(moduleFileName); - - if (!parts) { - return undefined; - } + fileNameIndex = partStart; - // Simplify the full file path to something that can be resolved by Node. - - // If the module could be imported by a directory name, use that directory's name - let moduleSpecifier = getDirectoryOrExtensionlessFileName(moduleFileName); - // Get a path that's relative to node_modules or the importing file's path - moduleSpecifier = getNodeResolvablePath(moduleSpecifier); - // If the module was found in @types, get the actual Node package name - return getPackageNameFromAtTypesDirectory(moduleSpecifier); - - function getDirectoryOrExtensionlessFileName(path: string): string { - // If the file is the main module, it can be imported by the package name - const packageRootPath = path.substring(0, parts.packageRootIndex); - const packageJsonPath = combinePaths(packageRootPath, "package.json"); - if (context.host.fileExists(packageJsonPath)) { - const packageJsonContent = JSON.parse(context.host.readFile(packageJsonPath)); - if (packageJsonContent) { - const mainFileRelative = packageJsonContent.typings || packageJsonContent.types || packageJsonContent.main; - if (mainFileRelative) { - const mainExportFile = toPath(mainFileRelative, packageRootPath, getCanonicalFileName); - if (mainExportFile === getCanonicalFileName(path)) { - return packageRootPath; - } - } - } - } - - // We still have a file name - remove the extension - const fullModulePathWithoutExtension = removeFileExtension(path); - - // If the file is /index, it can be imported by its directory name - if (getCanonicalFileName(fullModulePathWithoutExtension.substring(parts.fileNameIndex)) === "/index") { - return fullModulePathWithoutExtension.substring(0, parts.fileNameIndex); - } - - return fullModulePathWithoutExtension; - } + return state > States.NodeModules ? { topLevelNodeModulesIndex, topLevelPackageNameIndex, packageRootIndex, fileNameIndex } : undefined; + } - function getNodeResolvablePath(path: string): string { - const basePath = path.substring(0, parts.topLevelNodeModulesIndex); - if (sourceDirectory.indexOf(basePath) === 0) { - // if node_modules folder is in this folder or any of its parent folders, no need to keep it. - return path.substring(parts.topLevelPackageNameIndex + 1); - } - else { - return getRelativePath(path, sourceDirectory); - } - } - } - } + function getPathRelativeToRootDirs(path: string, rootDirs: ReadonlyArray, getCanonicalFileName: (fileName: string) => string): string | undefined { + return firstDefined(rootDirs, rootDir => getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName)); + } - 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/[@scope/otherpackage/@otherscope/node_modules/]package/[subdirectory/]file.js - // Returns indices: ^ ^ ^ ^ - - let topLevelNodeModulesIndex = 0; - let topLevelPackageNameIndex = 0; - let packageRootIndex = 0; - let fileNameIndex = 0; - - const enum States { - BeforeNodeModules, - NodeModules, - Scope, - PackageContent - } + function removeExtensionAndIndexPostFix(fileName: string) { + fileName = removeFileExtension(fileName); + if (endsWith(fileName, "/index")) { + fileName = fileName.substr(0, fileName.length - 6/* "/index".length */); + } + return fileName; + } - let partStart = 0; - let partEnd = 0; - let state = States.BeforeNodeModules; - - while (partEnd >= 0) { - partStart = partEnd; - partEnd = fullPath.indexOf("/", partStart + 1); - switch (state) { - case States.BeforeNodeModules: - if (fullPath.indexOf("/node_modules/", partStart) === partStart) { - topLevelNodeModulesIndex = partStart; - topLevelPackageNameIndex = partEnd; - state = States.NodeModules; - } - break; - case States.NodeModules: - 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) { - state = States.NodeModules; - } - else { - state = States.PackageContent; - } - break; - } - } + function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: (fileName: string) => string): string | undefined { + const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); + return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath; + } - fileNameIndex = partStart; + function getRelativePath(path: string, directoryPath: string, getCanonicalFileName: (fileName: string) => string) { + const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); + return !pathIsRelative(relativePath) ? "./" + relativePath : relativePath; + } - return state > States.NodeModules ? { topLevelNodeModulesIndex, topLevelPackageNameIndex, packageRootIndex, fileNameIndex } : undefined; + function getCodeActionForAddImport( + moduleSymbol: Symbol, + ctx: ImportCodeFixOptions, + declarations: ReadonlyArray): ImportCodeAction { + const fromExistingImport = firstDefined(declarations, declaration => { + if (declaration.kind === SyntaxKind.ImportDeclaration && declaration.importClause) { + const changes = tryUpdateExistingImport(ctx, ctx.kind, declaration.importClause); + if (changes) { + const moduleSpecifierWithoutQuotes = stripQuotes(declaration.moduleSpecifier.getText()); + return createCodeAction( + Diagnostics.Add_0_to_existing_import_declaration_from_1, + [ctx.symbolName, moduleSpecifierWithoutQuotes], + changes, + "InsertingIntoExistingImport", + moduleSpecifierWithoutQuotes); } + } + }); + if (fromExistingImport) { + return fromExistingImport; + } - function getPathRelativeToRootDirs(path: string, rootDirs: string[]) { - for (const rootDir of rootDirs) { - const relativeName = getRelativePathIfInDirectory(path, rootDir); - if (relativeName !== undefined) { - return relativeName; - } - } - return undefined; - } + const moduleSpecifier = firstDefined(declarations, moduleSpecifierFromAnyImport) + || getModuleSpecifierForNewImport(ctx.sourceFile, moduleSymbol, ctx.compilerOptions, ctx.getCanonicalFileName, ctx.host); + return getCodeActionForNewImport(ctx, moduleSpecifier); + } - function removeExtensionAndIndexPostFix(fileName: string) { - fileName = removeFileExtension(fileName); - if (endsWith(fileName, "/index")) { - fileName = fileName.substr(0, fileName.length - 6/* "/index".length */); - } - return fileName; - } + function moduleSpecifierFromAnyImport(node: AnyImportSyntax): string | undefined { + const expression = node.kind === SyntaxKind.ImportDeclaration + ? node.moduleSpecifier + : node.moduleReference.kind === SyntaxKind.ExternalModuleReference + ? node.moduleReference.expression + : undefined; + return expression && isStringLiteral(expression) ? expression.text : undefined; + } - function getRelativePathIfInDirectory(path: string, directoryPath: string) { - const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); - return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath; + function tryUpdateExistingImport(context: SymbolContext, kind: ImportKind, importClause: ImportClause): FileTextChanges[] | undefined { + const { symbolName, sourceFile } = context; + const { name, namedBindings } = importClause; + switch (kind) { + case ImportKind.Default: + return name ? undefined : ChangeTracker.with(context, t => + t.replaceNode(sourceFile, importClause, createImportClause(createIdentifier(symbolName), namedBindings))); + + case ImportKind.Named: { + const newImportSpecifier = createImportSpecifier(/*propertyName*/ undefined, createIdentifier(symbolName)); + if (namedBindings && namedBindings.kind === SyntaxKind.NamedImports && namedBindings.elements.length !== 0) { + // There are already named imports; add another. + return ChangeTracker.with(context, t => t.insertNodeInListAfter( + sourceFile, + namedBindings.elements[namedBindings.elements.length - 1], + newImportSpecifier)); } - - function getRelativePath(path: string, directoryPath: string) { - const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); - return !pathIsRelative(relativePath) ? "./" + relativePath : relativePath; + if (!namedBindings || namedBindings.kind === SyntaxKind.NamedImports && namedBindings.elements.length === 0) { + return ChangeTracker.with(context, t => + t.replaceNode(sourceFile, importClause, createImportClause(name, createNamedImports([newImportSpecifier])))); } + return undefined; } + case ImportKind.Namespace: + return namedBindings ? undefined : ChangeTracker.with(context, t => + t.replaceNode(sourceFile, importClause, createImportClause(name, createNamespaceImport(createIdentifier(symbolName))))); + + default: + Debug.assertNever(kind); } + } + + function getCodeActionForUseExistingNamespaceImport(namespacePrefix: string, context: SymbolContext, symbolToken: Node): ImportCodeAction { + const { symbolName, sourceFile } = context; + + /** + * Cases: + * import * as ns from "mod" + * import default, * as ns from "mod" + * import ns = require("mod") + * + * Because there is no import list, we alter the reference to include the + * namespace instead of altering the import declaration. For example, "foo" would + * become "ns.foo" + */ + return createCodeAction( + Diagnostics.Change_0_to_1, + [symbolName, `${namespacePrefix}.${symbolName}`], + ChangeTracker.with(context, tracker => + tracker.replaceNode(sourceFile, symbolToken, createPropertyAccess(createIdentifier(namespacePrefix), symbolName))), + "CodeChange", + /*moduleSpecifier*/ undefined); + } + + function getImportCodeActions(context: CodeFixContext): ImportCodeAction[] { + const importFixContext = convertToImportCodeFixContext(context); + return context.errorCode === Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead.code + ? getActionsForUMDImport(importFixContext) + : getActionsForNonUMDImport(importFixContext, context.program.getSourceFiles(), context.cancellationToken); + } - function createChangeTracker() { - return textChanges.ChangeTracker.fromContext(context); + function getActionsForUMDImport(context: ImportCodeFixContext): ImportCodeAction[] { + const { checker, symbolToken } = context; + const umdSymbol = checker.getSymbolAtLocation(symbolToken); + let symbol: ts.Symbol; + let symbolName: string; + if (umdSymbol.flags & ts.SymbolFlags.Alias) { + symbol = checker.getAliasedSymbol(umdSymbol); + symbolName = context.symbolName; } + else if (isJsxOpeningLikeElement(symbolToken.parent) && symbolToken.parent.tagName === symbolToken) { + // The error wasn't for the symbolAtLocation, it was for the JSX tag itself, which needs access to e.g. `React`. + symbol = checker.getAliasedSymbol(checker.resolveName(checker.getJsxNamespace(), symbolToken.parent.tagName, SymbolFlags.Value)); + symbolName = symbol.name; + } + else { + Debug.fail("Either the symbol or the JSX namespace should be a UMD global if we got here"); + } + + return getCodeActionForImport(symbol, { ...context, symbolName, kind: ImportKind.Namespace }); + } + + function getActionsForNonUMDImport(context: ImportCodeFixContext, allSourceFiles: ReadonlyArray, cancellationToken: CancellationToken): ImportCodeAction[] { + const { sourceFile, checker, symbolName, symbolToken } = context; + // "default" is a keyword and not a legal identifier for the import, so we don't expect it here + Debug.assert(symbolName !== "default"); + const symbolIdActionMap = new ImportCodeActionMap(); + const currentTokenMeaning = getMeaningFromLocation(symbolToken); + + forEachExternalModule(checker, allSourceFiles, moduleSymbol => { + if (moduleSymbol === sourceFile.symbol) { + return; + } + + cancellationToken.throwIfCancellationRequested(); + // check the default export + const defaultExport = checker.tryGetMemberInModuleExports("default", moduleSymbol); + if (defaultExport) { + const localSymbol = getLocalSymbolForExportDefault(defaultExport); + if (localSymbol && localSymbol.escapedName === symbolName && checkSymbolHasMeaning(localSymbol, currentTokenMeaning)) { + // check if this symbol is already used + const symbolId = getUniqueSymbolId(localSymbol, checker); + symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Default })); + } + } - function createCodeAction( - description: DiagnosticMessage, - diagnosticArgs: string[], - changes: FileTextChanges[], - kind: ImportCodeActionKind, - moduleSpecifier?: string): ImportCodeAction { - return { - description: formatMessage.apply(undefined, [undefined, description].concat(diagnosticArgs)), - changes, - kind, - moduleSpecifier - }; + // check exports with the same name + const exportSymbolWithIdenticalName = checker.tryGetMemberInModuleExportsAndProperties(symbolName, moduleSymbol); + if (exportSymbolWithIdenticalName && checkSymbolHasMeaning(exportSymbolWithIdenticalName, currentTokenMeaning)) { + const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName, checker); + symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Named })); + } + }); + + return symbolIdActionMap.getAllActions(); + } + + function checkSymbolHasMeaning({ declarations }: Symbol, meaning: SemanticMeaning): boolean { + return some(declarations, decl => !!(getMeaningFromDeclaration(decl) & meaning)); + } + + export function forEachExternalModule(checker: TypeChecker, allSourceFiles: ReadonlyArray, cb: (module: Symbol) => void) { + for (const ambient of checker.getAmbientModules()) { + cb(ambient); + } + for (const sourceFile of allSourceFiles) { + if (isExternalOrCommonJsModule(sourceFile)) { + cb(sourceFile.symbol); + } } } } diff --git a/src/services/completions.ts b/src/services/completions.ts index c222425693709..7afa85ce26df4 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -4,13 +4,31 @@ namespace ts.Completions { export type Log = (message: string) => void; + interface SymbolOriginInfo { + moduleSymbol: Symbol; + isDefaultExport: boolean; + } + /** + * Map from symbol id -> SymbolOriginInfo. + * Only populated for symbols that come from other modules. + */ + type SymbolOriginInfoMap = SymbolOriginInfo[]; + const enum KeywordCompletionFilters { None, ClassElementKeywords, // Keywords at class keyword ConstructorParameterKeywords, // Keywords at constructor parameter } - export function getCompletionsAtPosition(host: LanguageServiceHost, typeChecker: TypeChecker, log: Log, compilerOptions: CompilerOptions, sourceFile: SourceFile, position: number): CompletionInfo | undefined { + export function getCompletionsAtPosition( + host: LanguageServiceHost, + typeChecker: TypeChecker, + log: Log, + compilerOptions: CompilerOptions, + sourceFile: SourceFile, + position: number, + allSourceFiles: ReadonlyArray, + ): CompletionInfo | undefined { if (isInReferenceComment(sourceFile, position)) { return PathCompletions.getTripleSlashReferenceCompletion(sourceFile, position, compilerOptions, host); } @@ -19,12 +37,12 @@ namespace ts.Completions { return getStringLiteralCompletionEntries(sourceFile, position, typeChecker, compilerOptions, host, log); } - const completionData = getCompletionData(typeChecker, log, sourceFile, position); + const completionData = getCompletionData(typeChecker, log, sourceFile, position, allSourceFiles); if (!completionData) { return undefined; } - const { symbols, isGlobalCompletion, isMemberCompletion, allowStringLiteral, isNewIdentifierLocation, location, request, keywordFilters } = completionData; + const { symbols, isGlobalCompletion, isMemberCompletion, allowStringLiteral, isNewIdentifierLocation, location, request, keywordFilters, symbolToOriginInfoMap } = completionData; if (sourceFile.languageVariant === LanguageVariant.JSX && location && location.parent && location.parent.kind === SyntaxKind.JsxClosingElement) { @@ -56,7 +74,7 @@ namespace ts.Completions { const entries: CompletionEntry[] = []; if (isSourceFileJavaScript(sourceFile)) { - const uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true, typeChecker, compilerOptions.target, log, allowStringLiteral); + const uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true, typeChecker, compilerOptions.target, log, allowStringLiteral, symbolToOriginInfoMap); getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames, compilerOptions.target, entries); } else { @@ -64,7 +82,7 @@ namespace ts.Completions { return undefined; } - getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true, typeChecker, compilerOptions.target, log, allowStringLiteral); + getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true, typeChecker, compilerOptions.target, log, allowStringLiteral, symbolToOriginInfoMap); } // TODO add filter for keyword based on type/value/namespace and also location @@ -134,7 +152,17 @@ namespace ts.Completions { }; } - function getCompletionEntriesFromSymbols(symbols: Symbol[], entries: Push, location: Node, performCharacterChecks: boolean, typeChecker: TypeChecker, target: ScriptTarget, log: Log, allowStringLiteral: boolean): Map { + function getCompletionEntriesFromSymbols( + symbols: ReadonlyArray, + entries: Push, + location: Node, + performCharacterChecks: boolean, + typeChecker: TypeChecker, + target: ScriptTarget, + log: Log, + allowStringLiteral: boolean, + symbolToOriginInfoMap?: SymbolOriginInfoMap, + ): Map { const start = timestamp(); const uniqueNames = createMap(); if (symbols) { @@ -143,6 +171,9 @@ namespace ts.Completions { if (entry) { const id = entry.name; if (!uniqueNames.has(id)) { + if (symbolToOriginInfoMap && symbolToOriginInfoMap[getUniqueSymbolId(symbol, typeChecker)]) { + entry.hasAction = true; + } entries.push(entry); uniqueNames.set(id, true); } @@ -298,53 +329,89 @@ namespace ts.Completions { } } - export function getCompletionEntryDetails(typeChecker: TypeChecker, log: (message: string) => void, compilerOptions: CompilerOptions, sourceFile: SourceFile, position: number, entryName: string): CompletionEntryDetails { + export function getCompletionEntryDetails( + typeChecker: TypeChecker, + log: (message: string) => void, + compilerOptions: CompilerOptions, + sourceFile: SourceFile, + position: number, + name: string, + allSourceFiles: ReadonlyArray, + host: LanguageServiceHost, + rulesProvider: formatting.RulesProvider, + ): CompletionEntryDetails { + // Compute all the completion symbols again. - const completionData = getCompletionData(typeChecker, log, sourceFile, position); + const completionData = getCompletionData(typeChecker, log, sourceFile, position, allSourceFiles); if (completionData) { - const { symbols, location, allowStringLiteral } = completionData; + const { symbols, location, allowStringLiteral, symbolToOriginInfoMap } = completionData; // Find the symbol with the matching entry name. // We don't need to perform character checks here because we're only comparing the // name against 'entryName' (which is known to be good), not building a new // completion entry. - const symbol = forEach(symbols, s => getCompletionEntryDisplayNameForSymbol(s, compilerOptions.target, /*performCharacterChecks*/ false, allowStringLiteral) === entryName ? s : undefined); + const symbol = find(symbols, s => getCompletionEntryDisplayNameForSymbol(s, compilerOptions.target, /*performCharacterChecks*/ false, allowStringLiteral) === name); if (symbol) { + const codeActions = getCompletionEntryCodeActions(symbolToOriginInfoMap, symbol, typeChecker, host, compilerOptions, sourceFile, rulesProvider); + const kindModifiers = SymbolDisplay.getSymbolModifiers(symbol); const { displayParts, documentation, symbolKind, tags } = SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, location, location, SemanticMeaning.All); - return { - name: entryName, - kindModifiers: SymbolDisplay.getSymbolModifiers(symbol), - kind: symbolKind, - displayParts, - documentation, - tags - }; + return { name, kindModifiers, kind: symbolKind, displayParts, documentation, tags, codeActions }; } } // Didn't find a symbol with this name. See if we can find a keyword instead. const keywordCompletion = forEach( getKeywordCompletions(KeywordCompletionFilters.None), - c => c.name === entryName + c => c.name === name ); if (keywordCompletion) { return { - name: entryName, + name, kind: ScriptElementKind.keyword, kindModifiers: ScriptElementKindModifier.none, - displayParts: [displayPart(entryName, SymbolDisplayPartKind.keyword)], + displayParts: [displayPart(name, SymbolDisplayPartKind.keyword)], documentation: undefined, - tags: undefined + tags: undefined, + codeActions: undefined, }; } return undefined; } - export function getCompletionEntrySymbol(typeChecker: TypeChecker, log: (message: string) => void, compilerOptions: CompilerOptions, sourceFile: SourceFile, position: number, entryName: string): Symbol | undefined { + function getCompletionEntryCodeActions(symbolToOriginInfoMap: SymbolOriginInfoMap, symbol: Symbol, checker: TypeChecker, host: LanguageServiceHost, compilerOptions: CompilerOptions, sourceFile: SourceFile, rulesProvider: formatting.RulesProvider): CodeAction[] | undefined { + const symbolOriginInfo = symbolToOriginInfoMap[getUniqueSymbolId(symbol, checker)]; + if (!symbolOriginInfo) { + return undefined; + } + + const { moduleSymbol, isDefaultExport } = symbolOriginInfo; + return codefix.getCodeActionForImport(moduleSymbol, { + host, + checker, + newLineCharacter: host.getNewLine(), + compilerOptions, + sourceFile, + rulesProvider, + symbolName: symbol.name, + getCanonicalFileName: createGetCanonicalFileName(host.useCaseSensitiveFileNames ? host.useCaseSensitiveFileNames() : false), + symbolToken: undefined, + kind: isDefaultExport ? codefix.ImportKind.Default : codefix.ImportKind.Named, + }); + } + + export function getCompletionEntrySymbol( + typeChecker: TypeChecker, + log: (message: string) => void, + compilerOptions: CompilerOptions, + sourceFile: SourceFile, + position: number, + entryName: string, + allSourceFiles: ReadonlyArray, + ): Symbol | undefined { // Compute all the completion symbols again. - const completionData = getCompletionData(typeChecker, log, sourceFile, position); + const completionData = getCompletionData(typeChecker, log, sourceFile, position, allSourceFiles); if (!completionData) { return undefined; } @@ -353,7 +420,7 @@ namespace ts.Completions { // We don't need to perform character checks here because we're only comparing the // name against 'entryName' (which is known to be good), not building a new // completion entry. - return forEach(symbols, s => getCompletionEntryDisplayNameForSymbol(s, compilerOptions.target, /*performCharacterChecks*/ false, allowStringLiteral) === entryName ? s : undefined); + return find(symbols, s => getCompletionEntryDisplayNameForSymbol(s, compilerOptions.target, /*performCharacterChecks*/ false, allowStringLiteral) === entryName); } interface CompletionData { @@ -366,10 +433,17 @@ namespace ts.Completions { isRightOfDot: boolean; request?: Request; keywordFilters: KeywordCompletionFilters; + symbolToOriginInfoMap: SymbolOriginInfoMap; } type Request = { kind: "JsDocTagName" } | { kind: "JsDocTag" } | { kind: "JsDocParameterName", tag: JSDocParameterTag }; - function getCompletionData(typeChecker: TypeChecker, log: (message: string) => void, sourceFile: SourceFile, position: number): CompletionData | undefined { + function getCompletionData( + typeChecker: TypeChecker, + log: (message: string) => void, + sourceFile: SourceFile, + position: number, + allSourceFiles: ReadonlyArray, + ): CompletionData | undefined { const isJavaScriptFile = isSourceFileJavaScript(sourceFile); let request: Request | undefined; @@ -441,7 +515,18 @@ namespace ts.Completions { } if (request) { - return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, allowStringLiteral: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, request, keywordFilters: KeywordCompletionFilters.None }; + return { + symbols: undefined, + isGlobalCompletion: false, + isMemberCompletion: false, + allowStringLiteral: false, + isNewIdentifierLocation: false, + location: undefined, + isRightOfDot: false, + request, + keywordFilters: KeywordCompletionFilters.None, + symbolToOriginInfoMap: undefined, + }; } if (!insideJsDocTagTypeExpression) { @@ -543,6 +628,7 @@ namespace ts.Completions { let isNewIdentifierLocation: boolean; let keywordFilters = KeywordCompletionFilters.None; let symbols: Symbol[] = []; + const symbolToOriginInfoMap: SymbolOriginInfoMap = []; if (isRightOfDot) { getTypeScriptMemberSymbols(); @@ -579,7 +665,7 @@ namespace ts.Completions { log("getCompletionData: Semantic work: " + (timestamp() - semanticStart)); - return { symbols, isGlobalCompletion, isMemberCompletion, allowStringLiteral, isNewIdentifierLocation, location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), request, keywordFilters }; + return { symbols, isGlobalCompletion, isMemberCompletion, allowStringLiteral, isNewIdentifierLocation, location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), request, keywordFilters, symbolToOriginInfoMap }; type JSDocTagWithTypeExpression = JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag; @@ -752,13 +838,16 @@ namespace ts.Completions { } const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias; - symbols = filterGlobalCompletion(typeChecker.getSymbolsInScope(scopeNode, symbolMeanings)); + + symbols = typeChecker.getSymbolsInScope(scopeNode, symbolMeanings); + getSymbolsFromOtherSourceFileExports(symbols, previousToken && isIdentifier(previousToken) ? previousToken.text : ""); + filterGlobalCompletion(symbols); return true; } - function filterGlobalCompletion(symbols: Symbol[]) { - return filter(symbols, symbol => { + function filterGlobalCompletion(symbols: Symbol[]): void { + filterMutate(symbols, symbol => { if (!isSourceFile(location)) { // export = /**/ here we want to get all meanings, so any symbol is ok if (isExportAssignment(location.parent)) { @@ -832,6 +921,59 @@ namespace ts.Completions { } } + function getSymbolsFromOtherSourceFileExports(symbols: Symbol[], tokenText: string): void { + const tokenTextLowerCase = tokenText.toLowerCase(); + const symbolIdMap = arrayToNumericMap(symbols, s => getUniqueSymbolId(s, typeChecker)); + + codefix.forEachExternalModule(typeChecker, allSourceFiles, moduleSymbol => { + if (moduleSymbol === sourceFile.symbol) { + return; + } + + for (let symbol of typeChecker.getExportsOfModule(moduleSymbol)) { + let { name } = symbol; + const isDefaultExport = name === "default"; + if (isDefaultExport) { + const localSymbol = getLocalSymbolForExportDefault(symbol); + if (localSymbol) { + symbol = localSymbol; + name = localSymbol.name; + } + } + + const id = getUniqueSymbolId(symbol, typeChecker); + if (!symbolIdMap[id] && stringContainsCharactersInOrder(name.toLowerCase(), tokenTextLowerCase)) { + symbols.push(symbol); + symbolToOriginInfoMap[id] = { moduleSymbol, isDefaultExport }; + } + } + }); + } + + /** + * True if you could remove some characters in `a` to get `b`. + * E.g., true for "abcdef" and "bdf". + * But not true for "abcdef" and "dbf". + */ + function stringContainsCharactersInOrder(str: string, characters: string): boolean { + if (characters.length === 0) { + return true; + } + + let characterIndex = 0; + for (let strIndex = 0; strIndex < str.length; strIndex++) { + if (str.charCodeAt(strIndex) === characters.charCodeAt(characterIndex)) { + characterIndex++; + if (characterIndex === characters.length) { + return true; + } + } + } + + // Did not find all characters + return false; + } + /** * Finds the first node that "embraces" the position, so that one may * accurately aggregate locals from the closest containing scope. @@ -1627,7 +1769,7 @@ namespace ts.Completions { // First check of the displayName is not external module; if it is an external module, it is not valid entry if (symbol.flags & SymbolFlags.Namespace) { const firstCharCode = name.charCodeAt(0); - if (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote) { + if (isSingleOrDoubleQuote(firstCharCode)) { // If the symbol is external module, don't show it in the completion list // (i.e declare module "http" { const x; } | // <= request completion here, "http" should not be there) return undefined; diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index e3bf9deac8932..780b14db719f2 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -339,7 +339,7 @@ namespace ts.Completions.PathCompletions { } } else if (host.getDirectories) { - let typeRoots: string[]; + let typeRoots: ReadonlyArray; try { // Wrap in try catch because getEffectiveTypeRoots touches the filesystem typeRoots = getEffectiveTypeRoots(options, host); diff --git a/src/services/refactorProvider.ts b/src/services/refactorProvider.ts index e956a4121c791..b338882e6db68 100644 --- a/src/services/refactorProvider.ts +++ b/src/services/refactorProvider.ts @@ -14,13 +14,11 @@ namespace ts { getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined; } - export interface RefactorContext { + export interface RefactorContext extends textChanges.TextChangesContext { file: SourceFile; startPosition: number; endPosition?: number; program: Program; - newLineCharacter: string; - rulesProvider?: formatting.RulesProvider; cancellationToken?: CancellationToken; } diff --git a/src/services/services.ts b/src/services/services.ts index 6bdc96d8b4d65..0399ca1658e9d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1324,17 +1324,19 @@ namespace ts { function getCompletionsAtPosition(fileName: string, position: number): CompletionInfo { synchronizeHostData(); - return Completions.getCompletionsAtPosition(host, program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position); + return Completions.getCompletionsAtPosition(host, program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position, program.getSourceFiles()); } - function getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails { + function getCompletionEntryDetails(fileName: string, position: number, entryName: string, formattingOptions?: FormatCodeSettings): CompletionEntryDetails { synchronizeHostData(); - return Completions.getCompletionEntryDetails(program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position, entryName); + const ruleProvider = formattingOptions ? getRuleProvider(formattingOptions) : undefined; + return Completions.getCompletionEntryDetails( + program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position, entryName, program.getSourceFiles(), host, ruleProvider); } function getCompletionEntrySymbol(fileName: string, position: number, entryName: string): Symbol { synchronizeHostData(); - return Completions.getCompletionEntrySymbol(program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position, entryName); + return Completions.getCompletionEntrySymbol(program.getTypeChecker(), log, program.getCompilerOptions(), getValidSourceFile(fileName), position, entryName, program.getSourceFiles()); } function getQuickInfoAtPosition(fileName: string, position: number): QuickInfo { diff --git a/src/services/shims.ts b/src/services/shims.ts index 9d4baccc3c4e0..c9f5c4c2fb23c 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -141,7 +141,7 @@ namespace ts { getEncodedSemanticClassifications(fileName: string, start: number, length: number): string; getCompletionsAtPosition(fileName: string, position: number): string; - getCompletionEntryDetails(fileName: string, position: number, entryName: string): string; + getCompletionEntryDetails(fileName: string, position: number, entryName: string, options: string/*Services.FormatCodeOptions*/): string; getQuickInfoAtPosition(fileName: string, position: number): string; @@ -893,10 +893,13 @@ namespace ts { } /** Get a string based representation of a completion list entry details */ - public getCompletionEntryDetails(fileName: string, position: number, entryName: string) { + public getCompletionEntryDetails(fileName: string, position: number, entryName: string, options: string/*Services.FormatCodeOptions*/) { return this.forwardJSONCall( `getCompletionEntryDetails('${fileName}', ${position}, '${entryName}')`, - () => this.languageService.getCompletionEntryDetails(fileName, position, entryName) + () => { + const localOptions: ts.FormatCodeOptions = JSON.parse(options); + return this.languageService.getCompletionEntryDetails(fileName, position, entryName, localOptions); + } ); } diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 9e4c2ed3a6003..63cfa64ef07b9 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -186,14 +186,25 @@ namespace ts.textChanges { return s; } + export interface TextChangesContext { + newLineCharacter: string; + rulesProvider: formatting.RulesProvider; + } + export class ChangeTracker { private changes: Change[] = []; private readonly newLineCharacter: string; - public static fromContext(context: RefactorContext | CodeFixContext) { + public static fromContext(context: TextChangesContext): ChangeTracker { return new ChangeTracker(context.newLineCharacter === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed, context.rulesProvider); } + public static with(context: TextChangesContext, cb: (tracker: ChangeTracker) => void): FileTextChanges[] { + const tracker = ChangeTracker.fromContext(context); + cb(tracker); + return tracker.getChanges(); + } + constructor( private readonly newLine: NewLineKind, private readonly rulesProvider: formatting.RulesProvider, diff --git a/src/services/types.ts b/src/services/types.ts index e853eb7b96cad..7bf4a909a7a34 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -230,7 +230,8 @@ namespace ts { getEncodedSemanticClassifications(fileName: string, span: TextSpan): Classifications; getCompletionsAtPosition(fileName: string, position: number): CompletionInfo; - getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails; + // "options" is optional only for backwards-compatibility + getCompletionEntryDetails(fileName: string, position: number, entryName: string, options?: FormatCodeOptions | FormatCodeSettings): CompletionEntryDetails; getCompletionEntrySymbol(fileName: string, position: number, entryName: string): Symbol; getQuickInfoAtPosition(fileName: string, position: number): QuickInfo; @@ -668,6 +669,7 @@ namespace ts { * be used in that case */ replacementSpan?: TextSpan; + hasAction?: true; } export interface CompletionEntryDetails { @@ -677,6 +679,7 @@ namespace ts { displayParts: SymbolDisplayPart[]; documentation: SymbolDisplayPart[]; tags: JSDocTagInfo[]; + codeActions?: CodeAction[]; } export interface OutliningSpan { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index df3f3c5c6b489..65a882424de35 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1278,9 +1278,7 @@ namespace ts { */ export function stripQuotes(name: string) { const length = name.length; - if (length >= 2 && - name.charCodeAt(0) === name.charCodeAt(length - 1) && - (name.charCodeAt(0) === CharacterCodes.doubleQuote || name.charCodeAt(0) === CharacterCodes.singleQuote)) { + if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && isSingleOrDoubleQuote(name.charCodeAt(0))) { return name.substring(1, length - 1); } return name; @@ -1297,6 +1295,10 @@ namespace ts { return ensureScriptKind(fileName, host && host.getScriptKind && host.getScriptKind(fileName)); } + export function getUniqueSymbolId(symbol: Symbol, checker: TypeChecker) { + return getSymbolId(skipAlias(symbol, checker)); + } + export function getFirstNonSpaceCharacterPosition(text: string, position: number) { while (isWhiteSpaceLike(text.charCodeAt(position))) { position += 1; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 151c948602de4..c1c56430293be 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -3207,10 +3207,11 @@ declare namespace ts { }; } declare namespace ts { - function getEffectiveTypeRoots(options: CompilerOptions, host: { - directoryExists?: (directoryName: string) => boolean; - getCurrentDirectory?: () => string; - }): string[] | undefined; + interface GetEffectiveTypeRootsHost { + directoryExists?(directoryName: string): boolean; + getCurrentDirectory?(): string; + } + function getEffectiveTypeRoots(options: CompilerOptions, host: GetEffectiveTypeRootsHost): string[] | undefined; /** * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups @@ -3913,7 +3914,7 @@ declare namespace ts { getEncodedSyntacticClassifications(fileName: string, span: TextSpan): Classifications; getEncodedSemanticClassifications(fileName: string, span: TextSpan): Classifications; getCompletionsAtPosition(fileName: string, position: number): CompletionInfo; - getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails; + getCompletionEntryDetails(fileName: string, position: number, entryName: string, options?: FormatCodeOptions | FormatCodeSettings): CompletionEntryDetails; getCompletionEntrySymbol(fileName: string, position: number, entryName: string): Symbol; getQuickInfoAtPosition(fileName: string, position: number): QuickInfo; getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): TextSpan; @@ -4281,6 +4282,7 @@ declare namespace ts { * be used in that case */ replacementSpan?: TextSpan; + hasAction?: true; } interface CompletionEntryDetails { name: string; @@ -4289,6 +4291,7 @@ declare namespace ts { displayParts: SymbolDisplayPart[]; documentation: SymbolDisplayPart[]; tags: JSDocTagInfo[]; + codeActions?: CodeAction[]; } interface OutliningSpan { /** The span of the document to actually collapse. */ @@ -6038,6 +6041,11 @@ declare namespace ts.server.protocol { * this span should be used instead of the default one. */ replacementSpan?: TextSpan; + /** + * Indicates whether commiting this completion entry will require additional code actions to be + * made to avoid errors. The CompletionEntryDetails will have these actions. + */ + hasAction?: true; } /** * Additional completion entry details, available on demand @@ -6067,6 +6075,10 @@ declare namespace ts.server.protocol { * JSDoc tags for the symbol. */ tags: JSDocTagInfo[]; + /** + * The associated code actions for this entry + */ + codeActions?: CodeAction[]; } interface CompletionsResponse extends Response { body?: CompletionEntry[]; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 14fae7d0d77a0..2668a25bcfcd2 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3154,10 +3154,11 @@ declare namespace ts { function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; } declare namespace ts { - function getEffectiveTypeRoots(options: CompilerOptions, host: { - directoryExists?: (directoryName: string) => boolean; - getCurrentDirectory?: () => string; - }): string[] | undefined; + interface GetEffectiveTypeRootsHost { + directoryExists?(directoryName: string): boolean; + getCurrentDirectory?(): string; + } + function getEffectiveTypeRoots(options: CompilerOptions, host: GetEffectiveTypeRootsHost): string[] | undefined; /** * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups @@ -3913,7 +3914,7 @@ declare namespace ts { getEncodedSyntacticClassifications(fileName: string, span: TextSpan): Classifications; getEncodedSemanticClassifications(fileName: string, span: TextSpan): Classifications; getCompletionsAtPosition(fileName: string, position: number): CompletionInfo; - getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails; + getCompletionEntryDetails(fileName: string, position: number, entryName: string, options?: FormatCodeOptions | FormatCodeSettings): CompletionEntryDetails; getCompletionEntrySymbol(fileName: string, position: number, entryName: string): Symbol; getQuickInfoAtPosition(fileName: string, position: number): QuickInfo; getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): TextSpan; @@ -4281,6 +4282,7 @@ declare namespace ts { * be used in that case */ replacementSpan?: TextSpan; + hasAction?: true; } interface CompletionEntryDetails { name: string; @@ -4289,6 +4291,7 @@ declare namespace ts { displayParts: SymbolDisplayPart[]; documentation: SymbolDisplayPart[]; tags: JSDocTagInfo[]; + codeActions?: CodeAction[]; } interface OutliningSpan { /** The span of the document to actually collapse. */ diff --git a/tests/cases/fourslash/completionsImport_default_addToNamedImports.ts b/tests/cases/fourslash/completionsImport_default_addToNamedImports.ts new file mode 100644 index 0000000000000..5662b57fdf678 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_default_addToNamedImports.ts @@ -0,0 +1,19 @@ +/// + +// @Filename: /a.ts +////export default function foo() {} +////export const x = 0; + +// @Filename: /b.ts +////import { x } from "./a"; +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Add 'foo' to existing import declaration from "./a".`, + newFileContent: `import foo, { x } from "./a"; +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_default_addToNamespaceImport.ts b/tests/cases/fourslash/completionsImport_default_addToNamespaceImport.ts new file mode 100644 index 0000000000000..b442ed21549e1 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_default_addToNamespaceImport.ts @@ -0,0 +1,18 @@ +/// + +// @Filename: /a.ts +////export default function foo() {} + +// @Filename: /b.ts +////import * as a from "./a"; +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Add 'foo' to existing import declaration from "./a".`, + newFileContent: `import foo, * as a from "./a"; +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_default_alreadyExistedWithRename.ts b/tests/cases/fourslash/completionsImport_default_alreadyExistedWithRename.ts new file mode 100644 index 0000000000000..3ee4decb0f8b7 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_default_alreadyExistedWithRename.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: /a.ts +////export default function foo() {} + +// @Filename: /b.ts +////import f_o_o from "./a"; +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Import 'foo' from "./a".`, + // TODO: GH#18445 + newFileContent: `import f_o_o from "./a"; +import foo from "./a";\r +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_default_didNotExistBefore.ts b/tests/cases/fourslash/completionsImport_default_didNotExistBefore.ts new file mode 100644 index 0000000000000..6dbd437d36470 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_default_didNotExistBefore.ts @@ -0,0 +1,19 @@ +/// + +// @Filename: /a.ts +////export default function foo() {} + +// @Filename: /b.ts +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Import 'foo' from "./a".`, + // TODO: GH#18445 + newFileContent: `import foo from "./a";\r +\r +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_fromAmbientModule.ts b/tests/cases/fourslash/completionsImport_fromAmbientModule.ts new file mode 100644 index 0000000000000..b45ad9824865a --- /dev/null +++ b/tests/cases/fourslash/completionsImport_fromAmbientModule.ts @@ -0,0 +1,18 @@ +/// + +// @Filename: /a.ts +////declare module "m" { +//// export const x: number; +////} + +// @Filename: /b.ts +/////**/ + +verify.applyCodeActionFromCompletion("", { + name: "x", + description: `Import 'x' from "m".`, + // TODO: GH#18445 + newFileContent: `import { x } from "m";\r +\r +`, +}); diff --git a/tests/cases/fourslash/completionsImport_matching.ts b/tests/cases/fourslash/completionsImport_matching.ts new file mode 100644 index 0000000000000..3470d59bff20b --- /dev/null +++ b/tests/cases/fourslash/completionsImport_matching.ts @@ -0,0 +1,22 @@ +/// + +// @Filename: /a.ts +// Not included: +////export function abcde() {} +////export function dbf() {} +// Included: +////export function bdf() {} +////export function abcdef() {} +////export function BDF() {} + +// @Filename: /b.ts +////bdf/**/ + +goTo.marker(""); + +verify.not.completionListContains("abcde"); +verify.not.completionListContains("dbf"); + +verify.completionListContains("bdf", "function bdf(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); +verify.completionListContains("abcdef", "function abcdef(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); +verify.completionListContains("BDF", "function BDF(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); diff --git a/tests/cases/fourslash/completionsImport_named_addToNamedImports.ts b/tests/cases/fourslash/completionsImport_named_addToNamedImports.ts new file mode 100644 index 0000000000000..23119ad491f79 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_named_addToNamedImports.ts @@ -0,0 +1,19 @@ +/// + +// @Filename: /a.ts +////export function foo() {} +////export const x = 0; + +// @Filename: /b.ts +////import { x } from "./a"; +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Add 'foo' to existing import declaration from "./a".`, + newFileContent: `import { x, foo } from "./a"; +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_named_didNotExistBefore.ts b/tests/cases/fourslash/completionsImport_named_didNotExistBefore.ts new file mode 100644 index 0000000000000..95c68c2a05e88 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_named_didNotExistBefore.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: /a.ts +////export function Test1() {} +////export function Test2() {} + +// @Filename: /b.ts +////import { Test2 } from "./a"; +////t/**/ + +goTo.marker(""); +verify.completionListContains("Test1", "function Test1(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); +verify.completionListContains("Test2", "import Test2", "", "alias", /*spanIndex*/ undefined, /*hasAction*/ undefined); + +verify.applyCodeActionFromCompletion("", { + name: "Test1", + description: `Add 'Test1' to existing import declaration from "./a".`, + newFileContent: `import { Test2, Test1 } from "./a"; +t`, +}); diff --git a/tests/cases/fourslash/completionsImport_named_namespaceImportExists.ts b/tests/cases/fourslash/completionsImport_named_namespaceImportExists.ts new file mode 100644 index 0000000000000..da00907e60fb7 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_named_namespaceImportExists.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: /a.ts +////export function foo() {} + +// @Filename: /b.ts +////import * as a from "./a"; +////f/**/; + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + description: `Import 'foo' from "./a".`, + // TODO: GH#18445 + newFileContent: `import * as a from "./a"; +import { foo } from "./a";\r +f;`, +}); diff --git a/tests/cases/fourslash/completionsImport_previousTokenIsSemicolon.ts b/tests/cases/fourslash/completionsImport_previousTokenIsSemicolon.ts new file mode 100644 index 0000000000000..904de35354ac5 --- /dev/null +++ b/tests/cases/fourslash/completionsImport_previousTokenIsSemicolon.ts @@ -0,0 +1,11 @@ +/// + +// @Filename: /a.ts +////export function foo() {} + +// @Filename: /b.ts +////import * as a from 'a'; +/////**/ + +goTo.marker(""); +verify.completionListContains("foo", "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index f4d47abc9c9c4..e1d9607de8aaf 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -140,7 +140,14 @@ declare namespace FourSlashInterface { allowedConstructorParameterKeywords: string[]; constructor(negative?: boolean); completionListCount(expectedCount: number): void; - completionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number): void; + completionListContains( + symbol: string, + text?: string, + documentation?: string, + kind?: string, + spanIndex?: number, + hasAction?: boolean, + ): void; completionListItemsCountIsGreaterThan(count: number): void; completionListIsEmpty(): void; completionListContainsClassElementKeywords(): void; @@ -173,6 +180,20 @@ declare namespace FourSlashInterface { assertHasRanges(ranges: Range[]): void; caretAtMarker(markerName?: string): void; completionsAt(markerName: string, completions: string[], options?: { isNewIdentifierLocation?: boolean }): void; + completionsAndDetailsAt( + markerName: string, + completions: { + excludes?: ReadonlyArray, + //TODO: better type + entries: ReadonlyArray<{ entry: any, details: any }>, + }, + ): void; //TODO: better type + applyCodeActionFromCompletion(markerName: string, options: { + name: string, + description: string, + newFileContent?: string, + newRangeContent?: string, + }); indentationIs(numberOfSpaces: number): void; indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle?: ts.IndentStyle, baseIndentSize?: number): void; textAtCaretIs(text: string): void; diff --git a/tests/cases/fourslash/importNameCodeFixOptionalImport0.ts b/tests/cases/fourslash/importNameCodeFixOptionalImport0.ts index 34cebad941544..218ec4fabb724 100644 --- a/tests/cases/fourslash/importNameCodeFixOptionalImport0.ts +++ b/tests/cases/fourslash/importNameCodeFixOptionalImport0.ts @@ -8,7 +8,7 @@ //// export function foo() {}; // @Filename: a/foo.ts -//// export { foo } from "./foo/bar"; +//// export { foo } from "./foo/bar"; verify.importFixAtPosition([ `import * as ns from "./foo";