@@ -8,33 +8,69 @@ import docsUrl from '../docsUrl'
88const EXPORT_MESSAGE = 'Expected "export" or "export default"'
99 , IMPORT_MESSAGE = 'Expected "import" instead of "require()"'
1010
11- function allowPrimitive ( node , context ) {
12- if ( context . options . indexOf ( 'allow-primitive-modules' ) < 0 ) return false
11+ function normalizeLegacyOptions ( options ) {
12+ if ( options . indexOf ( 'allow-primitive-modules' ) >= 0 ) {
13+ return { allowPrimitiveModules : true }
14+ }
15+ return options [ 0 ] || { }
16+ }
17+
18+ function allowPrimitive ( node , options ) {
19+ if ( ! options . allowPrimitiveModules ) return false
1320 if ( node . parent . type !== 'AssignmentExpression' ) return false
1421 return ( node . parent . right . type !== 'ObjectExpression' )
1522}
1623
24+ function allowRequire ( node , options ) {
25+ return options . allowRequire
26+ }
27+
1728//------------------------------------------------------------------------------
1829// Rule Definition
1930//------------------------------------------------------------------------------
2031
32+ const schemaString = { enum : [ 'allow-primitive-modules' ] }
33+ const schemaObject = {
34+ type : 'object' ,
35+ properties : {
36+ allowPrimitiveModules : { 'type' : 'boolean' } ,
37+ allowRequire : { 'type' : 'boolean' } ,
38+ } ,
39+ additionalProperties : false ,
40+ }
2141
2242module . exports = {
2343 meta : {
2444 docs : {
2545 url : docsUrl ( 'no-commonjs' ) ,
2646 } ,
47+
48+ schema : {
49+ anyOf : [
50+ {
51+ type : 'array' ,
52+ items : [ schemaString ] ,
53+ additionalItems : false ,
54+ } ,
55+ {
56+ type : 'array' ,
57+ items : [ schemaObject ] ,
58+ additionalItems : false ,
59+ } ,
60+ ] ,
61+ } ,
2762 } ,
2863
2964 create : function ( context ) {
65+ const options = normalizeLegacyOptions ( context . options )
3066
3167 return {
3268
3369 'MemberExpression' : function ( node ) {
3470
3571 // module.exports
3672 if ( node . object . name === 'module' && node . property . name === 'exports' ) {
37- if ( allowPrimitive ( node , context ) ) return
73+ if ( allowPrimitive ( node , options ) ) return
3874 context . report ( { node, message : EXPORT_MESSAGE } )
3975 }
4076
@@ -65,6 +101,8 @@ module.exports = {
65101 if ( module . type !== 'Literal' ) return
66102 if ( typeof module . value !== 'string' ) return
67103
104+ if ( allowRequire ( call , options ) ) return
105+
68106 // keeping it simple: all 1-string-arg `require` calls are reported
69107 context . report ( {
70108 node : call . callee ,
0 commit comments