1
1
'use strict' ;
2
2
const { defaultsDeep} = require ( 'lodash' ) ;
3
3
const { getStringIfConstant} = require ( '@eslint-community/eslint-utils' ) ;
4
- const { callExpressionSelector } = require ( './selectors /index.js' ) ;
4
+ const { isCallExpression } = require ( './ast /index.js' ) ;
5
5
6
6
const MESSAGE_ID = 'importStyle' ;
7
7
const messages = {
@@ -117,6 +117,12 @@ const joinOr = words => words
117
117
} )
118
118
. join ( ' ' ) ;
119
119
120
+ const isAssignedDynamicImport = node =>
121
+ node . parent . type === 'AwaitExpression'
122
+ && node . parent . argument === node
123
+ && node . parent . parent . type === 'VariableDeclarator'
124
+ && node . parent . parent . init === node . parent ;
125
+
120
126
// Keep this alphabetically sorted for easier maintenance
121
127
const defaultStyles = {
122
128
chalk : {
@@ -130,19 +136,6 @@ const defaultStyles = {
130
136
} ,
131
137
} ;
132
138
133
- const assignedDynamicImportSelector = [
134
- 'VariableDeclarator' ,
135
- '[init.type="AwaitExpression"]' ,
136
- '[init.argument.type="ImportExpression"]' ,
137
- ] . join ( '' ) ;
138
-
139
- const assignedRequireSelector = [
140
- 'VariableDeclarator' ,
141
- '[init.type="CallExpression"]' ,
142
- '[init.callee.type="Identifier"]' ,
143
- '[init.callee.name="require"]' ,
144
- ] . join ( '' ) ;
145
-
146
139
/** @param {import('eslint').Rule.RuleContext } context */
147
140
const create = context => {
148
141
let [
@@ -201,106 +194,117 @@ const create = context => {
201
194
} ) ;
202
195
} ;
203
196
204
- let visitor = { } ;
205
-
206
197
if ( checkImport ) {
207
- visitor = {
208
- ...visitor ,
209
-
210
- ImportDeclaration ( node ) {
211
- const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
198
+ context . on ( 'ImportDeclaration' , node => {
199
+ const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
212
200
213
- const allowedImportStyles = styles . get ( moduleName ) ;
214
- const actualImportStyles = getActualImportDeclarationStyles ( node ) ;
201
+ const allowedImportStyles = styles . get ( moduleName ) ;
202
+ const actualImportStyles = getActualImportDeclarationStyles ( node ) ;
215
203
216
- report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
217
- } ,
218
- } ;
204
+ report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
205
+ } ) ;
219
206
}
220
207
221
208
if ( checkDynamicImport ) {
222
- visitor = {
223
- ...visitor ,
209
+ context . on ( 'ImportExpression' , node => {
210
+ if ( isAssignedDynamicImport ( node ) ) {
211
+ return ;
212
+ }
224
213
225
- 'ExpressionStatement > ImportExpression' ( node ) {
226
- const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
227
- const allowedImportStyles = styles . get ( moduleName ) ;
228
- const actualImportStyles = [ 'unassigned' ] ;
214
+ const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
215
+ const allowedImportStyles = styles . get ( moduleName ) ;
216
+ const actualImportStyles = [ 'unassigned' ] ;
229
217
230
- report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
231
- } ,
218
+ report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
219
+ } ) ;
232
220
233
- [ assignedDynamicImportSelector ] ( node ) {
234
- const assignmentTargetNode = node . id ;
235
- const moduleNameNode = node . init . argument . source ;
236
- const moduleName = getStringIfConstant ( moduleNameNode , sourceCode . getScope ( moduleNameNode ) ) ;
221
+ context . on ( 'VariableDeclarator' , node => {
222
+ if ( ! (
223
+ node . init ?. type === 'AwaitExpression'
224
+ && node . init . argument . type === 'ImportExpression'
225
+ ) ) {
226
+ return ;
227
+ }
237
228
238
- if ( ! moduleName ) {
239
- return ;
240
- }
229
+ const assignmentTargetNode = node . id ;
230
+ const moduleNameNode = node . init . argument . source ;
231
+ const moduleName = getStringIfConstant ( moduleNameNode , sourceCode . getScope ( moduleNameNode ) ) ;
241
232
242
- const allowedImportStyles = styles . get ( moduleName ) ;
243
- const actualImportStyles = getActualAssignmentTargetImportStyles ( assignmentTargetNode ) ;
233
+ if ( ! moduleName ) {
234
+ return ;
235
+ }
244
236
245
- report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
246
- } ,
247
- } ;
237
+ const allowedImportStyles = styles . get ( moduleName ) ;
238
+ const actualImportStyles = getActualAssignmentTargetImportStyles ( assignmentTargetNode ) ;
239
+
240
+ report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
241
+ } ) ;
248
242
}
249
243
250
244
if ( checkExportFrom ) {
251
- visitor = {
252
- ... visitor ,
245
+ context . on ( 'ExportAllDeclaration' , node => {
246
+ const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
253
247
254
- ExportAllDeclaration ( node ) {
255
- const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
248
+ const allowedImportStyles = styles . get ( moduleName ) ;
249
+ const actualImportStyles = [ 'namespace' ] ;
256
250
257
- const allowedImportStyles = styles . get ( moduleName ) ;
258
- const actualImportStyles = [ 'namespace' ] ;
259
-
260
- report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
261
- } ,
251
+ report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
252
+ } ) ;
262
253
263
- ExportNamedDeclaration ( node ) {
264
- const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
254
+ context . on ( 'ExportNamedDeclaration' , node => {
255
+ const moduleName = getStringIfConstant ( node . source , sourceCode . getScope ( node . source ) ) ;
265
256
266
- const allowedImportStyles = styles . get ( moduleName ) ;
267
- const actualImportStyles = getActualExportDeclarationStyles ( node ) ;
257
+ const allowedImportStyles = styles . get ( moduleName ) ;
258
+ const actualImportStyles = getActualExportDeclarationStyles ( node ) ;
268
259
269
- report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
270
- } ,
271
- } ;
260
+ report ( node , moduleName , actualImportStyles , allowedImportStyles ) ;
261
+ } ) ;
272
262
}
273
263
274
264
if ( checkRequire ) {
275
- visitor = {
276
- ...visitor ,
265
+ context . on ( 'CallExpression' , node => {
266
+ if ( ! (
267
+ isCallExpression ( node , {
268
+ name : 'require' ,
269
+ argumentsLength : 1 ,
270
+ optionalCall : false ,
271
+ optionalMember : false ,
272
+ } )
273
+ && ( node . parent . type === 'ExpressionStatement' && node . parent . expression === node )
274
+ ) ) {
275
+ return ;
276
+ }
277
277
278
- [ `ExpressionStatement > ${ callExpressionSelector ( { name : 'require' , argumentsLength : 1 } ) } .expression` ] ( node ) {
279
- const moduleName = getStringIfConstant ( node . arguments [ 0 ] , sourceCode . getScope ( node . arguments [ 0 ] ) ) ;
280
- const allowedImportStyles = styles . get ( moduleName ) ;
281
- const actualImportStyles = [ 'unassigned' ] ;
278
+ const moduleName = getStringIfConstant ( node . arguments [ 0 ] , sourceCode . getScope ( node . arguments [ 0 ] ) ) ;
279
+ const allowedImportStyles = styles . get ( moduleName ) ;
280
+ const actualImportStyles = [ 'unassigned' ] ;
282
281
283
- report ( node , moduleName , actualImportStyles , allowedImportStyles , true ) ;
284
- } ,
282
+ report ( node , moduleName , actualImportStyles , allowedImportStyles , true ) ;
283
+ } ) ;
285
284
286
- [ assignedRequireSelector ] ( node ) {
287
- const assignmentTargetNode = node . id ;
288
- const moduleNameNode = node . init . arguments [ 0 ] ;
289
- const moduleName = getStringIfConstant ( moduleNameNode , sourceCode . getScope ( moduleNameNode ) ) ;
285
+ context . on ( 'VariableDeclarator' , node => {
286
+ if ( ! (
287
+ node . init ?. type === 'CallExpression'
288
+ && node . init . callee . type === 'Identifier'
289
+ && node . init . callee . name === 'require'
290
+ ) ) {
291
+ return ;
292
+ }
290
293
291
- if ( ! moduleName ) {
292
- return ;
293
- }
294
+ const assignmentTargetNode = node . id ;
295
+ const moduleNameNode = node . init . arguments [ 0 ] ;
296
+ const moduleName = getStringIfConstant ( moduleNameNode , sourceCode . getScope ( moduleNameNode ) ) ;
294
297
295
- const allowedImportStyles = styles . get ( moduleName ) ;
296
- const actualImportStyles = getActualAssignmentTargetImportStyles ( assignmentTargetNode ) ;
298
+ if ( ! moduleName ) {
299
+ return ;
300
+ }
297
301
298
- report ( node , moduleName , actualImportStyles , allowedImportStyles , true ) ;
299
- } ,
300
- } ;
301
- }
302
+ const allowedImportStyles = styles . get ( moduleName ) ;
303
+ const actualImportStyles = getActualAssignmentTargetImportStyles ( assignmentTargetNode ) ;
302
304
303
- return visitor ;
305
+ report ( node , moduleName , actualImportStyles , allowedImportStyles , true ) ;
306
+ } ) ;
307
+ }
304
308
} ;
305
309
306
310
const schema = {
0 commit comments