Skip to content

Commit

Permalink
Remove renaming from declaration output
Browse files Browse the repository at this point in the history
  • Loading branch information
uhyo committed Oct 11, 2020
1 parent e360400 commit b15b484
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 19 deletions.
30 changes: 20 additions & 10 deletions src/compiler/checker.ts
Expand Up @@ -5267,19 +5267,29 @@ namespace ts {
return parameterNode;

function cloneBindingName(node: BindingName): BindingName {
return <BindingName>elideInitializerAndSetEmitFlags(node);
function elideInitializerAndSetEmitFlags(node: Node): Node {
return <BindingName>elideInitializerAndPropertyRenamingAndSetEmitFlags(node);
function elideInitializerAndPropertyRenamingAndSetEmitFlags(node: Node): Node {
if (context.tracker.trackSymbol && isComputedPropertyName(node) && isLateBindableName(node)) {
trackComputedName(node.expression, context.enclosingDeclaration, context);
}
let visited = visitEachChild(node, elideInitializerAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndSetEmitFlags)!;
let visited = visitEachChild(node, elideInitializerAndPropertyRenamingAndSetEmitFlags, nullTransformationContext, /*nodesVisitor*/ undefined, elideInitializerAndPropertyRenamingAndSetEmitFlags)!;
if (isBindingElement(visited)) {
visited = factory.updateBindingElement(
visited,
visited.dotDotDotToken,
visited.propertyName,
visited.name,
/*initializer*/ undefined);
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);
}
}
if (!nodeIsSynthesized(visited)) {
visited = factory.cloneNode(visited);
Expand Down Expand Up @@ -33608,7 +33618,7 @@ namespace ts {
}

if (node.kind === SyntaxKind.BindingElement) {
if (node.propertyName && isIdentifier(node.name) && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
if (node.propertyName && isIdentifier(node.propertyName) && isIdentifier(node.name) && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
// type F = ({a: string}) => void;
// ^^^^^^
// variable renaming in function type notation is confusing, so forbid it
Expand Down
22 changes: 19 additions & 3 deletions src/compiler/transformers/declarations.ts
Expand Up @@ -430,7 +430,7 @@ namespace ts {
return ret;
}

function filterBindingPatternInitializers(name: BindingName) {
function filterBindingPatternInitializersAndRenamings(name: BindingName) {
if (name.kind === SyntaxKind.Identifier) {
return name;
}
Expand All @@ -448,7 +448,23 @@ namespace ts {
if (elem.kind === SyntaxKind.OmittedExpression) {
return elem;
}
return factory.updateBindingElement(elem, elem.dotDotDotToken, elem.propertyName, filterBindingPatternInitializers(elem.name), shouldPrintWithInitializer(elem) ? elem.initializer : undefined);
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name)) {
// Property renaming is forbidden in types, so remove renaming
return factory.updateBindingElement(
elem,
elem.dotDotDotToken,
/* propertyName */ undefined,
elem.propertyName,
shouldPrintWithInitializer(elem) ? elem.initializer : undefined
);
}
return factory.updateBindingElement(
elem,
elem.dotDotDotToken,
elem.propertyName,
filterBindingPatternInitializersAndRenamings(elem.name),
shouldPrintWithInitializer(elem) ? elem.initializer : undefined
);
}
}

Expand All @@ -463,7 +479,7 @@ namespace ts {
/*decorators*/ undefined,
maskModifiers(p, modifierMask),
p.dotDotDotToken,
filterBindingPatternInitializers(p.name),
filterBindingPatternInitializersAndRenamings(p.name),
resolver.isOptionalParameter(p) ? (p.questionToken || factory.createToken(SyntaxKind.QuestionToken)) : undefined,
ensureType(p, type || p.type, /*ignorePrivate*/ true), // Ignore private param props, since this type is going straight back into a param
ensureNoInitializer(p)
Expand Down
Expand Up @@ -18,7 +18,7 @@ function f(_a, _b, _c) {


//// [declarationEmitBindingPatterns.d.ts]
declare const k: ({ x: z }: {
declare const k: ({ x }: {
x?: string;
}) => void;
declare var a: any;
Expand Down
@@ -1,7 +1,7 @@
=== tests/cases/compiler/declarationEmitBindingPatterns.ts ===
const k = ({x: z = 'y'}) => { }
>k : ({ x: z }: { x?: string; }) => void
>({x: z = 'y'}) => { } : ({ x: z }: { x?: string; }) => void
>k : ({ x }: { x?: string; }) => void
>({x: z = 'y'}) => { } : ({ x }: { x?: string; }) => void
>x : any
>z : string
>'y' : "y"
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/destructuringInFunctionType.errors.txt
@@ -0,0 +1,30 @@
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.
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.


==== tests/cases/conformance/es6/destructuring/destructuringInFunctionType.ts (2 errors) ====
interface a { a }
interface b { b }
interface c { c }

type T1 = ([a, b, c]);
type F1 = ([a, b, c]) => void;

type T2 = ({ a });
type F2 = ({ a }) => void;

type T3 = ([{ a: b }, { b: a }]);
type F3 = ([{ a: b }, { b: a }]) => void;
~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.

type T4 = ([{ a: [b, c] }]);
type F4 = ([{ a: [b, c] }]) => void;

type C1 = new ([{ a: [b, c] }]) => void;

var v1 = ([a, b, c]) => "hello";
var v2: ([a, b, c]) => string;

2 changes: 1 addition & 1 deletion tests/baselines/reference/destructuringInFunctionType.js
Expand Up @@ -52,7 +52,7 @@ declare type T3 = ([{
}, {
b: a;
}]);
declare type F3 = ([{ a: b }, { b: a }]: [{
declare type F3 = ([{ a }, { b }]: [{
a: any;
}, {
b: any;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/excessPropertyCheckWithSpread.errors.txt
@@ -0,0 +1,23 @@
tests/cases/compiler/excessPropertyCheckWithSpread.ts(1,25): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.


==== tests/cases/compiler/excessPropertyCheckWithSpread.ts (1 errors) ====
declare function f({ a: number }): void
~~~~~~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
interface I {
readonly n: number;
}
declare let i: I;
f({ a: 1, ...i });

interface R {
opt?: number
}
interface L {
opt: string
}
declare let l: L;
declare let r: R;
f({ a: 1, ...l, ...r });

@@ -0,0 +1,14 @@
tests/cases/compiler/paramterDestrcuturingDeclaration.ts(2,10): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
tests/cases/compiler/paramterDestrcuturingDeclaration.ts(3,14): error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.


==== tests/cases/compiler/paramterDestrcuturingDeclaration.ts (2 errors) ====
interface C {
({p: name}): any;
~~~~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
new ({p: boolean}): any;
~~~~~~~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/paramterDestrcuturingDeclaration.js
Expand Up @@ -10,10 +10,10 @@ interface C {

//// [paramterDestrcuturingDeclaration.d.ts]
interface C {
({ p: name }: {
({ p }: {
p: any;
}): any;
new ({ p: boolean }: {
new ({ p }: {
p: any;
}): any;
}
@@ -0,0 +1,48 @@
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.


==== tests/cases/conformance/jsx/file.tsx (1 errors) ====
import React = require('react')

declare function OneThing(k: {yxx: string}): JSX.Element;
declare function OneThing(k: {yxx1: string, children: string}): JSX.Element;
declare function OneThing(l: {yy: number, yy1: string}): JSX.Element;
declare function OneThing(l: {yy: number, yy1: string, yy2: boolean}): JSX.Element;
declare function OneThing(l1: {data: string, "data-prop": boolean}): JSX.Element;

// OK
const c1 = <OneThing yxx='ok' />
const c2 = <OneThing yy={100} yy1="hello"/>
const c3 = <OneThing yxx="hello" ignore-prop />
const c4 = <OneThing data="hello" data-prop />
const c5 = <OneThing yxx1='ok'>Hello</OneThing>


declare function TestingOneThing({y1: string}): JSX.Element;
~~~~~~
!!! error TS2797: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.
declare function TestingOneThing(j: {"extra-data": string, yy?: string}): JSX.Element;
declare function TestingOneThing(n: {yy: number, direction?: number}): JSX.Element;
declare function TestingOneThing(n: {yy: string, name: string}): JSX.Element;

// OK
const d1 = <TestingOneThing y1 extra-data />;
const d2 = <TestingOneThing extra-data="hello" />;
const d3 = <TestingOneThing extra-data="hello" yy="hihi" />;
const d4 = <TestingOneThing extra-data="hello" yy={9} direction={10} />;
const d5 = <TestingOneThing extra-data="hello" yy="hello" name="Bob" />;


declare function TestingOptional(a: {y1?: string, y2?: number}): JSX.Element;
declare function TestingOptional(a: {y1: boolean, y2?: number, y3: boolean}): JSX.Element;

// OK
const e1 = <TestingOptional />
const e3 = <TestingOptional y1="hello"/>
const e4 = <TestingOptional y1="hello" y2={1000} />
const e5 = <TestingOptional y1 y3/>
const e6 = <TestingOptional y1 y3 y2={10} />
const e2 = <TestingOptional y1 y3 extra-prop/>



0 comments on commit b15b484

Please sign in to comment.