UNPKG

@rushstack/eslint-plugin

Version:

An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package

47 lines 2.38 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.importRequiresChunkNameRule = exports.MESSAGE_ID_SINGLE_CHUNK_NAME = exports.MESSAGE_ID_CHUNK_NAME = void 0; exports.MESSAGE_ID_CHUNK_NAME = 'error-import-requires-chunk-name'; exports.MESSAGE_ID_SINGLE_CHUNK_NAME = 'error-import-requires-single-chunk-name'; const importRequiresChunkNameRule = { defaultOptions: [], meta: { type: 'problem', messages: { [exports.MESSAGE_ID_CHUNK_NAME]: 'Usage of "import(...)" for code splitting requires a /* webpackChunkName: \'...\' */ comment', [exports.MESSAGE_ID_SINGLE_CHUNK_NAME]: 'Usage of "import(...)" for code splitting cannot specify multiple /* webpackChunkName: \'...\' */ comments' }, schema: [], docs: { description: 'Requires that calls to "import(...)" for code splitting include a Webpack chunk name', url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin' } }, create: (context) => { const sourceCode = context.sourceCode; const webpackChunkNameRegex = /^webpackChunkName\s*:\s*('[^']+'|"[^"]+")$/; return { ImportExpression: (node) => { const nodeComments = sourceCode.getCommentsInside(node); const webpackChunkNameEntries = []; for (const comment of nodeComments) { const webpackChunkNameMatches = comment.value .split(',') .map((c) => c.trim()) .filter((c) => !!c.match(webpackChunkNameRegex)); webpackChunkNameEntries.push(...webpackChunkNameMatches); } if (webpackChunkNameEntries.length === 0) { context.report({ node, messageId: exports.MESSAGE_ID_CHUNK_NAME }); } else if (webpackChunkNameEntries.length !== 1) { context.report({ node, messageId: exports.MESSAGE_ID_SINGLE_CHUNK_NAME }); } } }; } }; exports.importRequiresChunkNameRule = importRequiresChunkNameRule; //# sourceMappingURL=import-requires-chunk-name.js.map