mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-14 22:29:17 +00:00
fix(minifier): mangle class private members in Node API (#26118)
## Summary Fixes #26097. The minify function did not work on class private fields,because their mappings were not forwarded to codegen: ```rs class Foo { #privateField = 1; method() { return this.#privateField; } } ``` And this PR is to make it match what's expected: ```rs class Foo { #e = 1; method() { return this.#e; } } ```
This commit is contained in:
1 parent
32bdc5b1d1
commit
affeb14f5a
2 files changed
+19
-1
No files matched your search
@@ -93,6 +93,7 @@ fn minify_impl(filename: &str, source_text: &str, options: Option<MinifyOptions>
|
||||
|
||||
let minifier_ret = Minifier::new(minifier_options).minify(&allocator, &mut program);
|
||||
let scoping = minifier_ret.scoping;
|
||||
let class_private_mappings = minifier_ret.class_private_mappings;
|
||||
let mangle_cache = parser_ret
|
||||
.diagnostics
|
||||
.is_empty()
|
||||
@@ -122,7 +123,11 @@ fn minify_impl(filename: &str, source_text: &str, options: Option<MinifyOptions>
|
||||
codegen_options.source_map_path = Some(PathBuf::from(&filename));
|
||||
}
|
||||
|
||||
let ret = Codegen::new().with_options(codegen_options).with_scoping(scoping).build(&program);
|
||||
let ret = Codegen::new()
|
||||
.with_options(codegen_options)
|
||||
.with_scoping(scoping)
|
||||
.with_private_member_mappings(class_private_mappings)
|
||||
.build(&program);
|
||||
|
||||
let legal_comments =
|
||||
ret.legal_comments.iter().map(|c| c.span.source_text(source_text).to_string()).collect();
|
||||
|
||||
@@ -140,6 +140,19 @@ describe("async minify", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("identifier mangling", () => {
|
||||
it("mangles class private members", () => {
|
||||
const ret = minifySync(
|
||||
"test.js",
|
||||
"class Foo { #privateField = 1; method() { return this.#privateField; } }",
|
||||
{ compress: false, mangle: true },
|
||||
);
|
||||
|
||||
expect(ret.errors).toHaveLength(0);
|
||||
expect(ret.code).toBe("class Foo{#e=1;method(){return this.#e}}");
|
||||
});
|
||||
});
|
||||
|
||||
describe("property mangling", () => {
|
||||
it("is independent from identifier mangling and returns a reusable cache", () => {
|
||||
const ret = minifySync("test.js", "let local; obj._often; obj._rare; obj._often;", {
|
||||
|
||||
Reference in new issue
Block a user