mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
Don't copy trivia when implementing an interface (#23343)
* Don't copy trivia when implementing an interface * Use an `includeTrivia` flag instead of a separate function
This commit is contained in:
parent
05c746b547
commit
d2dc2e6d48
@ -25,8 +25,7 @@ namespace ts.codefix {
|
||||
}
|
||||
|
||||
const declaration = declarations[0];
|
||||
// Clone name to remove leading trivia.
|
||||
const name = getSynthesizedDeepClone(getNameOfDeclaration(declaration)) as PropertyName;
|
||||
const name = getSynthesizedDeepClone(getNameOfDeclaration(declaration), /*includeTrivia*/ false) as PropertyName;
|
||||
const visibilityModifier = createVisibilityModifier(getModifierFlags(declaration));
|
||||
const modifiers = visibilityModifier ? createNodeArray([visibilityModifier]) : undefined;
|
||||
const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration));
|
||||
@ -69,7 +68,7 @@ namespace ts.codefix {
|
||||
|
||||
for (const signature of signatures) {
|
||||
// Need to ensure nodes are fresh each time so they can have different positions.
|
||||
outputMethod(signature, getSynthesizedDeepClones(modifiers), getSynthesizedDeepClone(name));
|
||||
outputMethod(signature, getSynthesizedDeepClones(modifiers, /*includeTrivia*/ false), getSynthesizedDeepClone(name, /*includeTrivia*/ false));
|
||||
}
|
||||
|
||||
if (declarations.length > signatures.length) {
|
||||
@ -103,10 +102,6 @@ namespace ts.codefix {
|
||||
return signatureDeclaration;
|
||||
}
|
||||
|
||||
function getSynthesizedDeepClones<T extends Node>(nodes: NodeArray<T> | undefined): NodeArray<T> | undefined {
|
||||
return nodes && createNodeArray(nodes.map(getSynthesizedDeepClone));
|
||||
}
|
||||
|
||||
export function createMethodFromCallExpression(
|
||||
{ typeArguments, arguments: args }: CallExpression,
|
||||
methodName: string,
|
||||
|
||||
@ -1459,11 +1459,13 @@ namespace ts {
|
||||
* WARNING: This is an expensive operation and is only intended to be used in refactorings
|
||||
* and code fixes (because those are triggered by explicit user actions).
|
||||
*/
|
||||
export function getSynthesizedDeepClone<T extends Node>(node: T | undefined): T | undefined {
|
||||
if (node === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
export function getSynthesizedDeepClone<T extends Node>(node: T | undefined, includeTrivia = true): T | undefined {
|
||||
const clone = node && getSynthesizedDeepCloneWorker(node);
|
||||
if (clone && !includeTrivia) suppressLeadingAndTrailingTrivia(clone);
|
||||
return clone;
|
||||
}
|
||||
|
||||
function getSynthesizedDeepCloneWorker<T extends Node>(node: T): T | undefined {
|
||||
const visited = visitEachChild(node, getSynthesizedDeepClone, nullTransformationContext);
|
||||
if (visited === node) {
|
||||
// This only happens for leaf nodes - internal nodes always see their children change.
|
||||
@ -1474,22 +1476,18 @@ namespace ts {
|
||||
else if (isNumericLiteral(clone)) {
|
||||
clone.numericLiteralFlags = (node as any).numericLiteralFlags;
|
||||
}
|
||||
clone.pos = node.pos;
|
||||
clone.end = node.end;
|
||||
return clone;
|
||||
return setTextRange(clone, node);
|
||||
}
|
||||
|
||||
// PERF: As an optimization, rather than calling getSynthesizedClone, we'll update
|
||||
// the new node created by visitEachChild with the extra changes getSynthesizedClone
|
||||
// would have made.
|
||||
|
||||
visited.parent = undefined;
|
||||
|
||||
return visited;
|
||||
}
|
||||
|
||||
export function getSynthesizedDeepClones<T extends Node>(nodes: NodeArray<T> | undefined): NodeArray<T> | undefined {
|
||||
return nodes && createNodeArray(nodes.map(getSynthesizedDeepClone), nodes.hasTrailingComma);
|
||||
export function getSynthesizedDeepClones<T extends Node>(nodes: NodeArray<T> | undefined, includeTrivia = true): NodeArray<T> | undefined {
|
||||
return nodes && createNodeArray(nodes.map(n => getSynthesizedDeepClone(n, includeTrivia)), nodes.hasTrailingComma);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -36,9 +36,9 @@ verify.codeFix({
|
||||
/**close-brace prefix*/ }
|
||||
/**close-brace prefix*/ }
|
||||
class C implements N.I {
|
||||
/** property prefix */ a /** colon prefix */: N.E.a;
|
||||
/** property prefix */ b /** colon prefix */: N.E;
|
||||
/**method signature prefix */ foo /**open angle prefix */<X>(a: X): string {
|
||||
a: N.E.a;
|
||||
b: N.E;
|
||||
foo<X>(a: X): string {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
|
||||
@ -84,7 +84,6 @@ class C implements I {
|
||||
20: any;
|
||||
21: any;
|
||||
22: any;
|
||||
/** a nice safe prime */
|
||||
23: any;
|
||||
}`,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user