Skip to content

Commit

Permalink
Cherry pick #50537 and #50779 to release-4.8 (#50907)
Browse files Browse the repository at this point in the history
* 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 <wewigham@microsoft.com>
  • Loading branch information
jakebailey and weswigham authored Sep 22, 2022
1 parent 41e1ade commit 369b4d8
Show file tree
Hide file tree
Showing 30 changed files with 395 additions and 133 deletions.
28 changes: 9 additions & 19 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6094,29 +6094,19 @@ namespace ts {
return parameterNode;

function cloneBindingName(node: BindingName): BindingName {
return elideInitializerAndPropertyRenamingAndSetEmitFlags(node) as BindingName;
function elideInitializerAndPropertyRenamingAndSetEmitFlags(node: Node): Node {
return elideInitializerAndSetEmitFlags(node) as BindingName;
function elideInitializerAndSetEmitFlags(node: Node): Node {
if (context.tracker.trackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
trackComputedName(node.expression, context.enclosingDeclaration, context);
}
let visited = visitEachChild(node, elideInitializerAndPropertyRenamingAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndPropertyRenamingAndSetEmitFlags)!;
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
if (isBindingElement(visited)) {
if (visited.propertyName && isIdentifier(visited.propertyName) && isIdentifier(visited.name)) {
visited = factory.updateBindingElement(
visited,
visited.dotDotDotToken,
/* propertyName*/ undefined,
visited.propertyName,
/*initializer*/ undefined);
}
else {
visited = factory.updateBindingElement(
visited,
visited.dotDotDotToken,
visited.propertyName,
visited.name,
/*initializer*/ undefined);
}
visited = factory.updateBindingElement(
visited,
visited.dotDotDotToken,
visited.propertyName,
visited.name,
/*initializer*/ undefined);
}
if (!nodeIsSynthesized(visited)) {
visited = factory.cloneNode(visited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface Show {
>x : number
}
function f({ show: showRename = v => v }: Show) {}
>f : ({ show }: Show) => void
>f : ({ show: showRename }: Show) => void
>show : any
>showRename : (x: number) => string
>v => v : (v: number) => number
Expand All @@ -32,7 +32,7 @@ interface Nested {
>nested : Show
}
function ff({ nested: nestedRename = { show: v => v } }: Nested) {}
>ff : ({ nested }: Nested) => void
>ff : ({ nested: nestedRename }: Nested) => void
>nested : any
>nestedRename : Show
>{ show: v => v } : { show: (v: number) => number; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [declarationEmitBindingPatternWithReservedWord.ts]
type LocaleData = Record<string, never>
type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
string,
T
>;
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;

export interface GetLocalesOptions<T extends LocaleData> {
app: unknown;
default: ConvertLocaleConfig<T>;
config?: LocaleConfig<T> | undefined;
name?: string;
}

export const getLocales = <T extends LocaleData>({
app,
name,
default: defaultLocalesConfig,
config: userLocalesConfig = {},
}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
return defaultLocalesConfig;
};


//// [declarationEmitBindingPatternWithReservedWord.js]
"use strict";
exports.__esModule = true;
exports.getLocales = void 0;
var getLocales = function (_a) {
var app = _a.app, name = _a.name, defaultLocalesConfig = _a["default"], _b = _a.config, userLocalesConfig = _b === void 0 ? {} : _b;
return defaultLocalesConfig;
};
exports.getLocales = getLocales;


//// [declarationEmitBindingPatternWithReservedWord.d.ts]
declare type LocaleData = Record<string, never>;
declare type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<string, T>;
declare type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
export interface GetLocalesOptions<T extends LocaleData> {
app: unknown;
default: ConvertLocaleConfig<T>;
config?: LocaleConfig<T> | undefined;
name?: string;
}
export declare const getLocales: <T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig, }: GetLocalesOptions<T>) => ConvertLocaleConfig<T>;
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
=== tests/cases/compiler/declarationEmitBindingPatternWithReservedWord.ts ===
type LocaleData = Record<string, never>
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 1, 25))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

string,
T
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 1, 25))

>;
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
>LocaleConfig : Symbol(LocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 4, 2))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 18))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 18))

export interface GetLocalesOptions<T extends LocaleData> {
>GetLocalesOptions : Symbol(GetLocalesOptions, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 82))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))

app: unknown;
>app : Symbol(GetLocalesOptions.app, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 58))

