Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1869,12 +1869,7 @@ namespace ts {
}

function checkSymbolUsageInExpressionContext(symbol: Symbol, name: __String, useSite: Node) {
if (
!(useSite.flags & NodeFlags.Ambient) &&
!isPartOfTypeQuery(useSite) &&
!isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) &&
isExpressionNode(useSite)
) {
if (!isValidTypeOnlyAliasUseSite(useSite)) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
if (typeOnlyDeclaration) {
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6134,4 +6134,11 @@ namespace ts {
export function pseudoBigIntToString({negative, base10Value}: PseudoBigInt): string {
return (negative && base10Value !== "0" ? "-" : "") + base10Value;
}

export function isValidTypeOnlyAliasUseSite(useSite: Node): boolean {
return !!(useSite.flags & NodeFlags.Ambient)
|| isPartOfTypeQuery(useSite)
|| isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite)
|| !isExpressionNode(useSite);
}
}
91 changes: 38 additions & 53 deletions src/services/codefixes/importFixes.ts

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ namespace ts.Completions {
symbolToOriginInfoMap: SymbolOriginInfoMap;
previousToken: Node | undefined;
readonly isJsxInitializer: IsJsxInitializer;
readonly isTypeOnlyLocation: boolean;
}
function getSymbolCompletionFromEntryId(
program: Program,
Expand All @@ -543,7 +544,7 @@ namespace ts.Completions {
return { type: "request", request: completionData };
}

const { symbols, literals, location, completionKind, symbolToOriginInfoMap, previousToken, isJsxInitializer } = completionData;
const { symbols, literals, location, completionKind, symbolToOriginInfoMap, previousToken, isJsxInitializer, isTypeOnlyLocation } = completionData;

const literal = find(literals, l => completionNameForLiteral(l) === entryId.name);
if (literal !== undefined) return { type: "literal", literal };
Expand All @@ -556,7 +557,7 @@ namespace ts.Completions {
const origin = symbolToOriginInfoMap[getSymbolId(symbol)];
const info = getCompletionEntryDisplayNameForSymbol(symbol, compilerOptions.target!, origin, completionKind);
return info && info.name === entryId.name && getSourceFromOrigin(origin) === entryId.source
? { type: "symbol" as const, symbol, location, symbolToOriginInfoMap, previousToken, isJsxInitializer }
? { type: "symbol" as const, symbol, location, symbolToOriginInfoMap, previousToken, isJsxInitializer, isTypeOnlyLocation }
: undefined;
}) || { type: "none" };
}
Expand Down Expand Up @@ -714,6 +715,7 @@ namespace ts.Completions {
readonly isJsxInitializer: IsJsxInitializer;
readonly insideJsDocTagTypeExpression: boolean;
readonly symbolToSortTextMap: SymbolSortTextMap;
readonly isTypeOnlyLocation: boolean;
}
type Request = { readonly kind: CompletionDataKind.JsDocTagName | CompletionDataKind.JsDocTag } | { readonly kind: CompletionDataKind.JsDocParameterName, tag: JSDocParameterTag };

