Skip to content

Commit b15b484

Browse files
committed
Remove renaming from declaration output
1 parent e360400 commit b15b484

10 files changed

+160
-19
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5267,19 +5267,29 @@ namespace ts {
52675267
return parameterNode;
52685268

52695269
function cloneBindingName(node: BindingName): BindingName {
5270-
return <BindingName>elideInitializerAndSetEmitFlags(node);
5271-
function elideInitializerAndSetEmitFlags(node: Node): Node {
5270+
return <BindingName>elideInitializerAndPropertyRenamingAndSetEmitFlags(node);
5271+
function elideInitializerAndPropertyRenamingAndSetEmitFlags(node: Node): Node {
52725272
if (context.tracker.trackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
52735273
trackComputedName(node.expression, context.enclosingDeclaration, context);
52745274
}
5275-
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
5275+
let visited = visitEachChild(node, elideInitializerAndPropertyRenamingAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndPropertyRenamingAndSetEmitFlags)!;
52765276
if (isBindingElement(visited)) {
5277-
visited = factory.updateBindingElement(
5278-
visited,
5279-
visited.dotDotDotToken,
5280-
visited.propertyName,
5281-
visited.name,
5282-
/*initializer*/ undefined);
5277+
if (visited.propertyName && isIdentifier(visited.propertyName) && isIdentifier(visited.name)) {
5278+
visited = factory.updateBindingElement(
5279+
visited,
5280+
visited.dotDotDotToken,
5281+
/* propertyName*/ undefined,
5282+
visited.propertyName,
5283+
/*initializer*/ undefined);
5284+
}
5285+
else {
5286+
visited = factory.updateBindingElement(
5287+
visited,
5288+
visited.dotDotDotToken,
5289+
visited.propertyName,
5290+
visited.name,
5291+
/*initializer*/ undefined);
5292+
}
52835293
}
52845294
if (!nodeIsSynthesized(visited)) {
52855295
visited = factory.cloneNode(visited);
@@ -33608,7 +33618,7 @@ namespace ts {
3360833618
}
3360933619

3361033620
if (node.kind === SyntaxKind.BindingElement) {
33611-
if (node.propertyName && isIdentifier(node.name) && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
33621+
if (node.propertyName && isIdentifier(node.propertyName) && isIdentifier(node.name) && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
3361233622
// type F = ({a: string}) => void;
3361333623
// ^^^^^^
3361433624
// variable renaming in function type notation is confusing, so forbid it

src/compiler/transformers/declarations.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ namespace ts {
430430
return ret;
431431
}
432432

433-
function filterBindingPatternInitializers(name: BindingName) {
433+
function filterBindingPatternInitializersAndRenamings(name: BindingName) {
434434
if (name.kind === SyntaxKind.Identifier) {
435435
return name;
436436
}
@@ -448,7 +448,23 @@ namespace ts {
448448
if (elem.kind === SyntaxKind.OmittedExpression) {
449449
return elem;
450450
}
451-
return factory.updateBindingElement(elem, elem.dotDotDotToken, elem.propertyName, filterBindingPatternInitializers(elem.name), shouldPrintWithInitializer(elem) ? elem.initializer : undefined);
451+
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name)) {
452+
// Property renaming is forbidden in types, so remove renaming
453+
return factory.updateBindingElement(
454+
elem,
455+
elem.dotDotDotToken,
456+
/* propertyName */ undefined,
457+
elem.propertyName,
458+
shouldPrintWithInitializer(elem) ? elem.initializer : undefined
459+
);
460+
}
461+
return factory.updateBindingElement(
462+
elem,
463+
elem.dotDotDotToken,
464+
elem.propertyName,
465+
filterBindingPatternInitializersAndRenamings(elem.name),
466+
shouldPrintWithInitializer(elem) ? elem.initializer : undefined
467+
);
452468
}
453469
}
454470

@@ -463,7 +479,7 @@ namespace ts {
463479
/*decorators*/ undefined,
464480
maskModifiers(p, modifierMask),
465481
p.dotDotDotToken,
466-
filterBindingPatternInitializers(p.name),
482+
filterBindingPatternInitializersAndRenamings(p.name),
467483
resolver.isOptionalParameter(p) ? (p.questionToken || factory.createToken(SyntaxKind.QuestionToken)) : undefined,
468484
ensureType(p, type || p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param
469485
ensureNoInitializer(p)

tests/baselines/reference/declarationEmitBindingPatterns.js

Lines changed: 1 addition & 1 deletion
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: z }: {
21+
declare const k: ({ x }: {
2222
x?: string;
2323
}) => void;
2424
declare var a: any;

tests/baselines/reference/declarationEmitBindingPatterns.types

Lines changed: 2 additions & 2 deletions
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: z }: { x?: string; }) => void
4-
>({x: z = 'y'}) => { } : ({ x: z }: { x?: string; }) => void
3+
>k : ({ x }: { x?: string; }) => void
4+
>({x: z = 'y'}) => { } : ({ x }: { x?: string; }) => void
55
>x : any
66
>z : string
77
>'y' : "y"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
tests/cases/conformance/es6/destructuring/destructuringInFunctionType.ts(12,18): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
2+
tests/cases/conformance/es6/destructuring/destructuringInFunctionType.ts(12,28): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
3+
4+
5+
==== tests/cases/conformance/es6/destructuring/destructuringInFunctionType.ts (2 errors) ====
6+
interface a { a }
7+
interface b { b }
8+
interface c { c }
9+
10+
type T1 = ([a, b, c]);
11+
type F1 = ([a, b, c]) => void;
12+
13+
type T2 = ({ a });
14+
type F2 = ({ a }) => void;
15+
16+
type T3 = ([{ a: b }, { b: a }]);
17+
type F3 = ([{ a: b }, { b: a }]) => void;
18+
~
19+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
20+
~
21+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
22+
23+
type T4 = ([{ a: [b, c] }]);
24+
type F4 = ([{ a: [b, c] }]) => void;
25+
26+
type C1 = new ([{ a: [b, c] }]) => void;
27+
28+
var v1 = ([a, b, c]) => "hello";
29+
var v2: ([a, b, c]) => string;
30+

tests/baselines/reference/destructuringInFunctionType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ declare type T3 = ([{
5252
}, {
5353
b: a;
5454
}]);
55-
declare type F3 = ([{ a: b }, { b: a }]: [{
55+
declare type F3 = ([{ a }, { b }]: [{
5656
a: any;
5757
}, {
5858
b: any;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/excessPropertyCheckWithSpread.ts(1,25): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
2+
3+
4+
==== tests/cases/compiler/excessPropertyCheckWithSpread.ts (1 errors) ====
5+
declare function f({ a: number }): void
6+
~~~~~~
7+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
8+
interface I {
9+
readonly n: number;
10+
}
11+
declare let i: I;
12+
f({ a: 1, ...i });
13+
14+
interface R {
15+
opt?: number
16+
}
17+
interface L {
18+
opt: string
19+
}
20+
declare let l: L;
21+
declare let r: R;
22+
f({ a: 1, ...l, ...r });
23+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/paramterDestrcuturingDeclaration.ts(2,10): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
2+
tests/cases/compiler/paramterDestrcuturingDeclaration.ts(3,14): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
3+
4+
5+
==== tests/cases/compiler/paramterDestrcuturingDeclaration.ts (2 errors) ====
6+
interface C {
7+
({p: name}): any;
8+
~~~~
9+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
10+
new ({p: boolean}): any;
11+
~~~~~~~
12+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
13+
}
14+

tests/baselines/reference/paramterDestrcuturingDeclaration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ interface C {
1010

1111
//// [paramterDestrcuturingDeclaration.d.ts]
1212
interface C {
13-
({ p: name }: {
13+
({ p }: {
1414
p: any;
1515
}): any;
16-
new ({ p: boolean }: {
16+
new ({ p }: {
1717
p: any;
1818
}): any;
1919
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
tests/cases/conformance/jsx/file.tsx(17,39): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
2+
3+
4+
==== tests/cases/conformance/jsx/file.tsx (1 errors) ====
5+
import React = require('react')
6+
7+
declare function OneThing(k: {yxx: string}): JSX.Element;
8+
declare function OneThing(k: {yxx1: string, children: string}): JSX.Element;
9+
declare function OneThing(l: {yy: number, yy1: string}): JSX.Element;
10+
declare function OneThing(l: {yy: number, yy1: string, yy2: boolean}): JSX.Element;
11+
declare function OneThing(l1: {data: string, "data-prop": boolean}): JSX.Element;
12+
13+
// OK
14+
const c1 = <OneThing yxx='ok' />
15+
const c2 = <OneThing yy={100} yy1="hello"/>
16+
const c3 = <OneThing yxx="hello" ignore-prop />
17+
const c4 = <OneThing data="hello" data-prop />
18+
const c5 = <OneThing yxx1='ok'>Hello</OneThing>
19+
20+
21+
declare function TestingOneThing({y1: string}): JSX.Element;
22+
~~~~~~
23+
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
24+
declare function TestingOneThing(j: {"extra-data": string, yy?: string}): JSX.Element;
25+
declare function TestingOneThing(n: {yy: number, direction?: number}): JSX.Element;
26+
declare function TestingOneThing(n: {yy: string, name: string}): JSX.Element;
27+
28+
// OK
29+
const d1 = <TestingOneThing y1 extra-data />;
30+
const d2 = <TestingOneThing extra-data="hello" />;
31+
const d3 = <TestingOneThing extra-data="hello" yy="hihi" />;
32+
const d4 = <TestingOneThing extra-data="hello" yy={9} direction={10} />;
33+
const d5 = <TestingOneThing extra-data="hello" yy="hello" name="Bob" />;
34+
35+
36+
declare function TestingOptional(a: {y1?: string, y2?: number}): JSX.Element;
37+
declare function TestingOptional(a: {y1: boolean, y2?: number, y3: boolean}): JSX.Element;
38+
39+
// OK
40+
const e1 = <TestingOptional />
41+
const e3 = <TestingOptional y1="hello"/>
42+
const e4 = <TestingOptional y1="hello" y2={1000} />
43+
const e5 = <TestingOptional y1 y3/>
44+
const e6 = <TestingOptional y1 y3 y2={10} />
45+
const e2 = <TestingOptional y1 y3 extra-prop/>
46+
47+
48+

0 commit comments

Comments
 (0)