default: ConvertLocaleConfig<T>;
>default : Symbol(GetLocalesOptions.default, Decl(declarationEmitBindingPatternWithReservedWord.ts, 8, 17))
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))

config?: LocaleConfig<T> | undefined;
>config : Symbol(GetLocalesOptions.config, Decl(declarationEmitBindingPatternWithReservedWord.ts, 9, 36))
>LocaleConfig : Symbol(LocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 4, 2))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 7, 35))

name?: string;
>name : Symbol(GetLocalesOptions.name, Decl(declarationEmitBindingPatternWithReservedWord.ts, 10, 41))
}

export const getLocales = <T extends LocaleData>({
>getLocales : Symbol(getLocales, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 12))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))
>LocaleData : Symbol(LocaleData, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 0))

app,
>app : Symbol(app, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 50))

name,
>name : Symbol(name, Decl(declarationEmitBindingPatternWithReservedWord.ts, 15, 8))

default: defaultLocalesConfig,
>default : Symbol(GetLocalesOptions.default, Decl(declarationEmitBindingPatternWithReservedWord.ts, 8, 17))
>defaultLocalesConfig : Symbol(defaultLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 16, 9))

config: userLocalesConfig = {},
>config : Symbol(GetLocalesOptions.config, Decl(declarationEmitBindingPatternWithReservedWord.ts, 9, 36))
>userLocalesConfig : Symbol(userLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 17, 34))

}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
>GetLocalesOptions : Symbol(GetLocalesOptions, Decl(declarationEmitBindingPatternWithReservedWord.ts, 5, 82))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))
>ConvertLocaleConfig : Symbol(ConvertLocaleConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 0, 39))
>T : Symbol(T, Decl(declarationEmitBindingPatternWithReservedWord.ts, 14, 27))

return defaultLocalesConfig;
>defaultLocalesConfig : Symbol(defaultLocalesConfig, Decl(declarationEmitBindingPatternWithReservedWord.ts, 16, 9))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
=== tests/cases/compiler/declarationEmitBindingPatternWithReservedWord.ts ===
type LocaleData = Record<string, never>
>LocaleData : { [x: string]: never; }

type ConvertLocaleConfig<T extends LocaleData = LocaleData> = Record<
>ConvertLocaleConfig : ConvertLocaleConfig<T>

string,
T
>;
type LocaleConfig<T extends LocaleData = LocaleData> = Record<string, Partial<T>>;
>LocaleConfig : LocaleConfig<T>

export interface GetLocalesOptions<T extends LocaleData> {
app: unknown;
>app : unknown

default: ConvertLocaleConfig<T>;
>default : ConvertLocaleConfig<T>

config?: LocaleConfig<T> | undefined;
>config : LocaleConfig<T>

name?: string;
>name : string
}

export const getLocales = <T extends LocaleData>({
>getLocales : <T extends LocaleData>({ app, name, default: defaultLocalesConfig, config: userLocalesConfig, }: GetLocalesOptions<T>) => ConvertLocaleConfig<T>
><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>

app,
>app : unknown

name,
>name : string

default: defaultLocalesConfig,
>default : any
>defaultLocalesConfig : ConvertLocaleConfig<T>

config: userLocalesConfig = {},
>config : any
>userLocalesConfig : LocaleConfig<T>
>{} : {}

}: GetLocalesOptions<T>): ConvertLocaleConfig<T> => {
return defaultLocalesConfig;
>defaultLocalesConfig : ConvertLocaleConfig<T>

};

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function f(_a, _b, _c) {


//// [declarationEmitBindingPatterns.d.ts]
declare const k: ({ x }: {
declare const k: ({ x: z }: {
x?: string;
}) => void;
declare var a: any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/compiler/declarationEmitBindingPatterns.ts ===
const k = ({x: z = 'y'}) => { }
>k : ({ x }: { x?: string; }) => void
>({x: z = 'y'}) => { } : ({ x }: { x?: string; }) => void
>k : ({ x: z }: { x?: string; }) => void
>({x: z = 'y'}) => { } : ({ x: z }: { x?: string; }) => void
>x : any
>z : string
>'y' : "y"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [declarationEmitDuplicateParameterDestructuring.ts]
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;

export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;




//// [declarationEmitDuplicateParameterDestructuring.d.ts]
export declare const fn1: ({ prop: a, prop: b }: {
prop: number;
}) => number;
export declare const fn2: ({ prop: a }: {
prop: number;
}, { prop: b }: {
prop: number;
}) => number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/declarationEmitDuplicateParameterDestructuring.ts ===
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;
>fn1 : Symbol(fn1, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 12))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 21))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 30))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 43))
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 21))
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 0, 30))

