Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | exports.__esModule = true; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 3 | |
| 4 | /** |
| 5 | * Returns an object of node visitors that will call |
| 6 | * 'visitor' with every discovered module path. |
| 7 | * |
| 8 | * todo: correct function prototype for visitor |
| 9 | * @param {Function(String)} visitor [description] |
| 10 | * @param {[type]} options [description] |
| 11 | * @return {object} |
| 12 | */ |
| 13 | exports.default = function visitModules(visitor, options) { |
| 14 | // if esmodule is not explicitly disabled, it is assumed to be enabled |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 15 | options = Object.assign({ esmodule: true }, options); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 16 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 17 | let ignoreRegExps = []; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 18 | if (options.ignore != null) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 19 | ignoreRegExps = options.ignore.map(p => new RegExp(p)); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | function checkSourceValue(source, importer) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 23 | if (source == null) return; //? |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 24 | |
| 25 | // handle ignore |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 26 | if (ignoreRegExps.some(re => re.test(source.value))) return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 27 | |
| 28 | // fire visitor |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 29 | visitor(source, importer); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // for import-y declarations |
| 33 | function checkSource(node) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 34 | checkSourceValue(node.source, node); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // for esmodule dynamic `import()` calls |
| 38 | function checkImportCall(node) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 39 | let modulePath; |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 40 | // refs https://github.com/estree/estree/blob/HEAD/es2020.md#importexpression |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 41 | if (node.type === 'ImportExpression') { |
| 42 | modulePath = node.source; |
| 43 | } else if (node.type === 'CallExpression') { |
| 44 | if (node.callee.type !== 'Import') return; |
| 45 | if (node.arguments.length !== 1) return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 46 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 47 | modulePath = node.arguments[0]; |
| 48 | } |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 49 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 50 | if (modulePath.type !== 'Literal') return; |
| 51 | if (typeof modulePath.value !== 'string') return; |
| 52 | |
| 53 | checkSourceValue(modulePath, node); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // for CommonJS `require` calls |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame] | 57 | // adapted from @mctep: https://git.io/v4rAu |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 58 | function checkCommon(call) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 59 | if (call.callee.type !== 'Identifier') return; |
| 60 | if (call.callee.name !== 'require') return; |
| 61 | if (call.arguments.length !== 1) return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 62 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 63 | const modulePath = call.arguments[0]; |
| 64 | if (modulePath.type !== 'Literal') return; |
| 65 | if (typeof modulePath.value !== 'string') return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 66 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 67 | checkSourceValue(modulePath, call); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | function checkAMD(call) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 71 | if (call.callee.type !== 'Identifier') return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 72 | if (call.callee.name !== 'require' && |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 73 | call.callee.name !== 'define') return; |
| 74 | if (call.arguments.length !== 2) return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 75 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 76 | const modules = call.arguments[0]; |
| 77 | if (modules.type !== 'ArrayExpression') return; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 78 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 79 | for (const element of modules.elements) { |
| 80 | if (element.type !== 'Literal') continue; |
| 81 | if (typeof element.value !== 'string') continue; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 82 | |
| 83 | if (element.value === 'require' || |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame] | 84 | element.value === 'exports') continue; // magic modules: https://git.io/vByan |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 85 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 86 | checkSourceValue(element, element); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 90 | const visitors = {}; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 91 | if (options.esmodule) { |
| 92 | Object.assign(visitors, { |
| 93 | 'ImportDeclaration': checkSource, |
| 94 | 'ExportNamedDeclaration': checkSource, |
| 95 | 'ExportAllDeclaration': checkSource, |
| 96 | 'CallExpression': checkImportCall, |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 97 | 'ImportExpression': checkImportCall, |
| 98 | }); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (options.commonjs || options.amd) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 102 | const currentCallExpression = visitors['CallExpression']; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 103 | visitors['CallExpression'] = function (call) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 104 | if (currentCallExpression) currentCallExpression(call); |
| 105 | if (options.commonjs) checkCommon(call); |
| 106 | if (options.amd) checkAMD(call); |
| 107 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 110 | return visitors; |
| 111 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * make an options schema for the module visitor, optionally |
| 115 | * adding extra fields. |
| 116 | */ |
| 117 | function makeOptionsSchema(additionalProperties) { |
| 118 | const base = { |
| 119 | 'type': 'object', |
| 120 | 'properties': { |
| 121 | 'commonjs': { 'type': 'boolean' }, |
| 122 | 'amd': { 'type': 'boolean' }, |
| 123 | 'esmodule': { 'type': 'boolean' }, |
| 124 | 'ignore': { |
| 125 | 'type': 'array', |
| 126 | 'minItems': 1, |
| 127 | 'items': { 'type': 'string' }, |
| 128 | 'uniqueItems': true, |
| 129 | }, |
| 130 | }, |
| 131 | 'additionalProperties': false, |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 132 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 133 | |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 134 | if (additionalProperties) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 135 | for (const key in additionalProperties) { |
| 136 | base.properties[key] = additionalProperties[key]; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 140 | return base; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 141 | } |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 142 | exports.makeOptionsSchema = makeOptionsSchema; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * json schema object for options parameter. can be used to build |
| 146 | * rule options schema object. |
| 147 | * @type {Object} |
| 148 | */ |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 149 | exports.optionsSchema = makeOptionsSchema(); |