From 2326cefac81ace32f525ae15f4296da16b85420d Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 4 Mar 2025 06:13:21 +0000 Subject: [PATCH] feat(parser): apply `pure` to argument of unary expression (#9530) Follow-up #9424 --- .../oxc_codegen/tests/integration/pure_comments.rs | 5 +++++ .../tests/integration/snapshots/pure_comments.snap | 12 ++++++++++++ crates/oxc_parser/src/js/expression.rs | 6 +++++- tasks/coverage/snapshots/transformer_babel.snap | 3 +-- tasks/coverage/snapshots/transformer_typescript.snap | 3 +-- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/crates/oxc_codegen/tests/integration/pure_comments.rs b/crates/oxc_codegen/tests/integration/pure_comments.rs index 89b81e846e626..f82debc0b5997 100644 --- a/crates/oxc_codegen/tests/integration/pure_comments.rs +++ b/crates/oxc_codegen/tests/integration/pure_comments.rs @@ -199,6 +199,11 @@ const defineSSRCustomElement = () => { ", "{ /* @__PURE__ */ (function() {})(); }", "{ /* @__PURE__ */ (() => {})(); }", + " +void /* @__PURE__ */ function() {}(); +typeof /* @__PURE__ */ function() {}(); +! /* @__PURE__ */ function() {}(); +delete /* @__PURE__ */ (() => {})();", ]; snapshot("pure_comments", &cases); diff --git a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap index d722e614adf86..daee6a5408d2a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap @@ -423,3 +423,15 @@ let new_exp_no = /* @__PURE__ */ new foo() ** foo(); { /* @__PURE__ */ (() => {})(); } + +########## 24 + +void /* @__PURE__ */ function() {}(); +typeof /* @__PURE__ */ function() {}(); +! /* @__PURE__ */ function() {}(); +delete /* @__PURE__ */ (() => {})(); +---------- +void /* @__PURE__ */ function() {}(); +typeof /* @__PURE__ */ function() {}(); +!/* @__PURE__ */ function() {}(); +delete /* @__PURE__ */ (() => {})(); diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index fab8679ff91ec..219407ad6ae77 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -1005,7 +1005,11 @@ impl<'a> ParserImpl<'a> { let span = self.start_span(); let operator = map_unary_operator(self.cur_kind()); self.bump_any(); - let argument = self.parse_simple_unary_expression(span)?; + let has_pure_comment = self.lexer.trivia_builder.previous_token_has_pure_comment(); + let mut argument = self.parse_simple_unary_expression(span)?; + if has_pure_comment { + Self::set_pure_on_call_or_new_expr(&mut argument); + } Ok(self.ast.expression_unary(self.end_span(span), operator, argument)) } diff --git a/tasks/coverage/snapshots/transformer_babel.snap b/tasks/coverage/snapshots/transformer_babel.snap index dd4ed9d0ae14c..0b2f9d8a79e06 100644 --- a/tasks/coverage/snapshots/transformer_babel.snap +++ b/tasks/coverage/snapshots/transformer_babel.snap @@ -2,7 +2,6 @@ commit: 578ac4df transformer_babel Summary: AST Parsed : 2322/2322 (100.00%) -Positive Passed: 2319/2322 (99.87%) -Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2017/async-functions/context-division-after-expression/input.js +Positive Passed: 2320/2322 (99.91%) Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-private-properties/await-in-private-property-in-async/input.js Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-properties/await-in-class-property-in-async/input.js diff --git a/tasks/coverage/snapshots/transformer_typescript.snap b/tasks/coverage/snapshots/transformer_typescript.snap index 9b3f62874fada..a669bc50dacfe 100644 --- a/tasks/coverage/snapshots/transformer_typescript.snap +++ b/tasks/coverage/snapshots/transformer_typescript.snap @@ -2,8 +2,7 @@ commit: 15392346 transformer_typescript Summary: AST Parsed : 6531/6531 (100.00%) -Positive Passed: 6527/6531 (99.94%) -Mismatch: tasks/coverage/typescript/tests/cases/compiler/tsxDiscriminantPropertyInference.tsx +Positive Passed: 6528/6531 (99.95%) Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessor2.ts Mismatch: tasks/coverage/typescript/tests/cases/conformance/esDecorators/classDeclaration/fields/esDecorators-classDeclaration-fields-staticPrivateAccessor.ts Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/inline/inlineJsxAndJsxFragPragmaOverridesCompilerOptions.tsx