mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(linter/tree-shaking): detect the correct export symbol resolution (#5467)
fixes: #5455
This commit is contained in:
parent
cba93f52d0
commit
5187f384cb
3 changed files with 26 additions and 7 deletions
|
|
@ -8,10 +8,10 @@ use oxc_ast::{
|
||||||
FormalParameter, Function, IdentifierReference, JSXAttribute, JSXAttributeItem,
|
FormalParameter, Function, IdentifierReference, JSXAttribute, JSXAttributeItem,
|
||||||
JSXAttributeValue, JSXChild, JSXElement, JSXElementName, JSXExpression,
|
JSXAttributeValue, JSXChild, JSXElement, JSXElementName, JSXExpression,
|
||||||
JSXExpressionContainer, JSXFragment, JSXMemberExpression, JSXMemberExpressionObject,
|
JSXExpressionContainer, JSXFragment, JSXMemberExpression, JSXMemberExpressionObject,
|
||||||
JSXOpeningElement, LogicalExpression, MemberExpression, NewExpression, ObjectExpression,
|
JSXOpeningElement, LogicalExpression, MemberExpression, ModuleExportName, NewExpression,
|
||||||
ObjectPropertyKind, ParenthesizedExpression, PrivateFieldExpression, Program, PropertyKey,
|
ObjectExpression, ObjectPropertyKind, ParenthesizedExpression, PrivateFieldExpression,
|
||||||
SequenceExpression, SimpleAssignmentTarget, Statement, StaticMemberExpression, SwitchCase,
|
Program, PropertyKey, SequenceExpression, SimpleAssignmentTarget, Statement,
|
||||||
ThisExpression, UnaryExpression, VariableDeclarator,
|
StaticMemberExpression, SwitchCase, ThisExpression, UnaryExpression, VariableDeclarator,
|
||||||
},
|
},
|
||||||
AstKind,
|
AstKind,
|
||||||
};
|
};
|
||||||
|
|
@ -195,9 +195,10 @@ impl<'a> ListenerMap for ExportSpecifier<'a> {
|
||||||
let ctx = options.ctx;
|
let ctx = options.ctx;
|
||||||
let symbol_table = ctx.symbols();
|
let symbol_table = ctx.symbols();
|
||||||
if has_comment_about_side_effect_check(self.exported.span(), ctx) {
|
if has_comment_about_side_effect_check(self.exported.span(), ctx) {
|
||||||
let Some(name) = self.exported.identifier_name() else { return };
|
let ModuleExportName::IdentifierReference(ident) = &self.local else {
|
||||||
let Some(symbol_id) = options.ctx.symbols().get_symbol_id_from_name(name.as_str())
|
return;
|
||||||
else {
|
};
|
||||||
|
let Some(symbol_id) = get_symbol_id_of_variable(ident, ctx) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -346,6 +346,11 @@ fn test() {
|
||||||
r#"export {x as default} from "import""#,
|
r#"export {x as default} from "import""#,
|
||||||
"export const /* tree-shaking no-side-effects-when-called */ x = function(){}",
|
"export const /* tree-shaking no-side-effects-when-called */ x = function(){}",
|
||||||
"export function /* tree-shaking no-side-effects-when-called */ x(){}",
|
"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}",
|
"const x = function(){}; export {/* tree-shaking no-side-effects-when-called */ x}",
|
||||||
// ExpressionStatement
|
// ExpressionStatement
|
||||||
"const x = 1",
|
"const x = 1",
|
||||||
|
|
@ -632,6 +637,11 @@ fn test() {
|
||||||
"export const /* tree-shaking no-side-effects-when-called */ x = ext",
|
"export const /* tree-shaking no-side-effects-when-called */ x = ext",
|
||||||
"export function /* 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}",
|
"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
|
// ExpressionStatement
|
||||||
"ext()",
|
"ext()",
|
||||||
// ForInStatement
|
// ForInStatement
|
||||||
|
|
|
||||||
|
|
@ -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 = () => {}; }
|
||||||
|
3 │ let x = ext
|
||||||
|
· ───
|
||||||
|
4 │ export {/* 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`
|
⚠ 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]
|
╭─[no_side_effects_in_initialization.tsx:1:1]
|
||||||
1 │ ext()
|
1 │ ext()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue