Skip to content

fix(59240): <semantic> TS Server Error Debug Failure when moving to a js file #59447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion src/services/refactors/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isClassLike,
isPrivateIdentifier,
isPropertyAccessExpression,
isSourceFile,
ModuleBlock,
Node,
Program,
Expand Down Expand Up @@ -76,7 +77,7 @@ export function addTargetFileImports(
* So in that case, fall back to copying the import verbatim.
*/
importsToCopy.forEach(([isValidTypeOnlyUseSite, declaration], symbol) => {
const targetSymbol = skipAlias(symbol, checker);
const targetSymbol = resolveTargetSymbol(checker, symbol);
if (checker.isUnknownSymbol(targetSymbol)) {
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
}
Expand All @@ -87,3 +88,11 @@ export function addTargetFileImports(

addImportsForMovedSymbols(targetFileImportsFromOldFile, oldFile.fileName, importAdder, program);
}

function resolveTargetSymbol(checker: TypeChecker, symbol: Symbol) {
if (symbol.flags & SymbolFlags.Alias) {
const targetSymbol = skipAlias(symbol, checker);
return targetSymbol.declarations?.some(isSourceFile) && targetSymbol.exports?.has(symbol.escapedName) ? targetSymbol.exports.get(symbol.escapedName) as Symbol : targetSymbol;
}
return symbol;
}
9 changes: 8 additions & 1 deletion src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
hasTSFileExtension,
hostGetCanonicalFileName,
Identifier,
idText,
ImportDeclaration,
ImportEqualsDeclaration,
importFromModuleSpecifier,
Expand Down Expand Up @@ -105,6 +106,7 @@ import {
isStatement,
isStringLiteral,
isStringLiteralLike,
isTransientSymbol,
isValidTypeOnlyAliasUseSite,
isVariableDeclaration,
isVariableDeclarationInitializedToRequire,
Expand Down Expand Up @@ -967,7 +969,7 @@ function forEachReference(node: Node, checker: TypeChecker, enclosingRange: Text
if (enclosingRange && !rangeContainsRange(enclosingRange, node)) {
return;
}
const sym = checker.getSymbolAtLocation(node);
const sym = resolveSymbol(checker, node);
if (sym) onReference(sym, isValidTypeOnlyAliasUseSite(node));
}
else {
Expand All @@ -976,6 +978,11 @@ function forEachReference(node: Node, checker: TypeChecker, enclosingRange: Text
});
}

function resolveSymbol(checker: TypeChecker, node: Identifier) {
const symbol = checker.getSymbolAtLocation(node);
return symbol === undefined || isTransientSymbol(symbol) ? checker.resolveName(idText(node), node, SymbolFlags.All, /*excludeGlobals*/ false) : symbol;
}

function forEachTopLevelDeclaration<T>(statement: Statement, cb: (node: TopLevelDeclaration) => T): T | undefined {
switch (statement.kind) {
case SyntaxKind.FunctionDeclaration:
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToNewFile_namespaceExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @module: esnext

// @filename: /a.ts
////export interface A {}

// @filename: /b.ts
////export * as A from "./a";
////export type B = string

// @filename: /c.ts
////import { A } from "./b"
////[|type B = A.B|]

verify.moveToNewFile({
newFileContents: {
"/c.ts": '',
"/B.1.ts":
`import { A } from "./a";

type B = A.B;
`,
},
});