Skip to content

Commit 369b4d8

Browse files
Cherry pick #50537 and #50779 to release-4.8 (#50907)
* Retain name and propertyName in declaration emit copies of binding patterns if property name is a keyword (#50537) * Retain name and propertyName in declaration emit copies of binding patterns if property name is a keyword * Accept baselines * Remove out of date file * Partially revert #41044, restoring parameter destructurings in d.ts files (#50779) * Update baselines Co-authored-by: Wesley Wigham <[email protected]>
1 parent 41e1ade commit 369b4d8

File tree

30 files changed

+395
-133
lines changed

30 files changed

+395
-133
lines changed

Diff for: src/compiler/checker.ts

+9-19
Original file line numberDiff line numberDiff line change
@@ -6094,29 +6094,19 @@ namespace ts {
60946094
return parameterNode;
60956095

60966096
function cloneBindingName(node: BindingName): BindingName {
6097-
return elideInitializerAndPropertyRenamingAndSetEmitFlags(node) as BindingName;
6098-
function elideInitializerAndPropertyRenamingAndSetEmitFlags(node: Node): Node {
6097+
return elideInitializerAndSetEmitFlags(node) as BindingName;
6098+
function elideInitializerAndSetEmitFlags(node: Node): Node {
60996099
if (context.tracker.trackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
61006100
trackComputedName(node.expression, context.enclosingDeclaration, context);
61016101
}
6102-
let visited = visitEachChild(node, elideInitializerAndPropertyRenamingAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndPropertyRenamingAndSetEmitFlags)!;
6102+
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
61036103
if (isBindingElement(visited)) {
6104-
if (visited.propertyName && isIdentifier(visited.propertyName) && isIdentifier(visited.name)) {
6105-
visited = factory.updateBindingElement(
6106-
visited,
6107-
visited.dotDotDotToken,
6108-
/* propertyName*/ undefined,
6109-
visited.propertyName,
6110-
/*initializer*/ undefined);
6111-
}
6112-
else {
6113-
visited = factory.updateBindingElement(
6114-
visited,
6115-
visited.dotDotDotToken,
6116-
visited.propertyName,
6117-
visited.name,
6118-
/*initializer*/ undefined);
6119-
}
6104+
visited = factory.updateBindingElement(
6105+
visited,
6106+
visited.dotDotDotToken,
6107+
visited.propertyName,
6108+
visited.name,
6109+
/*initializer*/ undefined);
61206110
}
61216111
if (!nodeIsSynthesized(visited)) {
61226112
visited = factory.cloneNode(visited);

Diff for: tests/baselines/reference/contextuallyTypedBindingInitializerNegative.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface Show {
55
>x : number
66
}
77
function f({ show: showRename = v => v }: Show) {}
8-
>f : ({ show }: Show) => void
8+
>f : ({ show: showRename }: Show) => void
99
>show : any
1010
>showRename : (x: number) => string
1111
>v => v : (v: number) => number
@@ -32,7 +32,7 @@ interface Nested {
3232
>nested : Show
3333
}
3434
function ff({ nested: nestedRename = { show: v => v } }: Nested) {}
35-
>ff : ({ nested }: Nested) => void
35+
>ff : ({ nested: nestedRename }: Nested) => void
3636
>nested : any
3737
>nestedRename : Show
3838
>{ show: v => v } : { show: (v: number) => number; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// [declarationEmitBindingPatternWithReservedWord.ts]
2+
type LocaleData = Record<string, never>
3+
type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
4+
string,
5+
T
6+
>;
7+
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
8+
9+
export interface GetLocalesOptions<T extends LocaleData> {
10+
app: unknown;
11+
default: ConvertLocaleConfig<T>;
12+
config?: LocaleConfig<T> | undefined;
13+
name?: string;
14+
}
15+
16+
export const getLocales = <T extends LocaleData>({
17+
app,
18+
name,
19+
default: defaultLocalesConfig,
20+
config: userLocalesConfig = {},
21+
}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
22+
return defaultLocalesConfig;
23+
};
24+
25+
26+
//// [declarationEmitBindingPatternWithReservedWord.js]
27+
"use strict";
28+
exports.__esModule = true;
29+
exports.getLocales = void 0;
30+
var getLocales = function (_a) {
31+
var app = _a.app, name = _a.name, defaultLocalesConfig = _a["default"], _b = _a.config, userLocalesConfig = _b === void 0 ? {} : _b;
32+
return defaultLocalesConfig;
33+
};
34+
exports.getLocales = getLocales;
35+
36+
37+
//// [declarationEmitBindingPatternWithReservedWord.d.ts]
38+
declare type LocaleData = Record<string, never>;
39+
declare type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<string, T>;
40+
declare type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
41+
export interface GetLocalesOptions<T extends LocaleData> {
42+
app: unknown;
43+
default: ConvertLocaleConfig<T>;
44+
config?: LocaleConfig<T> | undefined;
45+
name?: string;
46+
}
47+
export declare const getLocales: <T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig, }: GetLocalesOptions<T>) => ConvertLocaleConfig<T>;
48+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=== tests/cases/compiler/declarationEmitBindingPatternWithReservedWord.ts ===
2+
type LocaleData = Record<string, never>
3+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
4+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
5+
6+
type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
7+
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
8+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 1, 25))
9+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
10+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
11+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
12+
13+
string,
14+
T
15+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 1, 25))
16+
17+
>;
18+
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
19+
>LocaleConfig : Symbol(LocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 4, 2))
20+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 18))
21+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
22+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
23+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
24+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
25+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 18))
26+
27+
export interface GetLocalesOptions<T extends LocaleData> {
28+
>GetLocalesOptions : Symbol(GetLocalesOptions, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 82))
29+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))
30+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
31+
32+
app: unknown;
33+
>app : Symbol(GetLocalesOptions.app, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 58))
34+
35+
default: ConvertLocaleConfig<T>;
36+
>default : Symbol(GetLocalesOptions.default, Decl(declarationEmitBindingPatternWithReservedWord.ts, 8, 17))
37+
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
38+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))
39+
40+
config?: LocaleConfig<T> | undefined;
41+
>config : Symbol(GetLocalesOptions.config, Decl(declarationEmitBindingPatternWithReservedWord.ts, 9, 36))
42+
>LocaleConfig : Symbol(LocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 4, 2))
43+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))
44+
45+
name?: string;
46+
>name : Symbol(GetLocalesOptions.name, Decl(declarationEmitBindingPatternWithReservedWord.ts, 10, 41))
47+
}
48+
49+
export const getLocales = <T extends LocaleData>({
50+
>getLocales : Symbol(getLocales, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 12))
51+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))
52+
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
53+
54+
app,
55+
>app : Symbol(app, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 50))
56+
57+
name,
58+
>name : Symbol(name, Decl(declarationEmitBindingPatternWithReservedWord.ts, 15, 8))
59+
60+
default: defaultLocalesConfig,
61+
>default : Symbol(GetLocalesOptions.default, Decl(declarationEmitBindingPatternWithReservedWord.ts, 8, 17))
62+
>defaultLocalesConfig : Symbol(defaultLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 16, 9))
63+
64+
config: userLocalesConfig = {},
65+
>config : Symbol(GetLocalesOptions.config, Decl(declarationEmitBindingPatternWithReservedWord.ts, 9, 36))
66+
>userLocalesConfig : Symbol(userLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 17, 34))
67+
68+
}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
69+
>GetLocalesOptions : Symbol(GetLocalesOptions, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 82))
70+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))
71+
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
72+
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))
73+
74+
return defaultLocalesConfig;
75+
>defaultLocalesConfig : Symbol(defaultLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 16, 9))
76+
77+
};
78+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/declarationEmitBindingPatternWithReservedWord.ts ===
2+
type LocaleData = Record<string, never>
3+
>LocaleData : { [x: string]: never; }
4+
5+
type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
6+
>ConvertLocaleConfig : ConvertLocaleConfig<T>
7+
8+
string,
9+
T
10+
>;
11+
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
12+
>LocaleConfig : LocaleConfig<T>
13+
14+
export interface GetLocalesOptions<T extends LocaleData> {
15+
app: unknown;
16+
>app : unknown
17+
18+
default: ConvertLocaleConfig<T>;
19+
>default : ConvertLocaleConfig<T>
20+
21+
config?: LocaleConfig<T> | undefined;
22+
>config : LocaleConfig<T>
23+
24+
name?: string;
25+
>name : string
26+
}
27+
28+
export const getLocales = <T extends LocaleData>({
29+
>getLocales : <T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig, }: GetLocalesOptions<T>) => ConvertLocaleConfig<T>
30+
><T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig = {},}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => { return defaultLocalesConfig;} : <T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig, }: GetLocalesOptions<T>) => ConvertLocaleConfig<T>
31+
32+
app,
33+
>app : unknown
34+
35+
name,
36+
>name : string
37+
38+
default: defaultLocalesConfig,
39+
>default : any
40+
>defaultLocalesConfig : ConvertLocaleConfig<T>
41+
42+
config: userLocalesConfig = {},
43+
>config : any
44+
>userLocalesConfig : LocaleConfig<T>
45+
>{} : {}
46+
47+
}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
48+
return defaultLocalesConfig;
49+
>defaultLocalesConfig : ConvertLocaleConfig<T>
50+
51+
};
52+

