mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Omit last element semicolon from single-line object-like types
This commit is contained in:
@@ -2960,6 +2960,8 @@ namespace ts.server.protocol {
|
||||
Ignore = "ignore",
|
||||
Insert = "insert",
|
||||
Remove = "remove",
|
||||
/*@internal*/
|
||||
RemoveUnconventional = "remove-unconventional"
|
||||
}
|
||||
|
||||
export interface EditorSettings {
|
||||
|
||||
@@ -314,6 +314,7 @@ namespace ts.formatting {
|
||||
rule("SpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionEnabled("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.Space),
|
||||
rule("NoSpaceBeforeTypeAnnotation", anyToken, SyntaxKind.ColonToken, [isOptionDisabledOrUndefined("insertSpaceBeforeTypeAnnotation"), isNonJsxSameLineTokenContext, isTypeAnnotationContext], RuleAction.DeleteTrivia),
|
||||
rule("NoOptionalSemicolon", SyntaxKind.SemicolonToken, anyTokenIncludingEOF, [optionEquals("semicolons", SemicolonPreference.Remove), isSemicolonDeletionContext], RuleAction.DeleteToken),
|
||||
rule("NoUnconventionalSemicolon", SyntaxKind.SemicolonToken, anyTokenIncludingEOF, [optionEquals("semicolons", SemicolonPreference.RemoveUnconventional), isUnconventionalSemicolonDeletionContext], RuleAction.DeleteToken),
|
||||
rule("OptionalSemicolon", anyToken, anyTokenIncludingEOF, [optionEquals("semicolons", SemicolonPreference.Insert), isSemicolonInsertionContext], RuleAction.TrailingSemicolon),
|
||||
];
|
||||
|
||||
@@ -790,6 +791,14 @@ namespace ts.formatting {
|
||||
return context.contextNode.kind === SyntaxKind.NonNullExpression;
|
||||
}
|
||||
|
||||
function isUnconventionalSemicolonDeletionContext(context: FormattingContext): boolean {
|
||||
return nodeAllowsUnconventionalTrailingSemicolon(
|
||||
context.currentTokenParent,
|
||||
context.contextNode,
|
||||
context.nextTokenSpan.kind,
|
||||
context.sourceFile);
|
||||
}
|
||||
|
||||
function isSemicolonDeletionContext(context: FormattingContext): boolean {
|
||||
let nextTokenKind = context.nextTokenSpan.kind;
|
||||
let nextTokenStart = context.nextTokenSpan.pos;
|
||||
@@ -859,7 +868,7 @@ namespace ts.formatting {
|
||||
if (ancestor.end !== context.currentTokenSpan.end) {
|
||||
return "quit";
|
||||
}
|
||||
return syntaxMayBeASICandidate(ancestor.kind);
|
||||
return nodeMayBeASICandidate(ancestor);
|
||||
});
|
||||
|
||||
return !!contextAncestor && isASICandidate(contextAncestor, context.sourceFile);
|
||||
|
||||
@@ -836,12 +836,12 @@ namespace ts.textChanges {
|
||||
return {
|
||||
...context.options,
|
||||
// If the user has no semicolon preference defined and the file doesn’t use semicolons,
|
||||
// make the formatter remove them. Otherwise, ignore semicolons in the formatter because
|
||||
// make the formatter remove them. Otherwise, only remove unconventional semicolons because
|
||||
// the writer will insert them by default.
|
||||
semicolons: context.options.semicolons === SemicolonPreference.Remove ||
|
||||
(!context.options.semicolons || context.options.semicolons === SemicolonPreference.Ignore) && !probablyUsesSemicolons(sourceFile)
|
||||
? SemicolonPreference.Remove
|
||||
: SemicolonPreference.Ignore,
|
||||
: SemicolonPreference.RemoveUnconventional,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -684,6 +684,8 @@ namespace ts {
|
||||
Ignore = "ignore",
|
||||
Insert = "insert",
|
||||
Remove = "remove",
|
||||
/*@internal*/
|
||||
RemoveUnconventional = "remove-unconventional"
|
||||
}
|
||||
|
||||
/* @deprecated - consider using EditorSettings instead */
|
||||
|
||||
+43
-11
@@ -2027,13 +2027,37 @@ namespace ts {
|
||||
|| kind === SyntaxKind.ExportAssignment;
|
||||
}
|
||||
|
||||
export const syntaxMayBeASICandidate = or(
|
||||
syntaxRequiresTrailingCommaOrSemicolonOrASI,
|
||||
syntaxRequiresTrailingFunctionBlockOrSemicolonOrASI,
|
||||
syntaxRequiresTrailingModuleBlockOrSemicolonOrASI,
|
||||
syntaxRequiresTrailingSemicolonOrASI);
|
||||
function isMappedTypeNodeType(node: Node) {
|
||||
return node.parent && isMappedTypeNode(node.parent) && node.parent.type === node;
|
||||
}
|
||||
|
||||
export function isASICandidate(node: Node, sourceFile: SourceFileLike): boolean {
|
||||
export function nodeMayBeASICandidate(node: Node) {
|
||||
if (syntaxRequiresTrailingCommaOrSemicolonOrASI(node.kind) ||
|
||||
syntaxRequiresTrailingFunctionBlockOrSemicolonOrASI(node.kind) ||
|
||||
syntaxRequiresTrailingModuleBlockOrSemicolonOrASI(node.kind) ||
|
||||
syntaxRequiresTrailingSemicolonOrASI(node.kind)) {
|
||||
return true;
|
||||
}
|
||||
return isMappedTypeNodeType(node);
|
||||
}
|
||||
|
||||
export function nodeAllowsUnconventionalTrailingSemicolon(node: Node, contextNode: Node, nextTokenKind: SyntaxKind | undefined, sourceFile: SourceFileLike) {
|
||||
if (isMappedTypeNodeType(node)) {
|
||||
return rangeIsOnSingleLine(contextNode, sourceFile as SourceFile);
|
||||
}
|
||||
if (syntaxRequiresTrailingCommaOrSemicolonOrASI(node.kind)) {
|
||||
return nextTokenKind === SyntaxKind.CloseBraceToken && rangeIsOnSingleLine(contextNode, sourceFile as SourceFile);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param strict Return true for positions that allow semicolons but conventionally
|
||||
* drop them, even in code that largely contains semicolons. Examples include the last
|
||||
* declaration inside the curly braces of single-line object type literals and mapped types,
|
||||
* e.g. `type X = { x: string; }` and `type X<T> = { [K in keyof T]: T[K]; }`.
|
||||
*/
|
||||
export function isASICandidate(node: Node, sourceFile: SourceFileLike, strict?: boolean): boolean {
|
||||
const lastToken = node.getLastToken(sourceFile);
|
||||
if (lastToken && lastToken.kind === SyntaxKind.SemicolonToken) {
|
||||
return false;
|
||||
@@ -2060,20 +2084,28 @@ namespace ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
let nextToken = getNextToken();
|
||||
const contextNode = findAncestor(node, or(isObjectTypeDeclaration, isMappedTypeNode));
|
||||
if (contextNode && nodeAllowsUnconventionalTrailingSemicolon(node, contextNode, nextToken && nextToken.kind, sourceFile)) {
|
||||
return !!strict;
|
||||
}
|
||||
|
||||
// See comment in parser’s `parseDoStatement`
|
||||
if (node.kind === SyntaxKind.DoStatement) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const topNode = findAncestor(node, ancestor => !ancestor.parent)!;
|
||||
const nextToken = findNextToken(node, topNode, sourceFile);
|
||||
if (!nextToken || nextToken.kind === SyntaxKind.CloseBraceToken) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const startLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line;
|
||||
const endLine = sourceFile.getLineAndCharacterOfPosition(nextToken.getStart(sourceFile)).line;
|
||||
return startLine !== endLine;
|
||||
return !positionsAreOnSameLine(node.getEnd(),
|
||||
nextToken.getStart(sourceFile),
|
||||
sourceFile as SourceFile);
|
||||
|
||||
function getNextToken(): Node | undefined {
|
||||
return nextToken || (nextToken = findNextToken(node, findAncestor(node, ancestor => !ancestor.parent)!, sourceFile));
|
||||
}
|
||||
}
|
||||
|
||||
export function probablyUsesSemicolons(sourceFile: SourceFile): boolean {
|
||||
|
||||
+2
-2
@@ -5297,7 +5297,7 @@ declare namespace ts {
|
||||
enum SemicolonPreference {
|
||||
Ignore = "ignore",
|
||||
Insert = "insert",
|
||||
Remove = "remove"
|
||||
Remove = "remove",
|
||||
}
|
||||
interface EditorOptions {
|
||||
BaseIndentSize?: number;
|
||||
@@ -8202,7 +8202,7 @@ declare namespace ts.server.protocol {
|
||||
enum SemicolonPreference {
|
||||
Ignore = "ignore",
|
||||
Insert = "insert",
|
||||
Remove = "remove"
|
||||
Remove = "remove",
|
||||
}
|
||||
interface EditorSettings {
|
||||
baseIndentSize?: number;
|
||||
|
||||
+1
-1
@@ -5297,7 +5297,7 @@ declare namespace ts {
|
||||
enum SemicolonPreference {
|
||||
Ignore = "ignore",
|
||||
Insert = "insert",
|
||||
Remove = "remove"
|
||||
Remove = "remove",
|
||||
}
|
||||
interface EditorOptions {
|
||||
BaseIndentSize?: number;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ function /*[#|*/f/*|]*/() {
|
||||
// ==ASYNC FUNCTION::Convert to async function==
|
||||
|
||||
async function f() {
|
||||
let result: { x: number; } | { x: string; };
|
||||
let result: { x: number } | { x: string };
|
||||
try {
|
||||
await Promise.resolve();
|
||||
result = ({ x: 3 });
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ x; y;
|
||||
|
||||
// ==SCOPE::Extract to function in global scope==
|
||||
|
||||
const { x, y }: { x: number; y: string; } = /*RENAME*/newFunction();
|
||||
const { x, y }: { x: number; y: string } = /*RENAME*/newFunction();
|
||||
x; y;
|
||||
|
||||
function newFunction() {
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ x; y; z;
|
||||
|
||||
// ==SCOPE::Extract to function in global scope==
|
||||
|
||||
var { x, y, z }: { x: number; y: string; z: number; } = /*RENAME*/newFunction();
|
||||
var { x, y, z }: { x: number; y: string; z: number } = /*RENAME*/newFunction();
|
||||
x; y; z;
|
||||
|
||||
function newFunction() {
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ function f() {
|
||||
|
||||
function f() {
|
||||
let a = 1;
|
||||
let { x, y }: { x: number; y: number; } = /*RENAME*/newFunction();
|
||||
let { x, y }: { x: number; y: number } = /*RENAME*/newFunction();
|
||||
a; x; y;
|
||||
|
||||
function newFunction() {
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ function f() {
|
||||
|
||||
function f() {
|
||||
let a = 1;
|
||||
let { x, y, z }: { x: number; y: number; z: number; } = /*RENAME*/newFunction();
|
||||
let { x, y, z }: { x: number; y: number; z: number } = /*RENAME*/newFunction();
|
||||
a; x; y; z;
|
||||
|
||||
function newFunction() {
|
||||
|
||||
@@ -14,7 +14,7 @@ verify.codeFix({
|
||||
`
|
||||
/** @param {Object<string, boolean>} sb
|
||||
* @param {Object<number, string>} ns */
|
||||
function f(sb: { [s: string]: boolean; }, ns: { [n: number]: string; }) {
|
||||
function f(sb: { [s: string]: boolean }, ns: { [n: number]: string }) {
|
||||
sb; ns;
|
||||
}`,
|
||||
});
|
||||
|
||||
@@ -30,7 +30,7 @@ verify.codeFix({
|
||||
* @param alpha - the other best parameter
|
||||
* @param {*} beta - I have no idea how this got here
|
||||
*/
|
||||
function f(x: number, y: { a: string; b: Date; }, z: string, alpha, beta: any) {
|
||||
function f(x: number, y: { a: string; b: Date }, z: string, alpha, beta: any) {
|
||||
x; y; z; alpha; beta;
|
||||
}`,
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@ verify.codeFix({
|
||||
* @param {number?} gamma
|
||||
* @param {number!} delta
|
||||
*/
|
||||
function f(x: any, y: any, z: number | undefined, alpha: number[], beta: (this: { a: string; }, arg1: string, arg2: number) => boolean, gamma: number | null, delta: number) {
|
||||
function f(x: any, y: any, z: number | undefined, alpha: number[], beta: (this: { a: string }, arg1: string, arg2: number) => boolean, gamma: number | null, delta: number) {
|
||||
x; y; z; alpha; beta; gamma; delta;
|
||||
}`,
|
||||
});
|
||||
|
||||
@@ -18,6 +18,6 @@ verify.codeFix({
|
||||
}
|
||||
}
|
||||
class Person implements IPerson {
|
||||
coordinate: { x: number; y: number; };
|
||||
coordinate: { x: number; y: number };
|
||||
}`,
|
||||
});
|
||||
@@ -43,13 +43,13 @@ class C implements I {
|
||||
a0: {};
|
||||
a1: (b1: number, c1: string) => number;
|
||||
a2: (b2: number, c2: string) => number;
|
||||
a3: { (b3: number, c3: string): number; x: number; };
|
||||
a3: { (b3: number, c3: string): number; x: number };
|
||||
a4: new (b1: number, c1: string) => number;
|
||||
a5: new (b2: number, c2: string) => number;
|
||||
a6: { new(b3: number, c3: string): number; x: number; };
|
||||
a7: { foo(b7: number, c7: string): number; };
|
||||
a8: { (b81: number, c81: string): number; new(b82: number, c82: string): number; };
|
||||
a9: { (b9: number, c9: string): number;[d9: number]: I; };
|
||||
a10: { (b10: number, c10: string): number;[d10: string]: I; };
|
||||
a6: { new(b3: number, c3: string): number; x: number };
|
||||
a7: { foo(b7: number, c7: string): number };
|
||||
a8: { (b81: number, c81: string): number; new(b82: number, c82: string): number };
|
||||
a9: { (b9: number, c9: string): number;[d9: number]: I };
|
||||
a10: { (b10: number, c10: string): number;[d10: string]: I };
|
||||
}`,
|
||||
});
|
||||
|
||||
@@ -12,5 +12,5 @@
|
||||
//// }
|
||||
|
||||
verify.rangeAfterCodeFix(`
|
||||
z: number & { __iBrand: any; };
|
||||
z: number & { __iBrand: any };
|
||||
`);
|
||||
|
||||
@@ -12,6 +12,6 @@ verify.codeFix({
|
||||
x: { y: T, z: T[] };
|
||||
}
|
||||
class C implements I<number> {
|
||||
x: { y: number; z: number[]; };
|
||||
x: { y: number; z: number[] };
|
||||
}`,
|
||||
});
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
//// return result
|
||||
//// }
|
||||
|
||||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => any; }");
|
||||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => any }");
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
//// app.use('hi')
|
||||
//// }
|
||||
|
||||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => void; }");
|
||||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => void }");
|
||||
|
||||
@@ -21,5 +21,5 @@
|
||||
verify.codeFix({
|
||||
description: "Infer 'this' type of 'returnThisMember' from usage",
|
||||
index: 0,
|
||||
newRangeContent: "this: { member: string; returnThisMember: () => any; } ",
|
||||
newRangeContent: "this: { member: string; returnThisMember: () => any } ",
|
||||
});
|
||||
|
||||
@@ -13,5 +13,5 @@
|
||||
verify.codeFix({
|
||||
description: "Infer 'this' type of 'returnThisMember' from usage",
|
||||
index: 0,
|
||||
newRangeContent: "this: { member: string; returnThisMember: () => any; } ",
|
||||
newRangeContent: "this: { member: string; returnThisMember: () => any } ",
|
||||
});
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
//// }
|
||||
|
||||
// https://github.com/Microsoft/TypeScript/issues/29330
|
||||
verify.rangeAfterCodeFix("arr: { other: (arg0: (a: number, b: number) => 1 | -1) => void; }");
|
||||
verify.rangeAfterCodeFix("arr: { other: (arg0: (a: number, b: number) => 1 | -1) => void }");
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
//// var beforeExpr = !!conf.beforeExpr;
|
||||
////};
|
||||
|
||||
verify.rangeAfterCodeFix("label: any, conf: { keyword?: any; beforeExpr?: any; } | undefined",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
verify.rangeAfterCodeFix("label: any, conf: { keyword?: any; beforeExpr?: any } | undefined",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
|
||||
@@ -30,4 +30,4 @@
|
||||
//// }
|
||||
|
||||
|
||||
verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => any; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => any }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
//// f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");
|
||||
|
||||
|
||||
verify.rangeAfterCodeFix("a: number, b: string, c: { a: number; }, d: number, e = 0, ...d: (string | number)[]", /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
|
||||
verify.rangeAfterCodeFix("a: number, b: string, c: { a: number }, d: number, e = 0, ...d: (string | number)[]", /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
|
||||
|
||||
@@ -12,4 +12,4 @@
|
||||
//// return x.y.z
|
||||
////}
|
||||
|
||||
verify.rangeAfterCodeFix("a: { b: { c: void; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
|
||||
verify.rangeAfterCodeFix("a: { b: { c: void } }, m: { n: () => number }, x: { y: { z: number[] } }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
//// return a['hi'];
|
||||
////}
|
||||
|
||||
verify.rangeAfterCodeFix("a: { [x: string]: any; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
verify.rangeAfterCodeFix("a: { [x: string]: any }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
|
||||
@@ -16,4 +16,4 @@
|
||||
////kw("6", { beforeExpr: true, prefix: true, startsExpr: true })
|
||||
|
||||
|
||||
verify.rangeAfterCodeFix("name: string, options: { startsExpr?: boolean; beforeExpr?: boolean; isLoop?: boolean; prefix?: boolean; keyword?: any; } | undefined",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
verify.rangeAfterCodeFix("name: string, options: { startsExpr?: boolean; beforeExpr?: boolean; isLoop?: boolean; prefix?: boolean; keyword?: any } | undefined",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
|
||||
@@ -12,7 +12,7 @@ verify.codeFix({
|
||||
description: "Declare method 'foo1'",
|
||||
index: 0,
|
||||
newRangeContent: `
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string; }) {
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string }) {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
`,
|
||||
@@ -23,10 +23,10 @@ verify.codeFix({
|
||||
description: "Declare method 'foo2'",
|
||||
index: 0,
|
||||
newRangeContent: `
|
||||
foo2(arg0: null, arg1: {}, arg2: { a: number; b: string; }) {
|
||||
foo2(arg0: null, arg1: {}, arg2: { a: number; b: string }) {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string; }) {
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string }) {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
`,
|
||||
@@ -37,13 +37,13 @@ verify.codeFix({
|
||||
description: "Declare method 'foo3'",
|
||||
index: 0,
|
||||
newRangeContent: `
|
||||
foo3(arg0: null, arg1: {}, arg2: { a: number; b: string; }): number {
|
||||
foo3(arg0: null, arg1: {}, arg2: { a: number; b: string }): number {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
foo2(arg0: null, arg1: {}, arg2: { a: number; b: string; }) {
|
||||
foo2(arg0: null, arg1: {}, arg2: { a: number; b: string }) {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string; }) {
|
||||
foo1(arg0: null, arg1: {}, arg2: { a: number; b: string }) {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
`
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
verify.rangeAfterCodeFix(`
|
||||
class A {
|
||||
x: { a: number; b: string; c: any; d: any; e: any; };
|
||||
x: { a: number; b: string; c: any; d: any; e: any };
|
||||
|
||||
constructor() {
|
||||
let e: any = 10;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
verify.rangeAfterCodeFix(`
|
||||
class A {
|
||||
x: { a: number; b: string; c: undefined; d: null; e: any; };
|
||||
x: { a: number; b: string; c: undefined; d: null; e: any };
|
||||
|
||||
constructor() {
|
||||
let e: any = 10;
|
||||
|
||||
@@ -10,6 +10,6 @@ edit.applyRefactor({
|
||||
actionDescription: "Extract to constant in enclosing scope",
|
||||
newContent:
|
||||
`declare function fWithThis(fn: (this: { a: string }, a: string) => string): void;
|
||||
const newLocal = function(this: { a: string; }, a: string): string { return this.a; };
|
||||
const newLocal = function(this: { a: string }, a: string): string { return this.a; };
|
||||
fWithThis(/*RENAME*/newLocal);`
|
||||
});
|
||||
|
||||
@@ -10,6 +10,6 @@ edit.applyRefactor({
|
||||
actionDescription: "Extract to constant in enclosing scope",
|
||||
newContent:
|
||||
`declare function fWithThis(fn: (this: { a: string }, a: string) => string): void;
|
||||
const newLocal = function(this: { a: string; }, a: string): string { return this.a; };
|
||||
const newLocal = function(this: { a: string }, a: string): string { return this.a; };
|
||||
fWithThis(/*RENAME*/newLocal);`
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@ edit.applyRefactor({
|
||||
/*RENAME*/newFunction(x);
|
||||
}
|
||||
|
||||
function newFunction(x: { m: number; }) {
|
||||
function newFunction(x: { m: number }) {
|
||||
x.m = 3;
|
||||
}
|
||||
`
|
||||
|
||||
@@ -25,7 +25,7 @@ edit.applyRefactor({
|
||||
}
|
||||
}
|
||||
|
||||
function newFunction(m: number, j: string, k: { x: string; }) {
|
||||
function newFunction(m: number, j: string, k: { x: string }) {
|
||||
return m + j + k;
|
||||
}
|
||||
`
|
||||
|
||||
@@ -46,4 +46,4 @@ enum E {
|
||||
type M<T> = { [K in keyof T]: any };
|
||||
declare module 'foo' { }
|
||||
declare module 'bar';
|
||||
type T = { x: string, y: number; };`);
|
||||
type T = { x: string, y: number };`);
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function f({ a, b = "1" }: { a?: number; b?: string; } = {}): string {
|
||||
newContent: `function f({ a, b = "1" }: { a?: number; b?: string } = {}): string {
|
||||
return b;
|
||||
}
|
||||
f();`
|
||||
|
||||
@@ -8,6 +8,6 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `const foo = ({ a, b }: { a: number; b: number; }) => { };
|
||||
newContent: `const foo = ({ a, b }: { a: number; b: number }) => { };
|
||||
foo({ a: 1, b: 2 });`
|
||||
});
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo({ a, b, rest = [] }: { a: number; b: number; rest?: number[]; }) {
|
||||
newContent: `function foo({ a, b, rest = [] }: { a: number; b: number; rest?: number[] }) {
|
||||
return a + b;
|
||||
}
|
||||
foo({ /**a*/ a: 1 /**b*/, /**c*/ b: 2 /**d*/, rest: [/**e*/ 3 /**f*/, /**g*/ 4 /**h*/] });`
|
||||
|
||||
@@ -19,7 +19,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo({ a, b, rest = [] }: { a: number; b: number; rest?: number[]; }) {
|
||||
newContent: `function foo({ a, b, rest = [] }: { a: number; b: number; rest?: number[] }) {
|
||||
return a + b;
|
||||
}
|
||||
foo(
|
||||
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo({ a, b }: { a: number; b: number; }) {
|
||||
newContent: `function foo({ a, b }: { a: number; b: number }) {
|
||||
return { bar: () => a + b };
|
||||
}
|
||||
var x = foo({ a: 1, b: 2 }).bar();`
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ edit.applyRefactor({
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class C {
|
||||
static a: number = 2;
|
||||
constructor({ a, b }: { a: number; b: number; }) { }
|
||||
constructor({ a, b }: { a: number; b: number }) { }
|
||||
}
|
||||
const newC = new C({ a: 1, b: 2 });
|
||||
const b = C.a;
|
||||
|
||||
@@ -11,7 +11,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `const c = class {
|
||||
constructor({ a, b = { x: 1 } }: { a: number; b?: { x: number; }; }) { }
|
||||
constructor({ a, b = { x: 1 } }: { a: number; b?: { x: number } }) { }
|
||||
}
|
||||
var x = new c({ a: 2 });`
|
||||
});
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ edit.applyRefactor({
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `const c = class C {
|
||||
static a: number = 2;
|
||||
constructor({ a, b }: { a: number; b: number; }) { }
|
||||
constructor({ a, b }: { a: number; b: number }) { }
|
||||
}
|
||||
const a = new c({ a: 0, b: 1 });
|
||||
const b = c.a;
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo<T> {
|
||||
bar({ t, s }: { t: T; s: T; }) {
|
||||
bar({ t, s }: { t: T; s: T }) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ edit.applyRefactor({
|
||||
newContent: `class Foo {
|
||||
t: string;
|
||||
s: string;
|
||||
constructor({ t, s }: { t: string; s: string; }) {
|
||||
constructor({ t, s }: { t: string; s: string }) {
|
||||
this.t = t;
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function f({ a, b }: { a: number; b: string; }): string {
|
||||
newContent: `function f({ a, b }: { a: number; b: string }): string {
|
||||
return b;
|
||||
}
|
||||
f({ a: 4, b: "b" });`
|
||||
|
||||
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `foo({ a: 1, b: 2 }); /**a*/
|
||||
/**b*/ function foo(/**this1*/ this /**this2*/: /**void1*/ void /**void2*/, { a, b = /**k*/ 1 /**l*/ }: { /**c*/ a /**d*/: /**e*/ number /**f*/; /**g*/ b /**h*/?: /**i*/ number /**j*/; }) {
|
||||
/**b*/ function foo(/**this1*/ this /**this2*/: /**void1*/ void /**void2*/, { a, b = /**k*/ 1 /**l*/ }: { /**c*/ a /**d*/: /**e*/ number /**f*/; /**g*/ b /**h*/?: /**i*/ number /**j*/ }) {
|
||||
// m
|
||||
/**n*/ return a + b; // o
|
||||
// p
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo({ a, b }: { a: number /** a */; b: number /** b */; }) {
|
||||
newContent: `function foo({ a, b }: { a: number /** a */; b: number /** b */ }) {
|
||||
return a + b;
|
||||
}`
|
||||
});
|
||||
+1
-1
@@ -8,6 +8,6 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `const foo = function({ a, b }: { a: number; b: number; }) { };
|
||||
newContent: `const foo = function({ a, b }: { a: number; b: number }) { };
|
||||
foo({ a: 1, b: 2 });`
|
||||
});
|
||||
+1
-1
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo<T, S>({ t, s }: { t: T; s: S; }) {
|
||||
newContent: `function foo<T, S>({ t, s }: { t: T; s: S }) {
|
||||
return s;
|
||||
}
|
||||
foo({ t: "a", s: "b" });`
|
||||
|
||||
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export function f({ a, b }: { a: number; b: string; }): string {
|
||||
newContent: `export function f({ a, b }: { a: number; b: string }): string {
|
||||
return b;
|
||||
}`
|
||||
});
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export default function f({ a, b }: { a: number; b: string; }): string {
|
||||
newContent: `export default function f({ a, b }: { a: number; b: string }): string {
|
||||
return b;
|
||||
}`
|
||||
});
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo({ a, b }: { a: string; b: string; }) { }
|
||||
newContent: `function foo({ a, b }: { a: string; b: string }) { }
|
||||
export = foo;`
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export { foo as default };
|
||||
function foo({ a, b }: { a: number; b: number; }) {
|
||||
function foo({ a, b }: { a: number; b: number }) {
|
||||
return a + b;
|
||||
}`
|
||||
});
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export class C {
|
||||
constructor({ a, b }: { a: number; b: number; }) { }
|
||||
constructor({ a, b }: { a: number; b: number }) { }
|
||||
}`
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export function foo({ a, b }: { a: string; b: string; }) { }`
|
||||
newContent: `export function foo({ a, b }: { a: string; b: string }) { }`
|
||||
});
|
||||
|
||||
goTo.file("a.ts");
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo {
|
||||
constructor({ t, s }: { t: string; s: string; }) { }
|
||||
constructor({ t, s }: { t: string; s: string }) { }
|
||||
}
|
||||
class Bar extends Foo { }
|
||||
var bar = new Bar({ t: "a", s: "b" });
|
||||
|
||||
@@ -15,7 +15,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo {
|
||||
bar({ t, s }: { t: string; s: string; }): string {
|
||||
bar({ t, s }: { t: string; s: string }): string {
|
||||
return s + t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function f({ a, b = "1" }: { a: number; b?: string; }): string {
|
||||
newContent: `function f({ a, b = "1" }: { a: number; b?: string }): string {
|
||||
return b;
|
||||
}
|
||||
f({ a: 4, b: "b" });`
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function f({ a, b = { x: 1, z: { s: true } } }: { a: number; b?: { x: number; z: { s: boolean; }; }; }) {
|
||||
newContent: `function f({ a, b = { x: 1, z: { s: true } } }: { a: number; b?: { x: number; z: { s: boolean } } }) {
|
||||
return b;
|
||||
}
|
||||
f({ a: 2 });`
|
||||
|
||||
@@ -14,7 +14,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo {
|
||||
bar({ t, s }: { t: string; s: string; }): string {
|
||||
bar({ t, s }: { t: string; s: string }): string {
|
||||
return s + t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo {
|
||||
bar({ t, s }: { t: string; s: string; }): string {
|
||||
bar({ t, s }: { t: string; s: string }): string {
|
||||
return s + t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `export default class {
|
||||
constructor({ a, b }: { a: string; b: string; }) { }
|
||||
constructor({ a, b }: { a: string; b: string }) { }
|
||||
}`
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `const f = function foo({ a, b }: { a: number; b: number; }) {
|
||||
newContent: `const f = function foo({ a, b }: { a: number; b: number }) {
|
||||
foo({ a: 1, b: 2 });
|
||||
}
|
||||
function foo(a: number, b: number) { }
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function log({ a, b, args = [] }: { a: number; b: number; args?: any[]; }) { }
|
||||
newContent: `function log({ a, b, args = [] }: { a: number; b: number; args?: any[] }) { }
|
||||
let l = log({ a: -1, b: -2, args: [3, 4, 5] });
|
||||
let k = log({ a: 1, b: 2 });`
|
||||
});
|
||||
+1
-1
@@ -15,7 +15,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function f({ a, b, rest = [] }: { a: number; b: number; rest?: string[]; }) { }
|
||||
newContent: `function f({ a, b, rest = [] }: { a: number; b: number; rest?: string[] }) { }
|
||||
const a = 4;
|
||||
const b = 5;
|
||||
f({ a, b });
|
||||
|
||||
@@ -13,7 +13,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class Foo {
|
||||
static bar({ t, s }: { t: string; s: string; }): string {
|
||||
static bar({ t, s }: { t: string; s: string }): string {
|
||||
return s + t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `class A {
|
||||
constructor({ a, b }: { a: string; b: string; }) { }
|
||||
constructor({ a, b }: { a: string; b: string }) { }
|
||||
}
|
||||
class B extends A {
|
||||
constructor(a: string, b: string, c: string) {
|
||||
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: [
|
||||
'function insert({ template, overwriteBefore = 0 }: { template: string; overwriteBefore?: number; }) {}',
|
||||
'function insert({ template, overwriteBefore = 0 }: { template: string; overwriteBefore?: number }) {}',
|
||||
'insert({ template: `this is \\${not} a substitution` });'
|
||||
].join('\n')
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function foo(this: void, { t, s }: { t: string; s: string; }) {
|
||||
newContent: `function foo(this: void, { t, s }: { t: string; s: string }) {
|
||||
return s;
|
||||
}
|
||||
foo({ t: "a", s: "b" });`
|
||||
|
||||
@@ -23,7 +23,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function fn1({ a, b, args }: { a: number; b: number; args: [number, number]; }) { }
|
||||
newContent: `function fn1({ a, b, args }: { a: number; b: number; args: [number, number] }) { }
|
||||
fn1({ a: 1, b: 2, args: [3, 4] });`
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function fn2({ a, b, args }: { a: number; b: number; args: [number, number, ...string[]]; }) { }
|
||||
newContent: `function fn2({ a, b, args }: { a: number; b: number; args: [number, number, ...string[]] }) { }
|
||||
fn2({ a: 1, b: 2, args: [3, 4] });
|
||||
fn2({ a: 1, b: 2, args: [3, 4, "a"] });`
|
||||
});
|
||||
@@ -42,7 +42,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function fn3({ b, c }: { b: boolean; c: []; }) { }
|
||||
newContent: `function fn3({ b, c }: { b: boolean; c: [] }) { }
|
||||
fn3({ b: true, c: [] });`
|
||||
});
|
||||
|
||||
@@ -51,7 +51,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function fn4({ a, args = [] }: { a: number; args?: [...string[]]; }) { }
|
||||
newContent: `function fn4({ a, args = [] }: { a: number; args?: [...string[]] }) { }
|
||||
fn4({ a: 2 });
|
||||
fn4({ a: 1, args: ["two", "three"] });`
|
||||
});
|
||||
@@ -9,7 +9,7 @@ edit.applyRefactor({
|
||||
refactorName: "Convert parameters to destructured object",
|
||||
actionName: "Convert parameters to destructured object",
|
||||
actionDescription: "Convert parameters to destructured object",
|
||||
newContent: `function buildName({ firstName, middleName, restOfName = [] }: { firstName: string; middleName?: string; restOfName?: string[]; }) { }
|
||||
newContent: `function buildName({ firstName, middleName, restOfName = [] }: { firstName: string; middleName?: string; restOfName?: string[] }) { }
|
||||
let employeeName = buildName({ firstName: "Joseph", middleName: "Samuel", restOfName: ["Lucas", "MacKinzie"] });
|
||||
let myName = buildName({ firstName: "Joseph" });`
|
||||
});
|
||||
@@ -3,4 +3,4 @@
|
||||
// @noImplicitAny: true
|
||||
//// function f([|y |], z = { p: y[
|
||||
|
||||
verify.rangeAfterCodeFix("y: { [x: string]: any; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
verify.rangeAfterCodeFix("y: { [x: string]: any }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
|
||||
|
||||
Reference in New Issue
Block a user