fix(codegen): print negative bigint 1n- -1n correctly after constant folding (#6798)

closes #6767
This commit is contained in:
Boshen 2024-10-23 04:56:50 +00:00
parent b4bc300ebf
commit 2f6ad42348
6 changed files with 35 additions and 9 deletions

View file

@ -1,5 +1,5 @@
[jobs.example]
command = ["just", "run-example"]
command = ["just", "example"]
allow_warnings = true
need_stdout = true

View file

@ -1147,6 +1147,10 @@ impl<'a> GenExpr for NumericLiteral<'a> {
impl<'a> Gen for BigIntLiteral<'a> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
if self.raw.starts_with('-') {
p.print_space_before_operator(Operator::Unary(UnaryOperator::UnaryNegation));
}
p.print_space_before_identifier();
p.add_source_mapping(self.span.start);
p.print_str(self.raw.as_str());
}

View file

@ -601,6 +601,12 @@ mod test {
tester::test(&allocator, source_text, expected, &mut pass);
}
fn test_nospace(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeFoldConstants::new();
tester::test_impl(&allocator, source_text, expected, &mut pass, true);
}
fn test_same(source_text: &str) {
test(source_text, source_text);
}
@ -1312,6 +1318,10 @@ mod test {
test("x = 0 / 0", "x = NaN");
test("x = 0 % 0", "x = NaN");
test("x = (-1) ** 0.5", "x = NaN");
test_nospace("1n+ +1n", "1n + +1n");
test_nospace("1n- -1n", "1n - -1n");
test_nospace("a- -b", "a - -b");
}
#[test]

View file

@ -13,8 +13,18 @@ pub fn test<'a, P: CompressorPass<'a>>(
expected: &'a str,
pass: &mut P,
) {
let result = run(allocator, source_text, Some(pass));
let expected = run::<P>(allocator, expected, None);
test_impl(allocator, source_text, expected, pass, false);
}
pub fn test_impl<'a, P: CompressorPass<'a>>(
allocator: &'a Allocator,
source_text: &'a str,
expected: &'a str,
pass: &mut P,
remove_whitespace: bool,
) {
let result = run(allocator, source_text, Some(pass), remove_whitespace);
let expected = run::<P>(allocator, expected, None, remove_whitespace);
assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}");
}
@ -22,6 +32,7 @@ fn run<'a, P: CompressorPass<'a>>(
allocator: &'a Allocator,
source_text: &'a str,
pass: Option<&mut P>,
remove_whitespace: bool,
) -> String {
let source_type = SourceType::mjs();
let mut program = Parser::new(allocator, source_text, source_type).parse().program;
@ -35,7 +46,11 @@ fn run<'a, P: CompressorPass<'a>>(
}
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.with_options(CodegenOptions {
single_quote: true,
minify: remove_whitespace,
..CodegenOptions::default()
})
.build(&program)
.code
}

View file

@ -49,7 +49,7 @@ watch:
example tool *args='':
cargo --color always run -p oxc_{{tool}} --example {{tool}} -- {{args}}
watch-example args='':
watch-example *args='':
bacon example -- {{args}}
# Generate AST related boilerplate code.

View file

@ -2,7 +2,7 @@ commit: 06454619
runtime Summary:
AST Parsed : 18446/18446 (100.00%)
Positive Passed: 17182/18446 (93.15%)
Positive Passed: 17183/18446 (93.15%)
tasks/coverage/test262/test/annexB/language/function-code/block-decl-func-block-scoping.js
minify error: ReferenceError: f is not defined
@ -2088,9 +2088,6 @@ minify error: Test262Error: Expected SameValue(«"a"», «"m"») to be true
tasks/coverage/test262/test/language/expressions/subtraction/S11.6.2_A4_T5.js
minify error: Test262Error: #3.2: -0 - 0 === - 0. Actual: +0
tasks/coverage/test262/test/language/expressions/subtraction/bigint-arithmetic.js
minify error: SyntaxError: Invalid left-hand side expression in postfix operation
tasks/coverage/test262/test/language/expressions/super/call-spread-err-mult-err-obj-unresolvable.js
transform error: ReferenceError: require is not defined