Skip to content

Commit

Permalink
fix(dynamic-import-chunkname): allow single quotes to match Webpack s…
Browse files Browse the repository at this point in the history
…upport

Fixes #1130.
  • Loading branch information
straub committed Jul 7, 2020
1 parent 843055c commit 274b252
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
12 changes: 6 additions & 6 deletions docs/rules/dynamic-import-chunkname.md
Expand Up @@ -39,12 +39,6 @@ import(
'someModule',
);

// using single quotes instead of double quotes
import(
/* webpackChunkName: 'someModule' */
'someModule',
);

// invalid syntax for webpack comment
import(
/* totally not webpackChunkName: "someModule" */
Expand Down Expand Up @@ -78,6 +72,12 @@ The following patterns are valid:
/* webpackChunkName: "someModule", webpackPrefetch: true */
'someModule',
);

// using single quotes instead of double quotes
import(
/* webpackChunkName: 'someModule' */
'someModule',
);
```

## When Not To Use It
Expand Down
4 changes: 2 additions & 2 deletions src/rules/dynamic-import-chunkname.js
Expand Up @@ -30,8 +30,8 @@ module.exports = {
const { webpackChunknameFormat = '[0-9a-zA-Z-_/.]+' } = config || {}

const paddedCommentRegex = /^ (\S[\s\S]+\S) $/
const commentStyleRegex = /^( \w+: ("[^"]*"|\d+|false|true),?)+ $/
const chunkSubstrFormat = ` webpackChunkName: "${webpackChunknameFormat}",? `
const commentStyleRegex = /^( \w+: (["'][^"']*["']|\d+|false|true),?)+ $/
const chunkSubstrFormat = ` webpackChunkName: ["']${webpackChunknameFormat}["'],? `
const chunkSubstrRegex = new RegExp(chunkSubstrFormat)

function run(node, arg) {
Expand Down
79 changes: 56 additions & 23 deletions tests/src/rules/dynamic-import-chunkname.js
Expand Up @@ -21,8 +21,8 @@ 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 leading comment in the form /* webpackChunkName: ["']${commentFormat}["'],? */`
const pickyCommentFormatError = `dynamic imports require a leading comment in the form /* webpackChunkName: ["']${pickyCommentFormat}["'],? */`

ruleTester.run('dynamic-import-chunkname', rule, {
valid: [
Expand Down Expand Up @@ -132,6 +132,14 @@ ruleTester.run('dynamic-import-chunkname', rule, {
options,
parser,
},
{
code: `import(
/* webpackChunkName: 'someModule' */
'someModule'
)`,
options,
parser,
},
{
code: `import(
/* webpackChunkName: "someModule" */
Expand Down Expand Up @@ -192,17 +200,33 @@ ruleTester.run('dynamic-import-chunkname', rule, {
},
{
code: `import(
/* webpackChunkName: 'someModule' */
/* webpackChunkName: "someModule' */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackChunkName: 'someModule' */
/* webpackChunkName: "someModule' */
'someModule'
)`,
errors: [{
message: commentFormatError,
message: invalidSyntaxCommentError,
type: 'CallExpression',
}],
},
{
code: `import(
/* webpackChunkName: 'someModule" */
'someModule'
)`,
options,
parser,
output: `import(
/* webpackChunkName: 'someModule" */
'someModule'
)`,
errors: [{
message: invalidSyntaxCommentError,
type: 'CallExpression',
}],
},
Expand Down Expand Up @@ -421,21 +445,6 @@ ruleTester.run('dynamic-import-chunkname', rule, {
type: 'CallExpression',
}],
},
{
code: `dynamicImport(
/* webpackChunkName: 'someModule' */
'someModule'
)`,
options,
output: `dynamicImport(
/* webpackChunkName: 'someModule' */
'someModule'
)`,
errors: [{
message: commentFormatError,
type: 'CallExpression',
}],
},
{
code: `dynamicImport(
/* webpackChunkName "someModule" */
Expand Down Expand Up @@ -578,6 +587,14 @@ context('TypeScript', () => {
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName: 'someModule' */
'test'
)`,
options,
parser: typescriptParser,
},
],
invalid: [
{
Expand Down Expand Up @@ -624,17 +641,33 @@ context('TypeScript', () => {
},
{
code: `import(
/* webpackChunkName: 'someModule' */
/* webpackChunkName "someModule' */
'someModule'
)`,
options,
parser: typescriptParser,
output: `import(
/* webpackChunkName: 'someModule' */
/* webpackChunkName "someModule' */
'someModule'
)`,
errors: [{
message: commentFormatError,
message: invalidSyntaxCommentError,
type: nodeType,
}],
},
{
code: `import(
/* webpackChunkName 'someModule" */
'someModule'
)`,
options,
parser: typescriptParser,
output: `import(
/* webpackChunkName 'someModule" */
'someModule'
)`,
errors: [{
message: invalidSyntaxCommentError,
type: nodeType,
}],
},
Expand Down

0 comments on commit 274b252

Please sign in to comment.