securityos/node_modules/eslint-plugin-import/lib/rules/dynamic-import-chunkname.js

120 lines
14 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
'use strict';var _vm = require('vm');var _vm2 = _interopRequireDefault(_vm);
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
module.exports = {
meta: {
type: 'suggestion',
docs: {
category: 'Style guide',
description: 'Enforce a leading comment with the webpackChunkName for dynamic imports.',
url: (0, _docsUrl2['default'])('dynamic-import-chunkname') },
schema: [{
type: 'object',
properties: {
importFunctions: {
type: 'array',
uniqueItems: true,
items: {
type: 'string' } },
webpackChunknameFormat: {
type: 'string' } } }] },
create: function () {function create(context) {
var config = context.options[0];var _ref =
config || {},_ref$importFunctions = _ref.importFunctions,importFunctions = _ref$importFunctions === undefined ? [] : _ref$importFunctions;var _ref2 =
config || {},_ref2$webpackChunknam = _ref2.webpackChunknameFormat,webpackChunknameFormat = _ref2$webpackChunknam === undefined ? '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+' : _ref2$webpackChunknam;
var paddedCommentRegex = /^ (\S[\s\S]+\S) $/;
var commentStyleRegex = /^( ((webpackChunkName: .+)|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/;
var chunkSubstrFormat = ' webpackChunkName: ["\']' + String(webpackChunknameFormat) + '["\'],? ';
var chunkSubstrRegex = new RegExp(chunkSubstrFormat);
function run(node, arg) {
var sourceCode = context.getSourceCode();
var leadingComments = sourceCode.getCommentsBefore ?
sourceCode.getCommentsBefore(arg) // This method is available in ESLint >= 4.
: sourceCode.getComments(arg).leading; // This method is deprecated in ESLint 7.
if (!leadingComments || leadingComments.length === 0) {
context.report({
node: node,
message: 'dynamic imports require a leading comment with the webpack chunkname' });
return;
}
var isChunknamePresent = false;var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
for (var _iterator = leadingComments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var comment = _step.value;
if (comment.type !== 'Block') {
context.report({
node: node,
message: 'dynamic imports require a /* foo */ style comment, not a // foo comment' });
return;
}
if (!paddedCommentRegex.test(comment.value)) {
context.report({
node: node,
message: 'dynamic imports require a block comment padded with spaces - /* foo */' });
return;
}
try {
// just like webpack itself does
_vm2['default'].runInNewContext('(function() {return {' + String(comment.value) + '}})()');
} catch (error) {
context.report({
node: node,
message: 'dynamic imports require a "webpack" comment with valid syntax' });
return;
}
if (!commentStyleRegex.test(comment.value)) {
context.report({
node: node,
message: 'dynamic imports require a "webpack" comment with valid syntax' });
return;
}
if (chunkSubstrRegex.test(comment.value)) {
isChunknamePresent = true;
}
}} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
if (!isChunknamePresent) {
context.report({
node: node,
message: 'dynamic imports require a leading comment in the form /*' +
chunkSubstrFormat + '*/' });
}
}
return {
ImportExpression: function () {function ImportExpression(node) {
run(node, node.source);
}return ImportExpression;}(),
CallExpression: function () {function CallExpression(node) {
if (node.callee.type !== 'Import' && importFunctions.indexOf(node.callee.name) < 0) {
return;
}
run(node, node.arguments[0]);
}return CallExpression;}() };
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9keW5hbWljLWltcG9ydC1jaHVua25hbWUuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVz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