From 7088b14dd86a95db1c8e6096e4832b87b14e7936 Mon Sep 17 00:00:00 2001 From: Max Burmagin Date: Sat, 25 Dec 2021 15:00:52 +0300 Subject: [PATCH] show webpackChunkName format error only when webpackChunkName invalid --- src/rules/dynamic-import-chunkname.js | 4 +- tests/src/rules/dynamic-import-chunkname.js | 107 +++++++++++++++++++- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/src/rules/dynamic-import-chunkname.js b/src/rules/dynamic-import-chunkname.js index 7bf13f4894..2c59d35e59 100644 --- a/src/rules/dynamic-import-chunkname.js +++ b/src/rules/dynamic-import-chunkname.js @@ -30,7 +30,7 @@ module.exports = { const { webpackChunknameFormat = '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+' } = config || {}; const paddedCommentRegex = /^ (\S[\s\S]+\S) $/; - const commentStyleRegex = /^( ((webpackChunkName: ["']([0-9a-zA-Z-_/.]|\[(request|index)\])+["'])|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/; + const commentStyleRegex = /^( ((webpackChunkName: .+)|((webpackPrefetch|webpackPreload): (true|false|-?[0-9]+))|(webpackIgnore: (true|false))|((webpackInclude|webpackExclude): \/.*\/)|(webpackMode: ["'](lazy|lazy-once|eager|weak)["'])|(webpackExports: (['"]\w+['"]|\[(['"]\w+['"], *)+(['"]\w+['"]*)\]))),?)+ $/; const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `; const chunkSubstrRegex = new RegExp(chunkSubstrFormat); @@ -83,7 +83,7 @@ module.exports = { context.report({ node, message: - `dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`, + `dynamic imports require a "webpack" comment with valid syntax`, }); return; } diff --git a/tests/src/rules/dynamic-import-chunkname.js b/tests/src/rules/dynamic-import-chunkname.js index 843006227e..4c5c2f4716 100644 --- a/tests/src/rules/dynamic-import-chunkname.js +++ b/tests/src/rules/dynamic-import-chunkname.js @@ -21,8 +21,9 @@ const noLeadingCommentError = 'dynamic imports require a leading comment with th const nonBlockCommentError = 'dynamic imports require a /* foo */ style comment, not a // foo comment'; const noPaddingCommentError = 'dynamic imports require a block comment padded with spaces - /* foo */'; const invalidSyntaxCommentError = 'dynamic imports require a "webpack" comment with valid syntax'; -const commentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`; -const pickyCommentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`; +const commentFormatError = `dynamic imports require a "webpack" comment with valid syntax`; +const chunkNameFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`; +const pickyChunkNameFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`; ruleTester.run('dynamic-import-chunkname', rule, { valid: [ @@ -511,6 +512,54 @@ ruleTester.run('dynamic-import-chunkname', rule, { type: 'CallExpression', }], }, + { + code: `import( + /* webpackChunkName: true */ + 'someModule' + )`, + options, + parser, + output: `import( + /* webpackChunkName: true */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: 'CallExpression', + }], + }, + { + code: `import( + /* webpackChunkName: "my-module-[id]" */ + 'someModule' + )`, + options, + parser, + output: `import( + /* webpackChunkName: "my-module-[id]" */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: 'CallExpression', + }], + }, + { + code: `import( + /* webpackChunkName: ["request"] */ + 'someModule' + )`, + options, + parser, + output: `import( + /* webpackChunkName: ["request"] */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: 'CallExpression', + }], + }, { code: `import( /*webpackChunkName: "someModule"*/ @@ -621,7 +670,7 @@ ruleTester.run('dynamic-import-chunkname', rule, { 'someModule' )`, errors: [{ - message: pickyCommentFormatError, + message: pickyChunkNameFormatError, type: 'CallExpression', }], }, @@ -911,7 +960,7 @@ ruleTester.run('dynamic-import-chunkname', rule, { 'someModule' )`, errors: [{ - message: pickyCommentFormatError, + message: pickyChunkNameFormatError, type: 'CallExpression', }], }, @@ -1433,6 +1482,54 @@ context('TypeScript', () => { type: nodeType, }], }, + { + code: `import( + /* webpackChunkName: true */ + 'someModule' + )`, + options, + parser: typescriptParser, + output: `import( + /* webpackChunkName: true */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: nodeType, + }], + }, + { + code: `import( + /* webpackChunkName: "my-module-[id]" */ + 'someModule' + )`, + options, + parser: typescriptParser, + output: `import( + /* webpackChunkName: "my-module-[id]" */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: nodeType, + }], + }, + { + code: `import( + /* webpackChunkName: ["request"] */ + 'someModule' + )`, + options, + parser: typescriptParser, + output: `import( + /* webpackChunkName: ["request"] */ + 'someModule' + )`, + errors: [{ + message: chunkNameFormatError, + type: nodeType, + }], + }, { code: `import( /* webpackChunkName: "someModule123" */ @@ -1445,7 +1542,7 @@ context('TypeScript', () => { 'someModule' )`, errors: [{ - message: pickyCommentFormatError, + message: pickyChunkNameFormatError, type: nodeType, }], },