export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;
>fn2 : Symbol(fn2, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 12))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 34))
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 21))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 34))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 65))
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 52))
>prop : Symbol(prop, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 65))
>a : Symbol(a, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 21))
>b : Symbol(b, Decl(declarationEmitDuplicateParameterDestructuring.ts, 2, 52))

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/declarationEmitDuplicateParameterDestructuring.ts ===
export const fn1 = ({ prop: a, prop: b }: { prop: number }) => a + b;
>fn1 : ({ prop: a, prop: b }: { prop: number;}) => number
>({ prop: a, prop: b }: { prop: number }) => a + b : ({ prop: a, prop: b }: { prop: number;}) => number
>prop : any
>a : number
>prop : any
>b : number
>prop : number
>a + b : number
>a : number
>b : number

export const fn2 = ({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b;
>fn2 : ({ prop: a }: { prop: number;}, { prop: b }: { prop: number;}) => number
>({ prop: a }: { prop: number }, { prop: b }: { prop: number }) => a + b : ({ prop: a }: { prop: number;}, { prop: b }: { prop: number;}) => number
>prop : any
>a : number
>prop : number
>prop : any
>b : number
>prop : number
>a + b : number
>a : number
>b : number

8 changes: 4 additions & 4 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function f13() {
}

function f14([a = 1, [b = "hello", { x, y: c = false }]]) {
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>a : number
>1 : 1
>b : string
Expand All @@ -438,7 +438,7 @@ function f14([a = 1, [b = "hello", { x, y: c = false }]]) {
}
f14([2, ["abc", { x: 0, y: true }]]);
>f14([2, ["abc", { x: 0, y: true }]]) : void
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>[2, ["abc", { x: 0, y: true }]] : [number, [string, { x: number; y: true; }]]
>2 : 2
>["abc", { x: 0, y: true }] : [string, { x: number; y: true; }]
Expand All @@ -451,7 +451,7 @@ f14([2, ["abc", { x: 0, y: true }]]);

f14([2, ["abc", { x: 0 }]]);
>f14([2, ["abc", { x: 0 }]]) : void
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>[2, ["abc", { x: 0 }]] : [number, [string, { x: number; }]]
>2 : 2
>["abc", { x: 0 }] : [string, { x: number; }]
Expand All @@ -462,7 +462,7 @@ f14([2, ["abc", { x: 0 }]]);

f14([2, ["abc", { y: false }]]); // Error, no x
>f14([2, ["abc", { y: false }]]) : void
>f14 : ([a, [b, { x, y }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>f14 : ([a, [b, { x, y: c }]]: [number, [string, { x: any; y?: boolean; }]]) => void
>[2, ["abc", { y: false }]] : [number, [string, { y: false; }]]
>2 : 2
>["abc", { y: false }] : [string, { y: false; }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type T3 = ([{ a: b }, { b: a }]);
>b : a

type F3 = ([{ a: b }, { b: a }]) => void;
>F3 : ([{ a }, { b }]: [{ a: any; }, { b: any; }]) => void
>F3 : ([{ a: b }, { b: a }]: [{ a: any; }, { b: any; }]) => void
>a : any
>b : any
>b : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ d5(); // Parameter is optional as its declaration included an initializer
// Type annotations must instead be written on the top- level parameter declaration

function e1({x: number}) { } // x has type any NOT number
>e1 : ({ x }: { x: any; }) => void
>e1 : ({ x: number }: { x: any; }) => void
>x : any
>number : any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ d5(); // Parameter is optional as its declaration included an initializer
// Type annotations must instead be written on the top- level parameter declaration

function e1({x: number}) { } // x has type any NOT number
>e1 : ({ x }: { x: any; }) => void
>e1 : ({ x: number }: { x: any; }) => void
>x : any
>number : any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ d5(); // Parameter is optional as its declaration included an initializer
// Type annotations must instead be written on the top- level parameter declaration

function e1({x: number}) { } // x has type any NOT number
>e1 : ({ x }: { x: any; }) => void
>e1 : ({ x: number }: { x: any; }) => void
>x : any
>number : any

Expand Down
Loading

0 comments on commit 369b4d8

Please sign in to comment.