Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] dynamic-import-chunkname/TypeScript: support @typescript-eslint/parser #1833

Merged
merged 1 commit into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -23,6 +23,9 @@ matrix:
include:
- env: LINT=true
node_js: lts/*
- env: TS_PARSER=2 ESLINT_VERSION=7
node_js: lts/*
before_script: 'npm install --no-save @typescript-eslint/parser@2'
- env: PACKAGE=resolvers/node
node_js: 14
- env: PACKAGE=resolvers/node
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing type from dev dependencies ([#1820], thanks [@fernandopasik])
- [`default`]: avoid crash with `export =` ([#1822], thanks [@AndrewLeedham])
- [`order`]/[`newline-after-import`]: ignore TypeScript's "export import object" ([#1830], thanks [@be5invis])
- [`dynamic-import-chunkname`]/TypeScript: supports `@typescript-eslint/parser` ([#1833], thanks [@noelebrun])

### Changed
- [`no-extraneous-dependencies`]: add tests for importing types ([#1824], thanks [@taye])
Expand Down Expand Up @@ -713,6 +714,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1833]: https://github.com/benmosher/eslint-plugin-import/pull/1833
[#1830]: https://github.com/benmosher/eslint-plugin-import/pull/1830
[#1824]: https://github.com/benmosher/eslint-plugin-import/pull/1824
[#1823]: https://github.com/benmosher/eslint-plugin-import/pull/1823
Expand Down Expand Up @@ -1238,3 +1240,4 @@ for info on changes for earlier releases.
[@taye]: https://github.com/taye
[@AndrewLeedham]: https://github.com/AndrewLeedham
[@be5invis]: https://github.com/be5invis
[@noelebrun]: https://github.com/noelebrun
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,7 +55,7 @@
"devDependencies": {
"@eslint/import-test-order-redirect-scoped": "file:./tests/files/order-redirect-scoped",
"@test-scope/some-module": "file:./tests/files/symlinked-module",
"@typescript-eslint/parser": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0 || ^3.3.0",
"array.prototype.flatmap": "^1.2.3",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
Expand Down
117 changes: 62 additions & 55 deletions src/rules/dynamic-import-chunkname.js
Expand Up @@ -34,78 +34,85 @@ module.exports = {
const chunkSubstrFormat = ` webpackChunkName: "${webpackChunknameFormat}",? `
const chunkSubstrRegex = new RegExp(chunkSubstrFormat)

return {
CallExpression(node) {
if (node.callee.type !== 'Import' && importFunctions.indexOf(node.callee.name) < 0) {
return
}
function run(node, arg) {
const sourceCode = context.getSourceCode()
const leadingComments = sourceCode.getCommentsBefore
? sourceCode.getCommentsBefore(arg) // This method is available in ESLint >= 4.
: sourceCode.getComments(arg).leading // This method is deprecated in ESLint 7.

const sourceCode = context.getSourceCode()
const arg = node.arguments[0]
const 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,
message: 'dynamic imports require a leading comment with the webpack chunkname',
})
return
}

if (!leadingComments || leadingComments.length === 0) {
let isChunknamePresent = false

for (const comment of leadingComments) {
if (comment.type !== 'Block') {
context.report({
node,
message: 'dynamic imports require a leading comment with the webpack chunkname',
message: 'dynamic imports require a /* foo */ style comment, not a // foo comment',
})
return
}

let isChunknamePresent = false

for (const comment of leadingComments) {
if (comment.type !== 'Block') {
context.report({
node,
message: 'dynamic imports require a /* foo */ style comment, not a // foo comment',
})
return
}

if (!paddedCommentRegex.test(comment.value)) {
context.report({
node,
message: `dynamic imports require a block comment padded with spaces - /* foo */`,
})
return
}

try {
// just like webpack itself does
vm.runInNewContext(`(function(){return {${comment.value}}})()`)
}
catch (error) {
context.report({
node,
message: `dynamic imports require a "webpack" comment with valid syntax`,
})
return
}

if (!commentStyleRegex.test(comment.value)) {
context.report({
node,
message:
`dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`,
})
return
}
if (!paddedCommentRegex.test(comment.value)) {
context.report({
node,
message: `dynamic imports require a block comment padded with spaces - /* foo */`,
})
return
}

if (chunkSubstrRegex.test(comment.value)) {
isChunknamePresent = true
}
try {
// just like webpack itself does
vm.runInNewContext(`(function(){return {${comment.value}}})()`)
}
catch (error) {
context.report({
node,
message: `dynamic imports require a "webpack" comment with valid syntax`,
})
return
}

if (!isChunknamePresent) {
if (!commentStyleRegex.test(comment.value)) {
context.report({
node,
message:
`dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`,
})
return
}

if (chunkSubstrRegex.test(comment.value)) {
isChunknamePresent = true
}
}

if (!isChunknamePresent) {
context.report({
node,
message:
`dynamic imports require a leading comment in the form /*${chunkSubstrFormat}*/`,
})
}
}

return {
ImportExpression(node) {
run(node, node.source)
},

CallExpression(node) {
if (node.callee.type !== 'Import' && importFunctions.indexOf(node.callee.name) < 0) {
return
}

run(node, node.arguments[0])
},
}
},
Expand Down