@@ -10,21 +10,7 @@ const funcNames = Object.keys(fsMetaData);
1010const fsPackageNames = [ 'fs' , 'node:fs' , 'fs/promises' , 'node:fs/promises' , 'fs-extra' ] ;
1111
1212const { getImportAccessPath } = require ( '../utils/import-utils' ) ;
13-
14- //------------------------------------------------------------------------------
15- // Utils
16- //------------------------------------------------------------------------------
17-
18- function getIndices ( node , argMeta ) {
19- return ( argMeta || [ ] ) . filter ( ( argIndex ) => node . arguments [ argIndex ] . type !== 'Literal' ) ;
20- }
21-
22- function generateReport ( { context, node, packageName, methodName, indices } ) {
23- if ( ! indices || indices . length === 0 ) {
24- return ;
25- }
26- context . report ( { node, message : `Found ${ methodName } from package "${ packageName } " with non literal argument at index ${ indices . join ( ',' ) } ` } ) ;
27- }
13+ const { isStaticExpression, isStaticExpressionFast } = require ( '../utils/is-static-expression' ) ;
2814
2915//------------------------------------------------------------------------------
3016// Rule Definition
@@ -44,7 +30,7 @@ module.exports = {
4430 return {
4531 CallExpression : function ( node ) {
4632 // don't check require. If all arguments are Literals, it's surely safe!
47- if ( ( node . callee . type === 'Identifier' && node . callee . name === 'require' ) || node . arguments . every ( ( argument ) => argument . type === 'Literal' ) ) {
33+ if ( ( node . callee . type === 'Identifier' && node . callee . name === 'require' ) || node . arguments . every ( ( argument ) => isStaticExpressionFast ( argument ) ) ) {
4834 return ;
4935 }
5036
@@ -87,15 +73,23 @@ module.exports = {
8773 }
8874 const packageName = pathInfo . packageName ;
8975
90- const indices = getIndices ( node , fsMetaData [ fnName ] ) ;
91-
92- generateReport ( {
93- context,
94- node,
95- packageName,
96- methodName : fnName ,
97- indices,
98- } ) ;
76+ const indices = [ ] ;
77+ for ( const index of fsMetaData [ fnName ] || [ ] ) {
78+ if ( index >= node . arguments . length ) {
79+ continue ;
80+ }
81+ const argument = node . arguments [ index ] ;
82+ if ( isStaticExpression ( { node : argument , scope : context . getScope ( ) } ) ) {
83+ continue ;
84+ }
85+ indices . push ( index ) ;
86+ }
87+ if ( indices . length ) {
88+ context . report ( {
89+ node,
90+ message : `Found ${ fnName } from package "${ packageName } " with non literal argument at index ${ indices . join ( ',' ) } ` ,
91+ } ) ;
92+ }
9993 } ,
10094 } ;
10195 } ,
0 commit comments