From b15b484d0fb0863fc862a8e7d205e4c6d5d24ac3 Mon Sep 17 00:00:00 2001 From: uhyo Date: Sun, 11 Oct 2020 20:15:03 +0900 Subject: [PATCH] Remove renaming from declaration output --- src/compiler/checker.ts | 30 ++++++++---- src/compiler/transformers/declarations.ts | 22 +++++++-- .../declarationEmitBindingPatterns.js | 2 +- .../declarationEmitBindingPatterns.types | 4 +- .../destructuringInFunctionType.errors.txt | 30 ++++++++++++ .../reference/destructuringInFunctionType.js | 2 +- .../excessPropertyCheckWithSpread.errors.txt | 23 +++++++++ ...aramterDestrcuturingDeclaration.errors.txt | 14 ++++++ .../paramterDestrcuturingDeclaration.js | 4 +- ...elessFunctionComponentOverload1.errors.txt | 48 +++++++++++++++++++ 10 files changed, 160 insertions(+), 19 deletions(-) create mode 100644 tests/baselines/reference/destructuringInFunctionType.errors.txt create mode 100644 tests/baselines/reference/excessPropertyCheckWithSpread.errors.txt create mode 100644 tests/baselines/reference/paramterDestrcuturingDeclaration.errors.txt create mode 100644 tests/baselines/reference/tsxStatelessFunctionComponentOverload1.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58bee03360fa8..c95c222e186a7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5267,19 +5267,29 @@ namespace ts { return parameterNode; function cloneBindingName(node: BindingName): BindingName { - return elideInitializerAndSetEmitFlags(node); - function elideInitializerAndSetEmitFlags(node: Node): Node { + return 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); @@ -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 diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index 43539cb06ad71..d6600a46d649f 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -430,7 +430,7 @@ namespace ts { return ret; } - function filterBindingPatternInitializers(name: BindingName) { + function filterBindingPatternInitializersAndRenamings(name: BindingName) { if (name.kind === SyntaxKind.Identifier) { return name; } @@ -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 + ); } } @@ -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) diff --git a/tests/baselines/reference/declarationEmitBindingPatterns.js b/tests/baselines/reference/declarationEmitBindingPatterns.js index a8550f5473809..ebe6d50e0652f 100644 --- a/tests/baselines/reference/declarationEmitBindingPatterns.js +++ b/tests/baselines/reference/declarationEmitBindingPatterns.js @@ -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; diff --git a/tests/baselines/reference/declarationEmitBindingPatterns.types b/tests/baselines/reference/declarationEmitBindingPatterns.types index d49893dad94fa..151991e3039fb 100644 --- a/tests/baselines/reference/declarationEmitBindingPatterns.types +++ b/tests/baselines/reference/declarationEmitBindingPatterns.types @@ -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" diff --git a/tests/baselines/reference/destructuringInFunctionType.errors.txt b/tests/baselines/reference/destructuringInFunctionType.errors.txt new file mode 100644 index 0000000000000..da7a2fc32bbf1 --- /dev/null +++ b/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; + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringInFunctionType.js b/tests/baselines/reference/destructuringInFunctionType.js index 1844e8e5a8d21..1a38ef067acfc 100644 --- a/tests/baselines/reference/destructuringInFunctionType.js +++ b/tests/baselines/reference/destructuringInFunctionType.js @@ -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; diff --git a/tests/baselines/reference/excessPropertyCheckWithSpread.errors.txt b/tests/baselines/reference/excessPropertyCheckWithSpread.errors.txt new file mode 100644 index 0000000000000..9ae33a275805d --- /dev/null +++ b/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 }); + \ No newline at end of file diff --git a/tests/baselines/reference/paramterDestrcuturingDeclaration.errors.txt b/tests/baselines/reference/paramterDestrcuturingDeclaration.errors.txt new file mode 100644 index 0000000000000..69e5d888a1b0c --- /dev/null +++ b/tests/baselines/reference/paramterDestrcuturingDeclaration.errors.txt @@ -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. + } + \ No newline at end of file diff --git a/tests/baselines/reference/paramterDestrcuturingDeclaration.js b/tests/baselines/reference/paramterDestrcuturingDeclaration.js index 3ce2527f57226..e0cf710a528da 100644 --- a/tests/baselines/reference/paramterDestrcuturingDeclaration.js +++ b/tests/baselines/reference/paramterDestrcuturingDeclaration.js @@ -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; } diff --git a/tests/baselines/reference/tsxStatelessFunctionComponentOverload1.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponentOverload1.errors.txt new file mode 100644 index 0000000000000..69c6d7d79e3c2 --- /dev/null +++ b/tests/baselines/reference/tsxStatelessFunctionComponentOverload1.errors.txt @@ -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 = + const c2 = + const c3 = + const c4 = + const c5 = Hello + + + 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 = ; + const d2 = ; + const d3 = ; + const d4 = ; + const d5 = ; + + + 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 = + const e3 = + const e4 = + const e5 = + const e6 = + const e2 = + + + \ No newline at end of file