Skip to content

Commit

Permalink
fix(linter/tree-shaking): detect the correct export symbol resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Sep 5, 2024
1 parent cba93f5 commit 5da8706
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use oxc_ast::{
FormalParameter, Function, IdentifierReference, JSXAttribute, JSXAttributeItem,
JSXAttributeValue, JSXChild, JSXElement, JSXElementName, JSXExpression,
JSXExpressionContainer, JSXFragment, JSXMemberExpression, JSXMemberExpressionObject,
JSXOpeningElement, LogicalExpression, MemberExpression, NewExpression, ObjectExpression,
ObjectPropertyKind, ParenthesizedExpression, PrivateFieldExpression, Program, PropertyKey,
SequenceExpression, SimpleAssignmentTarget, Statement, StaticMemberExpression, SwitchCase,
ThisExpression, UnaryExpression, VariableDeclarator,
JSXOpeningElement, LogicalExpression, MemberExpression, ModuleExportName, NewExpression,
ObjectExpression, ObjectPropertyKind, ParenthesizedExpression, PrivateFieldExpression,
Program, PropertyKey, SequenceExpression, SimpleAssignmentTarget, Statement,
StaticMemberExpression, SwitchCase, ThisExpression, UnaryExpression, VariableDeclarator,
},
AstKind,
};
Expand Down Expand Up @@ -195,9 +195,10 @@ impl<'a> ListenerMap for ExportSpecifier<'a> {
let ctx = options.ctx;
let symbol_table = ctx.symbols();
if has_comment_about_side_effect_check(self.exported.span(), ctx) {
let Some(name) = self.exported.identifier_name() else { return };
let Some(symbol_id) = options.ctx.symbols().get_symbol_id_from_name(name.as_str())
else {
let ModuleExportName::IdentifierReference(ident) = &self.local else {
return;
};
let Some(symbol_id) = get_symbol_id_of_variable(ident, ctx) else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ fn test() {
r#"export {x as default} from "import""#,
"export const /* tree-shaking no-side-effects-when-called */ x = function(){}",
"export function /* tree-shaking no-side-effects-when-called */ x(){}",
"
{ let x = ext; }
let x = () => {}
export {/* tree-shaking no-side-effects-when-called */ x}
",
"const x = function(){}; export {/* tree-shaking no-side-effects-when-called */ x}",
// ExpressionStatement
"const x = 1",
Expand Down Expand Up @@ -632,6 +637,11 @@ fn test() {
"export const /* tree-shaking no-side-effects-when-called */ x = ext",
"export function /* tree-shaking no-side-effects-when-called */ x(){ext()}",
"const x = ext; export {/* tree-shaking no-side-effects-when-called */ x}",
"
{ let x = () => {}; }
let x = ext
export {/* tree-shaking no-side-effects-when-called */ x}
",
// ExpressionStatement
"ext()",
// ForInStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ source: crates/oxc_linter/src/tester.rs
· ───
╰────

eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext`
╭─[no_side_effects_in_initialization.tsx:3:21]
2 │ { let x = () => {}; }
3let x = ext
· ───
4export {/* tree-shaking no-side-effects-when-called */ x}
╰────

eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext`
╭─[no_side_effects_in_initialization.tsx:1:1]
1 │ ext()
Expand Down

0 comments on commit 5da8706

Please sign in to comment.