mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Remove renaming from declaration output
This commit is contained in:
1 parent
a37d6d471b
commit
67732d31e1
10 files changed
+160
-19
No files matched your search
+20
-10
@@ -5964,19 +5964,29 @@ namespace ts {
|
||||
return parameterNode;
|
||||
|
||||
function cloneBindingName(node: BindingName): BindingName {
|
||||
return elideInitializerAndSetEmitFlags(node) as BindingName;
|
||||
function elideInitializerAndSetEmitFlags(node: Node): Node {
|
||||
return elideInitializerAndPropertyRenamingAndSetEmitFlags(node) as BindingName;
|
||||
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);
|
||||
@@ -37905,7 +37915,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
if (isBindingElement(node)) {
|
||||
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
|
||||
|
||||
@@ -454,7 +454,7 @@ namespace ts {
|
||||
return ret;
|
||||
}
|
||||
|
||||
function filterBindingPatternInitializers(name: BindingName) {
|
||||
function filterBindingPatternInitializersAndRenamings(name: BindingName) {
|
||||
if (name.kind === SyntaxKind.Identifier) {
|
||||
return name;
|
||||
}
|
||||
@@ -472,7 +472,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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -487,7 +503,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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
@@ -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/>
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user