-
-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): remove private fields (#3514)
* fix(build): remove private fields * chore: format scripts * test: add test code * reduce package.json diff * feat(build): add progress for removing private fields * feat: rename `scripts` to `build`
- Loading branch information
Showing
7 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference types="vitest/globals" /> | ||
|
||
import { removePrivateFields } from './remove-private-fields' | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import os from 'node:os' | ||
|
||
describe('removePrivateFields', () => { | ||
it('Works', async () => { | ||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'removePrivateFields')) | ||
const tsPath = path.join(tmpDir, 'class.ts') | ||
await fs.writeFile(tsPath, 'class X { #private: number = 0; a: number = 0 }') | ||
expect(removePrivateFields(tsPath)).toBe(`class X { | ||
a: number = 0; | ||
} | ||
`) | ||
}) | ||
it('Should throw error when path does not exist', () => { | ||
expect(() => removePrivateFields('./unknown.ts')).toThrowError(Error) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as ts from 'typescript' | ||
|
||
const removePrivateTransformer = <T extends ts.Node>(ctx: ts.TransformationContext) => { | ||
const visit: ts.Visitor = (node) => { | ||
if (ts.isClassDeclaration(node)) { | ||
const newMembers = node.members.filter((elem) => { | ||
if (ts.isPropertyDeclaration(elem) || ts.isMethodDeclaration(elem)) { | ||
for (const modifier of elem.modifiers ?? []) { | ||
if (modifier.kind === ts.SyntaxKind.PrivateKeyword) { | ||
return false | ||
} | ||
} | ||
} | ||
if (elem.name && ts.isPrivateIdentifier(elem.name)) { | ||
return false | ||
} | ||
return true | ||
}) | ||
return ts.factory.createClassDeclaration( | ||
node.modifiers, | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
newMembers | ||
) | ||
} | ||
return ts.visitEachChild(node, visit, ctx) | ||
} | ||
|
||
return (node: T) => { | ||
const visited = ts.visitNode(node, visit) | ||
if (!visited) { | ||
throw new Error('The result visited is undefined.') | ||
} | ||
return visited | ||
} | ||
} | ||
|
||
export const removePrivateFields = (tsPath: string) => { | ||
const program = ts.createProgram([tsPath], { | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.ESNext, | ||
}) | ||
const file = program.getSourceFile(tsPath) | ||
|
||
const transformed = ts.transform(file!, [removePrivateTransformer]) | ||
const printer = ts.createPrinter() | ||
const transformedSourceFile = transformed.transformed[0] as ts.SourceFile | ||
const code = printer.printFile(transformedSourceFile) | ||
transformed.dispose() | ||
return code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters