2023-12-28 23:41:32 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
exports.__esModule = true;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object of node visitors that will call
|
|
|
|
* 'visitor' with every discovered module path.
|
|
|
|
*
|
|
|
|
* todo: correct function prototype for visitor
|
|
|
|
* @param {Function(String)} visitor [description]
|
|
|
|
* @param {[type]} options [description]
|
|
|
|
* @return {object}
|
|
|
|
*/
|
|
|
|
exports.default = function visitModules(visitor, options) {
|
|
|
|
// if esmodule is not explicitly disabled, it is assumed to be enabled
|
2023-12-28 23:41:32 +08:00
|
|
|
options = Object.assign({ esmodule: true }, options);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
let ignoreRegExps = [];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (options.ignore != null) {
|
2023-12-28 23:41:32 +08:00
|
|
|
ignoreRegExps = options.ignore.map((p) => new RegExp(p));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkSourceValue(source, importer) {
|
2023-12-28 23:41:32 +08:00
|
|
|
if (source == null) { return; } //?
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
// handle ignore
|
2023-12-28 23:41:32 +08:00
|
|
|
if (ignoreRegExps.some((re) => re.test(source.value))) { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
// fire visitor
|
2023-12-28 23:41:32 +08:00
|
|
|
visitor(source, importer);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// for import-y declarations
|
|
|
|
function checkSource(node) {
|
2023-12-28 23:41:32 +08:00
|
|
|
checkSourceValue(node.source, node);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// for esmodule dynamic `import()` calls
|
|
|
|
function checkImportCall(node) {
|
2023-12-28 23:41:32 +08:00
|
|
|
let modulePath;
|
|
|
|
// refs https://github.com/estree/estree/blob/HEAD/es2020.md#importexpression
|
|
|
|
if (node.type === 'ImportExpression') {
|
|
|
|
modulePath = node.source;
|
|
|
|
} else if (node.type === 'CallExpression') {
|
|
|
|
if (node.callee.type !== 'Import') { return; }
|
|
|
|
if (node.arguments.length !== 1) { return; }
|
|
|
|
|
|
|
|
modulePath = node.arguments[0];
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
if (modulePath.type !== 'Literal') { return; }
|
|
|
|
if (typeof modulePath.value !== 'string') { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
checkSourceValue(modulePath, node);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// for CommonJS `require` calls
|
2023-12-28 23:41:32 +08:00
|
|
|
// adapted from @mctep: https://git.io/v4rAu
|
2023-12-18 13:12:25 +08:00
|
|
|
function checkCommon(call) {
|
2023-12-28 23:41:32 +08:00
|
|
|
if (call.callee.type !== 'Identifier') { return; }
|
|
|
|
if (call.callee.name !== 'require') { return; }
|
|
|
|
if (call.arguments.length !== 1) { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
const modulePath = call.arguments[0];
|
|
|
|
if (modulePath.type !== 'Literal') { return; }
|
|
|
|
if (typeof modulePath.value !== 'string') { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
checkSourceValue(modulePath, call);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkAMD(call) {
|
2023-12-28 23:41:32 +08:00
|
|
|
if (call.callee.type !== 'Identifier') { return; }
|
|
|
|
if (call.callee.name !== 'require' && call.callee.name !== 'define') { return; }
|
|
|
|
if (call.arguments.length !== 2) { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
const modules = call.arguments[0];
|
|
|
|
if (modules.type !== 'ArrayExpression') { return; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
for (const element of modules.elements) {
|
|
|
|
if (element.type !== 'Literal') { continue; }
|
|
|
|
if (typeof element.value !== 'string') { continue; }
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
if (
|
|
|
|
element.value === 'require'
|
|
|
|
|| element.value === 'exports'
|
|
|
|
) {
|
|
|
|
continue; // magic modules: https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic-modules
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
checkSourceValue(element, element);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
const visitors = {};
|
2023-12-18 13:12:25 +08:00
|
|
|
if (options.esmodule) {
|
|
|
|
Object.assign(visitors, {
|
2023-12-28 23:41:32 +08:00
|
|
|
ImportDeclaration: checkSource,
|
|
|
|
ExportNamedDeclaration: checkSource,
|
|
|
|
ExportAllDeclaration: checkSource,
|
|
|
|
CallExpression: checkImportCall,
|
|
|
|
ImportExpression: checkImportCall,
|
|
|
|
});
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.commonjs || options.amd) {
|
2023-12-28 23:41:32 +08:00
|
|
|
const currentCallExpression = visitors.CallExpression;
|
|
|
|
visitors.CallExpression = function (call) {
|
|
|
|
if (currentCallExpression) { currentCallExpression(call); }
|
|
|
|
if (options.commonjs) { checkCommon(call); }
|
|
|
|
if (options.amd) { checkAMD(call); }
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
return visitors;
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* make an options schema for the module visitor, optionally
|
|
|
|
* adding extra fields.
|
|
|
|
*/
|
|
|
|
function makeOptionsSchema(additionalProperties) {
|
|
|
|
const base = {
|
2023-12-28 23:41:32 +08:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
commonjs: { type: 'boolean' },
|
|
|
|
amd: { type: 'boolean' },
|
|
|
|
esmodule: { type: 'boolean' },
|
|
|
|
ignore: {
|
|
|
|
type: 'array',
|
|
|
|
minItems: 1,
|
|
|
|
items: { type: 'string' },
|
|
|
|
uniqueItems: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
},
|
2023-12-28 23:41:32 +08:00
|
|
|
additionalProperties: false,
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
if (additionalProperties) {
|
|
|
|
for (const key in additionalProperties) {
|
|
|
|
base.properties[key] = additionalProperties[key];
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
return base;
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2023-12-28 23:41:32 +08:00
|
|
|
exports.makeOptionsSchema = makeOptionsSchema;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* json schema object for options parameter. can be used to build
|
|
|
|
* rule options schema object.
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2023-12-28 23:41:32 +08:00
|
|
|
exports.optionsSchema = makeOptionsSchema();
|