mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add a code fixer for --isolatedDeclarations errors (#58260)
Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
This commit is contained in:
co-authored by
Andrew Branch
parent
749bd834be
commit
33b156147b
@@ -50,6 +50,7 @@ export * from "../codefixes/fixUnreachableCode";
|
||||
export * from "../codefixes/fixUnusedLabel";
|
||||
export * from "../codefixes/fixJSDocTypes";
|
||||
export * from "../codefixes/fixMissingCallParentheses";
|
||||
export * from "../codefixes/fixMissingTypeAnnotationOnExports";
|
||||
export * from "../codefixes/fixAwaitInSyncFunction";
|
||||
export * from "../codefixes/fixPropertyOverrideAccessor";
|
||||
export * from "../codefixes/inferFromUsage";
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
DiagnosticWithLocation,
|
||||
FileTextChanges,
|
||||
flatMap,
|
||||
getEmitDeclarations,
|
||||
isString,
|
||||
map,
|
||||
TextChange,
|
||||
@@ -124,9 +125,15 @@ export function eachDiagnostic(context: CodeFixAllContext, errorCodes: readonly
|
||||
}
|
||||
|
||||
function getDiagnostics({ program, sourceFile, cancellationToken }: CodeFixContextBase) {
|
||||
return [
|
||||
const diagnostics = [
|
||||
...program.getSemanticDiagnostics(sourceFile, cancellationToken),
|
||||
...program.getSyntacticDiagnostics(sourceFile, cancellationToken),
|
||||
...computeSuggestionDiagnostics(sourceFile, program, cancellationToken),
|
||||
];
|
||||
if (getEmitDeclarations(program.getCompilerOptions())) {
|
||||
diagnostics.push(
|
||||
...program.getDeclarationDiagnostics(sourceFile, cancellationToken),
|
||||
);
|
||||
}
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -61,7 +61,6 @@ import {
|
||||
hasEffectiveModifier,
|
||||
hasSyntacticModifier,
|
||||
Identifier,
|
||||
identifierToKeywordKind,
|
||||
isArray,
|
||||
isArrowFunction,
|
||||
isAssignmentExpression,
|
||||
@@ -92,7 +91,6 @@ import {
|
||||
isModuleBlock,
|
||||
isParenthesizedTypeNode,
|
||||
isPartOfTypeNode,
|
||||
isPrivateIdentifier,
|
||||
isPropertyAccessExpression,
|
||||
isPropertyDeclaration,
|
||||
isQualifiedName,
|
||||
@@ -161,6 +159,7 @@ import {
|
||||
VisitResult,
|
||||
} from "../_namespaces/ts";
|
||||
import {
|
||||
getIdentifierForNode,
|
||||
refactorKindBeginsWith,
|
||||
registerRefactor,
|
||||
} from "../_namespaces/ts.refactor";
|
||||
@@ -1374,9 +1373,7 @@ function extractConstantInScope(
|
||||
|
||||
// Make a unique name for the extracted variable
|
||||
const file = scope.getSourceFile();
|
||||
const localNameText = isPropertyAccessExpression(node) && !isClassLike(scope) && !checker.resolveName(node.name.text, node, SymbolFlags.Value, /*excludeGlobals*/ false) && !isPrivateIdentifier(node.name) && !identifierToKeywordKind(node.name)
|
||||
? node.name.text
|
||||
: getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file);
|
||||
const localNameText = getIdentifierForNode(node, scope, checker, file);
|
||||
const isJS = isInJSFile(scope);
|
||||
|
||||
let variableType = isJS || !checker.isContextSensitive(node)
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
import {
|
||||
ClassLikeDeclaration,
|
||||
codefix,
|
||||
Debug,
|
||||
findAncestor,
|
||||
FunctionLikeDeclaration,
|
||||
getUniqueName,
|
||||
identifierToKeywordKind,
|
||||
isAnyImportOrRequireStatement,
|
||||
isClassLike,
|
||||
isPrivateIdentifier,
|
||||
isPropertyAccessExpression,
|
||||
ModuleBlock,
|
||||
Node,
|
||||
Program,
|
||||
skipAlias,
|
||||
SourceFile,
|
||||
Symbol,
|
||||
SymbolFlags,
|
||||
TypeChecker,
|
||||
} from "../_namespaces/ts";
|
||||
import { addImportsForMovedSymbols } from "./moveToFile";
|
||||
@@ -39,6 +49,18 @@ export function refactorKindBeginsWith(known: string, requested: string | undefi
|
||||
return known.substr(0, requested.length) === requested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to come up with a unique name for a given node within the scope for the
|
||||
* use of being used as a property/variable name.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export function getIdentifierForNode(node: Node, scope: FunctionLikeDeclaration | SourceFile | ModuleBlock | ClassLikeDeclaration, checker: TypeChecker, file: SourceFile) {
|
||||
return isPropertyAccessExpression(node) && !isClassLike(scope) && !checker.resolveName(node.name.text, node, SymbolFlags.Value, /*excludeGlobals*/ false) && !isPrivateIdentifier(node.name) && !identifierToKeywordKind(node.name)
|
||||
? node.name.text
|
||||
: getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function addTargetFileImports(
|
||||
oldFile: SourceFile,
|
||||
|
||||
Reference in New Issue
Block a user