Expand Down Expand Up @@ -1012,6 +1014,7 @@ namespace ts.Completions {
const symbolToOriginInfoMap: SymbolOriginInfoMap = [];
const symbolToSortTextMap: SymbolSortTextMap = [];
const importSuggestionsCache = host.getImportSuggestionsCache && host.getImportSuggestionsCache();
const isTypeOnly = isTypeOnlyCompletion();

if (isRightOfDot || isRightOfQuestionDot) {
getTypeScriptMemberSymbols();
Expand Down Expand Up @@ -1061,7 +1064,8 @@ namespace ts.Completions {
previousToken,
isJsxInitializer,
insideJsDocTagTypeExpression,
symbolToSortTextMap
symbolToSortTextMap,
isTypeOnlyLocation: isTypeOnly
};

type JSDocTagWithTypeExpression = JSDocParameterTag | JSDocPropertyTag | JSDocReturnTag | JSDocTypeTag | JSDocTypedefTag;
Expand Down Expand Up @@ -1329,7 +1333,6 @@ namespace ts.Completions {
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
isInSnippetScope = isSnippetScope(scopeNode);

const isTypeOnly = isTypeOnlyCompletion();
const symbolMeanings = (isTypeOnly ? SymbolFlags.None : SymbolFlags.Value) | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias;

symbols = Debug.assertEachDefined(typeChecker.getSymbolsInScope(scopeNode, symbolMeanings), "getSymbolsInScope() should all be defined");
Expand Down
9 changes: 9 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,10 @@ namespace ts {
return contains(typeKeywords, kind);
}

export function isTypeKeywordToken(node: Node): node is Token<SyntaxKind.TypeKeyword> {
return node.kind === SyntaxKind.TypeKeyword;
}

/** True if the symbol is for an external module, as opposed to a namespace. */
export function isExternalModuleSymbol(moduleSymbol: Symbol): boolean {
return !!(moduleSymbol.flags & SymbolFlags.Module) && moduleSymbol.name.charCodeAt(0) === CharacterCodes.doubleQuote;
Expand Down Expand Up @@ -1494,6 +1498,11 @@ namespace ts {
}
}

export function getTypeKeywordOfTypeOnlyImport(importClause: ImportClause, sourceFile: SourceFile): Token<SyntaxKind.TypeKeyword> {
Debug.assert(importClause.isTypeOnly);
return cast(importClause.getChildAt(0, sourceFile), isTypeKeywordToken);
}

export function textSpansEqual(a: TextSpan | undefined, b: TextSpan | undefined): boolean {
return !!a && !!b && a.start === b.start && a.length === b.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ goTo.file("/b.ts");
verify.codeFix({
description: "Infer parameter types from usage",
newFileContent:
`import type { User } from "./a";
`import { User } from "./a";

export function f(user: User) {
getEmail(user);
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/completionsImport_exportEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ verify.applyCodeActionFromCompletion("1", {
source: "/a",
description: `Import 'b' from module "./a"`,
newFileContent:
`import type { b } from "./a";
`import { b } from "./a";

a;
let x: b;`,
Expand All @@ -54,7 +54,7 @@ verify.applyCodeActionFromCompletion("0", {
source: "/a",
description: `Import 'a' from module "./a"`,
newFileContent:
`import type { b } from "./a";
`import { b } from "./a";
import a = require("./a");

a;
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/completionsImport_typeOnly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />

// @target: esnext

// @Filename: /a.ts
//// export class A {}
//// export class B {}

// @Filename: /b.ts
//// import type { A } from './a';
//// const b: B/**/

goTo.file('/b.ts');
verify.applyCodeActionFromCompletion('', {
name: 'B',
source: '/a',
description: `Add 'B' to existing import declaration from "./a"`,
preferences: {
includeCompletionsForModuleExports: true,
includeInsertTextCompletions: true
},
newFileContent: `import type { A, B } from './a';
const b: B`
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/importNameCodeFixConvertTypeOnly1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.ts
//// export class A {}
//// export class B {}

// @Filename: /b.ts
//// import type { A } from './a';
//// new B

goTo.file('/b.ts');
verify.importFixAtPosition([`import { A, B } from './a';
new B`]);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/importNameCodeFixNewImportFile3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//// }

verify.importFixAtPosition([
`import type { XXX } from "./module";
`import { XXX } from "./module";

let t: XXX.I;`
]);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/importNameCodeFixNewImportFile4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//// }

verify.importFixAtPosition([
`import type { A } from "./module";
`import { A } from "./module";

let t: A.B.I;`
]);
14 changes: 0 additions & 14 deletions tests/cases/fourslash/importNameCodeFixNewImportTypeOnly.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tests/cases/fourslash/importNameCodeFix_exportEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ verify.codeFixAll({
fixId: "fixMissingImport",
fixAllDescription: "Add all missing imports",
newFileContent:
`import a = require("./a");
`import { b } from "./a";

import type { b } from "./a";
import a = require("./a");

a;
let x: b;`,
Expand Down