-
Notifications
You must be signed in to change notification settings - Fork 13k
Fixed serialization of param names in setters from JS files #55393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9e9f03b
dbbe67f
e603c7d
30a3f56
014f3d9
68368f4
18fb528
13ef021
05cc089
9509273
202eff7
ebfcbc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7594,11 +7594,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||||||||||||||||||||||||||||
return typeParameterToDeclarationWithConstraint(type, context, constraintNode); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function symbolToParameterDeclaration(parameterSymbol: Symbol, context: NodeBuilderContext, preserveModifierFlags?: boolean, privateSymbolVisitor?: (s: Symbol) => void, bundledImports?: boolean): ParameterDeclaration { | ||||||||||||||||||||||||||||||
let parameterDeclaration: ParameterDeclaration | JSDocParameterTag | undefined = getDeclarationOfKind<ParameterDeclaration>(parameterSymbol, SyntaxKind.Parameter); | ||||||||||||||||||||||||||||||
if (!parameterDeclaration && !isTransientSymbol(parameterSymbol)) { | ||||||||||||||||||||||||||||||
parameterDeclaration = getDeclarationOfKind<JSDocParameterTag>(parameterSymbol, SyntaxKind.JSDocParameterTag); | ||||||||||||||||||||||||||||||
function getEffectiveParameterDeclaration(parameterSymbol: Symbol): ParameterDeclaration | JSDocParameterTag | undefined { | ||||||||||||||||||||||||||||||
const parameterDeclaration: ParameterDeclaration | JSDocParameterTag | undefined = getDeclarationOfKind<ParameterDeclaration>(parameterSymbol, SyntaxKind.Parameter); | ||||||||||||||||||||||||||||||
if (parameterDeclaration) { | ||||||||||||||||||||||||||||||
return parameterDeclaration; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
if (!isTransientSymbol(parameterSymbol)) { | ||||||||||||||||||||||||||||||
return getDeclarationOfKind<JSDocParameterTag>(parameterSymbol, SyntaxKind.JSDocParameterTag); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function symbolToParameterDeclaration(parameterSymbol: Symbol, context: NodeBuilderContext, preserveModifierFlags?: boolean, privateSymbolVisitor?: (s: Symbol) => void, bundledImports?: boolean): ParameterDeclaration { | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to extract some useful bits to separate helpers instead of reusing
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
const parameterDeclaration = getEffectiveParameterDeclaration(parameterSymbol); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
let parameterType = getTypeOfSymbol(parameterSymbol); | ||||||||||||||||||||||||||||||
if (parameterDeclaration && isRequiredInitializedParameter(parameterDeclaration)) { | ||||||||||||||||||||||||||||||
|
@@ -7609,12 +7616,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||||||||||||||||||||||||||||
const modifiers = !(context.flags & NodeBuilderFlags.OmitParameterModifiers) && preserveModifierFlags && parameterDeclaration && canHaveModifiers(parameterDeclaration) ? map(getModifiers(parameterDeclaration), factory.cloneNode) : undefined; | ||||||||||||||||||||||||||||||
const isRest = parameterDeclaration && isRestParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.RestParameter; | ||||||||||||||||||||||||||||||
const dotDotDotToken = isRest ? factory.createToken(SyntaxKind.DotDotDotToken) : undefined; | ||||||||||||||||||||||||||||||
const name = parameterDeclaration ? parameterDeclaration.name ? | ||||||||||||||||||||||||||||||
parameterDeclaration.name.kind === SyntaxKind.Identifier ? setEmitFlags(factory.cloneNode(parameterDeclaration.name), EmitFlags.NoAsciiEscaping) : | ||||||||||||||||||||||||||||||
parameterDeclaration.name.kind === SyntaxKind.QualifiedName ? setEmitFlags(factory.cloneNode(parameterDeclaration.name.right), EmitFlags.NoAsciiEscaping) : | ||||||||||||||||||||||||||||||
cloneBindingName(parameterDeclaration.name) : | ||||||||||||||||||||||||||||||
symbolName(parameterSymbol) : | ||||||||||||||||||||||||||||||
symbolName(parameterSymbol); | ||||||||||||||||||||||||||||||
const name = parameterToParameterDeclarationName(parameterSymbol, parameterDeclaration, context); | ||||||||||||||||||||||||||||||
const isOptional = parameterDeclaration && isOptionalParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.OptionalParameter; | ||||||||||||||||||||||||||||||
const questionToken = isOptional ? factory.createToken(SyntaxKind.QuestionToken) : undefined; | ||||||||||||||||||||||||||||||
const parameterNode = factory.createParameterDeclaration( | ||||||||||||||||||||||||||||||
|
@@ -7627,6 +7629,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||
context.approximateLength += symbolName(parameterSymbol).length + 3; | ||||||||||||||||||||||||||||||
return parameterNode; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function parameterToParameterDeclarationName(parameterSymbol: Symbol, parameterDeclaration: ParameterDeclaration | JSDocParameterTag | undefined, context: NodeBuilderContext) { | ||||||||||||||||||||||||||||||
return parameterDeclaration ? parameterDeclaration.name ? | ||||||||||||||||||||||||||||||
parameterDeclaration.name.kind === SyntaxKind.Identifier ? setEmitFlags(factory.cloneNode(parameterDeclaration.name), EmitFlags.NoAsciiEscaping) : | ||||||||||||||||||||||||||||||
parameterDeclaration.name.kind === SyntaxKind.QualifiedName ? setEmitFlags(factory.cloneNode(parameterDeclaration.name.right), EmitFlags.NoAsciiEscaping) : | ||||||||||||||||||||||||||||||
cloneBindingName(parameterDeclaration.name) : | ||||||||||||||||||||||||||||||
symbolName(parameterSymbol) : | ||||||||||||||||||||||||||||||
jakebailey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
symbolName(parameterSymbol); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function cloneBindingName(node: BindingName): BindingName { | ||||||||||||||||||||||||||||||
return elideInitializerAndSetEmitFlags(node) as BindingName; | ||||||||||||||||||||||||||||||
|
@@ -9779,14 +9790,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||||||||||||||||||||||||||||
if (p.flags & SymbolFlags.Accessor && useAccessors) { | ||||||||||||||||||||||||||||||
const result: AccessorDeclaration[] = []; | ||||||||||||||||||||||||||||||
if (p.flags & SymbolFlags.SetAccessor) { | ||||||||||||||||||||||||||||||
const setter = p.declarations && forEach(p.declarations, d => { | ||||||||||||||||||||||||||||||
if (d.kind === SyntaxKind.SetAccessor) { | ||||||||||||||||||||||||||||||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
return d as SetAccessorDeclaration; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
if (isCallExpression(d) && isBindableObjectDefinePropertyCall(d)) { | ||||||||||||||||||||||||||||||
return forEach(d.arguments[2].properties, propDecl => { | ||||||||||||||||||||||||||||||
const id = getNameOfDeclaration(propDecl); | ||||||||||||||||||||||||||||||
if (!!id && isIdentifier(id) && idText(id) === "set") { | ||||||||||||||||||||||||||||||
return propDecl; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Debug.assert(setter && isFunctionLikeDeclaration(setter)); | ||||||||||||||||||||||||||||||
const paramSymbol: Symbol | undefined = getSignatureFromDeclaration(setter).parameters[0]; | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This annotation isn't enforced, it acts as a documentation hint~. The problem is that indexed access here won't return |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
result.push(setTextRange( | ||||||||||||||||||||||||||||||
factory.createSetAccessorDeclaration( | ||||||||||||||||||||||||||||||
factory.createModifiersFromModifierFlags(flag), | ||||||||||||||||||||||||||||||
name, | ||||||||||||||||||||||||||||||
[factory.createParameterDeclaration( | ||||||||||||||||||||||||||||||
/*modifiers*/ undefined, | ||||||||||||||||||||||||||||||
/*dotDotDotToken*/ undefined, | ||||||||||||||||||||||||||||||
"arg", | ||||||||||||||||||||||||||||||
paramSymbol ? parameterToParameterDeclarationName(paramSymbol, getEffectiveParameterDeclaration(paramSymbol), context) : "value", | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to use TypeScript/src/compiler/transformers/declarations.ts Lines 897 to 903 in 202eff7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to use TypeScript/src/compiler/transformers/declarations.ts Lines 897 to 903 in 202eff7
|
||||||||||||||||||||||||||||||
/*questionToken*/ undefined, | ||||||||||||||||||||||||||||||
isPrivate ? undefined : serializeTypeForDeclaration(context, getTypeOfSymbol(p), p, enclosingDeclaration, includePrivateSymbol, bundled), | ||||||||||||||||||||||||||||||
)], | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs.ts] //// | ||
|
||
//// [foo.js] | ||
// https://github.com/microsoft/TypeScript/issues/55391 | ||
|
||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {string} baz Baz. | ||
*/ | ||
set bar(baz) {} | ||
} | ||
|
||
|
||
|
||
|
||
//// [foo.d.ts] | ||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {string} baz Baz. | ||
*/ | ||
set bar(baz: string); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs.ts] //// | ||
|
||
=== foo.js === | ||
// https://github.com/microsoft/TypeScript/issues/55391 | ||
|
||
export class Foo { | ||
>Foo : Symbol(Foo, Decl(foo.js, 0, 0)) | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {string} baz Baz. | ||
*/ | ||
set bar(baz) {} | ||
>bar : Symbol(Foo.bar, Decl(foo.js, 2, 18)) | ||
>baz : Symbol(baz, Decl(foo.js, 8, 12)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs.ts] //// | ||
|
||
=== foo.js === | ||
// https://github.com/microsoft/TypeScript/issues/55391 | ||
|
||
export class Foo { | ||
>Foo : Foo | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {string} baz Baz. | ||
*/ | ||
set bar(baz) {} | ||
>bar : string | ||
>baz : string | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs2.ts] //// | ||
|
||
//// [foo.js] | ||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string }} baz Baz. | ||
*/ | ||
set bar({}) {} | ||
} | ||
|
||
|
||
|
||
|
||
//// [foo.d.ts] | ||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string }} baz Baz. | ||
*/ | ||
set bar({}: { | ||
prop: string; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs2.ts] //// | ||
|
||
=== foo.js === | ||
export class Foo { | ||
>Foo : Symbol(Foo, Decl(foo.js, 0, 0)) | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string }} baz Baz. | ||
*/ | ||
set bar({}) {} | ||
>bar : Symbol(Foo.bar, Decl(foo.js, 0, 18)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs2.ts] //// | ||
|
||
=== foo.js === | ||
export class Foo { | ||
>Foo : Foo | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string }} baz Baz. | ||
*/ | ||
set bar({}) {} | ||
>bar : { prop: string; } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs3.ts] //// | ||
|
||
//// [foo.js] | ||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string | undefined }} baz Baz. | ||
*/ | ||
set bar({ prop = 'foo' }) {} | ||
} | ||
|
||
|
||
|
||
|
||
//// [foo.d.ts] | ||
export class Foo { | ||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string | undefined }} baz Baz. | ||
*/ | ||
set bar({ prop }: { | ||
prop: string | undefined; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs3.ts] //// | ||
|
||
=== foo.js === | ||
export class Foo { | ||
>Foo : Symbol(Foo, Decl(foo.js, 0, 0)) | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string | undefined }} baz Baz. | ||
*/ | ||
set bar({ prop = 'foo' }) {} | ||
>bar : Symbol(Foo.bar, Decl(foo.js, 0, 18)) | ||
>prop : Symbol(prop, Decl(foo.js, 6, 13)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//// [tests/cases/compiler/declarationEmitClassSetAccessorParamNameInJs3.ts] //// | ||
|
||
=== foo.js === | ||
export class Foo { | ||
>Foo : Foo | ||
|
||
/** | ||
* Bar. | ||
* | ||
* @param {{ prop: string | undefined }} baz Baz. | ||
*/ | ||
set bar({ prop = 'foo' }) {} | ||
>bar : { prop: string | undefined; } | ||
>prop : string | ||
>'foo' : "foo" | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.