Diff for: tests/baselines/reference/declarationEmitBindingPatterns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function f(_a, _b, _c) {
1818

1919

2020
//// [declarationEmitBindingPatterns.d.ts]
21-
declare const k: ({ x }: {
21+
declare const k: ({ x: z }: {
2222
x?: string;
2323
}) => void;
2424
declare var a: any;

Diff for: tests/baselines/reference/declarationEmitBindingPatterns.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/compiler/declarationEmitBindingPatterns.ts ===
22
const k = ({x: z = 'y'}) => { }
3-
>k : ({ x }: { x?: string; }) => void
4-
>({x: z = 'y'}) => { } : ({ x }: { x?: string; }) => void
3+
>k : ({ x: z }: { x?: string; }) => void
4+
>({x: z = 'y'}) => { } : ({ x: z }: { x?: string; }) => void
55
>x : any
66
>z : string
77
>'y' : "y"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [declarationEmitDuplicateParameterDestructuring.ts]
2+
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;
3+
4+
export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;
5+
6+
7+
8+
9+
//// [declarationEmitDuplicateParameterDestructuring.d.ts]
10+
export declare const fn1: ({ prop: a, prop: b }: {
11+
prop: number;
12+
}) => number;
13+
export declare const fn2: ({ prop: a }: {
14+
prop: number;
15+
}, { prop: b }: {
16+
prop: number;
17+
}) => number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/declarationEmitDuplicateParameterDestructuring.ts ===
2+
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;
3+
>fn1 : Symbol(fn1, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 12))
4+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
5+
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 21))
6+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
7+
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 30))
8+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
9+
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 21))
10+
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 30))
11+
12+
export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;
13+
>fn2 : Symbol(fn2, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 12))
14+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 34))
15+
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 21))
16+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 34))
17+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 65))
18+
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 52))
19+
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 65))
20+
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 21))
21+
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 52))
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/declarationEmitDuplicateParameterDestructuring.ts ===
2+
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;
3+
>fn1 : ({ prop: a, prop: b }: { prop: number;}) => number
4+
>({ prop: a, prop: b }: { prop: number }) => a + b : ({ prop: a, prop: b }: { prop: number;}) => number
5+
>prop : any
6+
>a : number
7+
>prop : any
8+
>b : number
9+
>prop : number
10+
>a + b : number
11+
>a : number
12+
>b : number
13+
14+
export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;
15+
>fn2 : ({ prop: a }: { prop: number;}, { prop: b }: { prop: number;}) => number
16+
>({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b : ({ prop: a }: { prop: number;}, { prop: b }: { prop: number;}) => number
17+
>prop : any
18+
>a : number
19+
>prop : number
20+
>prop : any
21+
>b : number
22+
>prop : number
23+
>a + b : number
24+
>a : number
25+
>b : number
26+

Diff for: tests/baselines/reference/declarationsAndAssignments.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ function f13() {
417417
}
418418

419419
function f14([a = 1, [b = "hello", { x, y: c = false }]]) {
420-
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
420+
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
421421
>a : number
422422
>1 : 1
423423
>b : string
@@ -438,7 +438,7 @@ function f14([a = 1, [b = "hello", { x, y: c = false }]]) {
438438
}
439439
f14([2, ["abc", { x: 0, y: true }]]);
440440
>f14([2, ["abc", { x: 0, y: true }]]) : void
441-
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
441+
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
442442
>[2, ["abc", { x: 0, y: true }]] : [number, [string, { x: number; y: true; }]]
443443
>2 : 2
444444
>["abc", { x: 0, y: true }] : [string, { x: number; y: true; }]
@@ -451,7 +451,7 @@ f14([2, ["abc", { x: 0, y: true }]]);
451451

452452
f14([2, ["abc", { x: 0 }]]);
453453
>f14([2, ["abc", { x: 0 }]]) : void
454-
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
454+
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
455455
>[2, ["abc", { x: 0 }]] : [number, [string, { x: number; }]]
456456
>2 : 2
457457
>["abc", { x: 0 }] : [string, { x: number; }]
@@ -462,7 +462,7 @@ f14([2, ["abc", { x: 0 }]]);
462462

463463
f14([2, ["abc", { y: false }]]); // Error, no x
464464
>f14([2, ["abc", { y: false }]]) : void
465-
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
465+
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
466466
>[2, ["abc", { y: false }]] : [number, [string, { y: false; }]]
467467
>2 : 2
468468
>["abc", { y: false }] : [string, { y: false; }]

Diff for: tests/baselines/reference/destructuringInFunctionType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type T3 = ([{ a: b }, { b: a }]);
3131
>b : a
3232

3333
type F3 = ([{ a: b }, { b: a }]) => void;
34-
>F3 : ([{ a }, { b }]: [{ a: any; }, { b: any; }]) => void
34+
>F3 : ([{ a: b }, { b: a }]: [{ a: any; }, { b: any; }]) => void
3535
>a : any
3636
>b : any
3737
>b : any

Diff for: tests/baselines/reference/destructuringParameterDeclaration1ES5.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ d5(); // Parameter is optional as its declaration included an initializer
402402
// Type annotations must instead be written on the top- level parameter declaration
403403

404404
function e1({x: number}) { } // x has type any NOT number
405-
>e1 : ({ x }: { x: any; }) => void
405+
>e1 : ({ x: number }: { x: any; }) => void
406406
>x : any
407407
>number : any
408408

Diff for: tests/baselines/reference/destructuringParameterDeclaration1ES5iterable.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ d5(); // Parameter is optional as its declaration included an initializer
402402
// Type annotations must instead be written on the top- level parameter declaration
403403

404404
function e1({x: number}) { } // x has type any NOT number
405-
>e1 : ({ x }: { x: any; }) => void
405+
>e1 : ({ x: number }: { x: any; }) => void
406406
>x : any
407407
>number : any
408408

Diff for: tests/baselines/reference/destructuringParameterDeclaration1ES6.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ d5(); // Parameter is optional as its declaration included an initializer
376376
// Type annotations must instead be written on the top- level parameter declaration
377377

378378
function e1({x: number}) { } // x has type any NOT number
379-
>e1 : ({ x }: { x: any; }) => void
379+
>e1 : ({ x: number }: { x: any; }) => void
380380
>x : any
381381
>number : any
382382

0 commit comments

Comments
 (0)