Skip to content

Commit

Permalink
Filter #private static members from js declaration emit
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Jan 16, 2025
1 parent 700ee07 commit dcfd267
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -9770,7 +9771,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const publicProperties = flatMap<Symbol, ClassElement>(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
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/declarationEmitJsPrivateClassStatics.js
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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))
}

Original file line number Diff line number Diff line change
@@ -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."
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

14 changes: 14 additions & 0 deletions tests/cases/compiler/declarationEmitJsPrivateClassStatics.ts
Original file line number Diff line number Diff line change
@@ -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.'; }
}

0 comments on commit dcfd267

Please sign in to comment.