From dcfd2676db8d44fab5caae2ec4e326d5135a652e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 15 Jan 2025 16:05:57 -0800 Subject: [PATCH] Filter #private static members from js declaration emit --- src/compiler/checker.ts | 3 +- .../declarationEmitJsPrivateClassStatics.js | 20 ++++++++++++ ...clarationEmitJsPrivateClassStatics.symbols | 19 +++++++++++ ...declarationEmitJsPrivateClassStatics.types | 32 +++++++++++++++++++ .../declarationEmitJsPrivateClassStatics.ts | 14 ++++++++ 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/declarationEmitJsPrivateClassStatics.js create mode 100644 tests/baselines/reference/declarationEmitJsPrivateClassStatics.symbols create mode 100644 tests/baselines/reference/declarationEmitJsPrivateClassStatics.types create mode 100644 tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 80b61edd3657a..6337599acfd1b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1133,6 +1133,7 @@ import { WithStatement, WriterContextOut, YieldExpression, + isPrivateIdentifierSymbol, } from "./_namespaces/ts.js"; import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers.js"; import * as performance from "./_namespaces/ts.performance.js"; @@ -9770,7 +9771,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const publicProperties = flatMap(publicSymbolProps, p => serializePropertySymbolForClass(p, /*isStatic*/ false, baseTypes[0])); // Consider static members empty if symbol also has function or module meaning - function namespacey emit will handle statics const staticMembers = flatMap( - filter(getPropertiesOfType(staticType), p => !(p.flags & SymbolFlags.Prototype) && p.escapedName !== "prototype" && !isNamespaceMember(p)), + filter(getPropertiesOfType(staticType), p => !(p.flags & SymbolFlags.Prototype) && p.escapedName !== "prototype" && !isNamespaceMember(p) && !isPrivateIdentifierSymbol(p)), p => serializePropertySymbolForClass(p, /*isStatic*/ true, staticBaseType), ); // When we encounter an `X.prototype.y` assignment in a JS file, we bind `X` as a class regardless as to whether diff --git a/tests/baselines/reference/declarationEmitJsPrivateClassStatics.js b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.js new file mode 100644 index 0000000000000..d5026deecc5b9 --- /dev/null +++ b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.js @@ -0,0 +1,20 @@ +//// [tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts] //// + +//// [input.js] +class Test { + static #privateStaticProp = 'Not removed for JS declaration generation; is removed for TS.'; + + static #privateStaticFn() { return 'Not removed for JS declaration generation; is removed for TS.'; } + + #privateProp = 'This is removed in JS / TS declaration generation.'; + + #privateFn() { return 'This is removed in JS / TS declaration generation.'; } +} + + + + +//// [input.d.ts] +declare class Test { + #private; +} diff --git a/tests/baselines/reference/declarationEmitJsPrivateClassStatics.symbols b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.symbols new file mode 100644 index 0000000000000..cc54d09cd4355 --- /dev/null +++ b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.symbols @@ -0,0 +1,19 @@ +//// [tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts] //// + +=== input.js === +class Test { +>Test : Symbol(Test, Decl(input.js, 0, 0)) + + static #privateStaticProp = 'Not removed for JS declaration generation; is removed for TS.'; +>#privateStaticProp : Symbol(Test.#privateStaticProp, Decl(input.js, 0, 12)) + + static #privateStaticFn() { return 'Not removed for JS declaration generation; is removed for TS.'; } +>#privateStaticFn : Symbol(Test.#privateStaticFn, Decl(input.js, 1, 96)) + + #privateProp = 'This is removed in JS / TS declaration generation.'; +>#privateProp : Symbol(Test.#privateProp, Decl(input.js, 3, 105)) + + #privateFn() { return 'This is removed in JS / TS declaration generation.'; } +>#privateFn : Symbol(Test.#privateFn, Decl(input.js, 5, 72)) +} + diff --git a/tests/baselines/reference/declarationEmitJsPrivateClassStatics.types b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.types new file mode 100644 index 0000000000000..fd58ad8ece9df --- /dev/null +++ b/tests/baselines/reference/declarationEmitJsPrivateClassStatics.types @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts] //// + +=== input.js === +class Test { +>Test : Test +> : ^^^^ + + static #privateStaticProp = 'Not removed for JS declaration generation; is removed for TS.'; +>#privateStaticProp : string +> : ^^^^^^ +>'Not removed for JS declaration generation; is removed for TS.' : "Not removed for JS declaration generation; is removed for TS." +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + static #privateStaticFn() { return 'Not removed for JS declaration generation; is removed for TS.'; } +>#privateStaticFn : () => string +> : ^^^^^^^^^^^^ +>'Not removed for JS declaration generation; is removed for TS.' : "Not removed for JS declaration generation; is removed for TS." +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + #privateProp = 'This is removed in JS / TS declaration generation.'; +>#privateProp : string +> : ^^^^^^ +>'This is removed in JS / TS declaration generation.' : "This is removed in JS / TS declaration generation." +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + #privateFn() { return 'This is removed in JS / TS declaration generation.'; } +>#privateFn : () => string +> : ^^^^^^^^^^^^ +>'This is removed in JS / TS declaration generation.' : "This is removed in JS / TS declaration generation." +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +} + diff --git a/tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts b/tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts new file mode 100644 index 0000000000000..e1120a0332016 --- /dev/null +++ b/tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts @@ -0,0 +1,14 @@ +// @allowJs: true +// @emitDeclarationOnly: true +// @declaration: true +// @target: es2015 +// @filename: input.js +class Test { + static #privateStaticProp = 'Not removed for JS declaration generation; is removed for TS.'; + + static #privateStaticFn() { return 'Not removed for JS declaration generation; is removed for TS.'; } + + #privateProp = 'This is removed in JS / TS declaration generation.'; + + #privateFn() { return 'This is removed in JS / TS declaration generation.'; } +}