Skip to content

Commit df979d3

Browse files
committed
🎨 refactor how to collect 'require/import'
1 parent b91b48d commit df979d3

13 files changed

+204
-198
lines changed

‎lib/rules/file-extension-in-import.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
const path = require("path")
88
const fs = require("fs")
9-
const getImportExportTargets = require("../util/get-import-export-targets")
109
const getTryExtensions = require("../util/get-try-extensions")
10+
const visitImport = require("../util/visit-import")
1111
const packageNamePattern = /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
1212
const corePackageOverridePattern = /^(?:assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|worker_threads|zlib)[/\\]$/u
1313

@@ -122,11 +122,8 @@ module.exports = {
122122
}
123123
}
124124

125-
return {
126-
"Program:exit"(node) {
127-
const opts = { optionIndex: 1 }
128-
getImportExportTargets(context, node, opts).forEach(verify)
129-
},
130-
}
125+
return visitImport(context, { optionIndex: 1 }, targets => {
126+
targets.forEach(verify)
127+
})
131128
},
132129
}

‎lib/rules/no-extraneous-import.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
const checkExtraneous = require("../util/check-extraneous")
88
const getAllowModules = require("../util/get-allow-modules")
99
const getConvertPath = require("../util/get-convert-path")
10-
const getImportTargets = require("../util/get-import-export-targets")
1110
const getResolvePaths = require("../util/get-resolve-paths")
1211
const getTryExtensions = require("../util/get-try-extensions")
12+
const visitImport = require("../util/visit-import")
1313

1414
module.exports = {
1515
meta: {
@@ -42,14 +42,8 @@ module.exports = {
4242
return {}
4343
}
4444

45-
return {
46-
"Program:exit"(node) {
47-
checkExtraneous(
48-
context,
49-
filePath,
50-
getImportTargets(context, node, false)
51-
)
52-
},
53-
}
45+
return visitImport(context, {}, targets => {
46+
checkExtraneous(context, filePath, targets)
47+
})
5448
},
5549
}

‎lib/rules/no-extraneous-require.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
const checkExtraneous = require("../util/check-extraneous")
88
const getAllowModules = require("../util/get-allow-modules")
99
const getConvertPath = require("../util/get-convert-path")
10-
const getRequireTargets = require("../util/get-require-targets")
1110
const getResolvePaths = require("../util/get-resolve-paths")
1211
const getTryExtensions = require("../util/get-try-extensions")
12+
const visitRequire = require("../util/visit-require")
1313

1414
module.exports = {
1515
meta: {
@@ -42,14 +42,8 @@ module.exports = {
4242
return {}
4343
}
4444

45-
return {
46-
"Program:exit"() {
47-
checkExtraneous(
48-
context,
49-
filePath,
50-
getRequireTargets(context, false)
51-
)
52-
},
53-
}
45+
return visitRequire(context, {}, targets => {
46+
checkExtraneous(context, filePath, targets)
47+
})
5448
},
5549
}

‎lib/rules/no-hide-core-modules.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
const path = require("path")
1212
const resolve = require("resolve")
1313
const getPackageJson = require("../util/get-package-json")
14-
const getRequireTargets = require("../util/get-require-targets")
15-
const getImportExportTargets = require("../util/get-import-export-targets")
14+
const mergeVisitorsInPlace = require("../util/merge-visitors-in-place")
15+
const visitImport = require("../util/visit-import")
16+
const visitRequire = require("../util/visit-require")
1617

1718
const CORE_MODULES = new Set([
1819
"assert",
@@ -98,49 +99,57 @@ module.exports = {
9899
const ignoreIndirectDependencies = Boolean(
99100
options.ignoreIndirectDependencies
100101
)
102+
const targets = []
101103

102-
return {
103-
"Program:exit"(node) {
104-
const targets = []
105-
.concat(
106-
getRequireTargets(context, true),
107-
getImportExportTargets(context, node, {
108-
includeCore: true,
109-
})
110-
)
111-
.filter(t => CORE_MODULES.has(t.moduleName))
104+
return [
105+
visitImport(context, { includeCore: true }, importTargets =>
106+
targets.push(...importTargets)
107+
),
108+
visitRequire(context, { includeCore: true }, requireTargets =>
109+
targets.push(...requireTargets)
110+
),
111+
{
112+
"Program:exit"() {
113+
for (const target of targets.filter(t =>
114+
CORE_MODULES.has(t.moduleName)
115+
)) {
116+
const name = target.moduleName
117+
const allowed =
118+
allow.indexOf(name) !== -1 ||
119+
(ignoreDirectDependencies && deps.has(name)) ||
120+
(ignoreIndirectDependencies && !deps.has(name))
112121

113-
for (const target of targets) {
114-
const name = target.moduleName
115-
const allowed =
116-
allow.indexOf(name) !== -1 ||
117-
(ignoreDirectDependencies && deps.has(name)) ||
118-
(ignoreIndirectDependencies && !deps.has(name))
122+
if (allowed) {
123+
continue
124+
}
119125

120-
if (allowed) {
121-
continue
122-
}
126+
const resolved = resolve.sync(name, {
127+
basedir: dirPath,
128+
})
129+
const isCore = resolved === name
123130

124-
const resolved = resolve.sync(name, { basedir: dirPath })
125-
const isCore = resolved === name
131+
if (isCore) {
132+
continue
133+
}
126134

127-
if (isCore) {
128-
continue
135+
context.report({
136+
node: target.node,
137+
loc: target.node.loc,
138+
message:
139+
"Unexpected import of third-party module '{{name}}'.",
140+
data: {
141+
name: path
142+
.relative(dirPath, resolved)
143+
.replace(BACK_SLASH, "/"),
144+
},
145+
})
129146
}
130-
131-
context.report({
132-
node: target.node,
133-
loc: target.node.loc,
134-
message:
135-
"Unexpected import of third-party module '{{name}}'.",
136-
data: {
137-
name: path
138-
.relative(dirPath, resolved)
139-
.replace(BACK_SLASH, "/"),
140-
},
141-
})
142-
}
147+
},
143148
},
144-
}
149+
].reduce(
150+
(mergedVisitor, thisVisitor) =>
151+
mergeVisitorsInPlace(mergedVisitor, thisVisitor),
152+
{}
153+
)
145154
},
146155
}

‎lib/rules/no-missing-import.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
const checkExistence = require("../util/check-existence")
88
const getAllowModules = require("../util/get-allow-modules")
9-
const getImportExportTargets = require("../util/get-import-export-targets")
109
const getResolvePaths = require("../util/get-resolve-paths")
1110
const getTryExtensions = require("../util/get-try-extensions")
11+
const visitImport = require("../util/visit-import")
1212

1313
module.exports = {
1414
meta: {
@@ -40,10 +40,8 @@ module.exports = {
4040
return {}
4141
}
4242

43-
return {
44-
"Program:exit"(node) {
45-
checkExistence(context, getImportExportTargets(context, node))
46-
},
47-
}
43+
return visitImport(context, {}, targets => {
44+
checkExistence(context, targets)
45+
})
4846
},
4947
}

‎lib/rules/no-missing-require.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
const checkExistence = require("../util/check-existence")
88
const getAllowModules = require("../util/get-allow-modules")
9-
const getRequireTargets = require("../util/get-require-targets")
109
const getResolvePaths = require("../util/get-resolve-paths")
1110
const getTryExtensions = require("../util/get-try-extensions")
11+
const visitRequire = require("../util/visit-require")
1212

1313
module.exports = {
1414
meta: {
@@ -40,10 +40,8 @@ module.exports = {
4040
return {}
4141
}
4242

43-
return {
44-
"Program:exit"() {
45-
checkExistence(context, getRequireTargets(context))
46-
},
47-
}
43+
return visitRequire(context, {}, targets => {
44+
checkExistence(context, targets)
45+
})
4846
},
4947
}

‎lib/rules/no-unpublished-import.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
const checkPublish = require("../util/check-publish")
88
const getAllowModules = require("../util/get-allow-modules")
99
const getConvertPath = require("../util/get-convert-path")
10-
const getImportExportTargets = require("../util/get-import-export-targets")
1110
const getResolvePaths = require("../util/get-resolve-paths")
1211
const getTryExtensions = require("../util/get-try-extensions")
12+
const visitImport = require("../util/visit-import")
1313

1414
module.exports = {
1515
meta: {
@@ -42,14 +42,8 @@ module.exports = {
4242
return {}
4343
}
4444

45-
return {
46-
"Program:exit"(node) {
47-
checkPublish(
48-
context,
49-
filePath,
50-
getImportExportTargets(context, node)
51-
)
52-
},
53-
}
45+
return visitImport(context, {}, targets => {
46+
checkPublish(context, filePath, targets)
47+
})
5448
},
5549
}

‎lib/rules/no-unpublished-require.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
const checkPublish = require("../util/check-publish")
88
const getAllowModules = require("../util/get-allow-modules")
99
const getConvertPath = require("../util/get-convert-path")
10-
const getRequireTargets = require("../util/get-require-targets")
1110
const getResolvePaths = require("../util/get-resolve-paths")
1211
const getTryExtensions = require("../util/get-try-extensions")
12+
const visitRequire = require("../util/visit-require")
1313

1414
module.exports = {
1515
meta: {
@@ -42,10 +42,8 @@ module.exports = {
4242
return {}
4343
}
4444

45-
return {
46-
"Program:exit"() {
47-
checkPublish(context, filePath, getRequireTargets(context))
48-
},
49-
}
45+
return visitRequire(context, {}, targets => {
46+
checkPublish(context, filePath, targets)
47+
})
5048
},
5149
}

‎lib/rules/no-unsupported-features/es-syntax.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { getInnermostScope } = require("eslint-utils")
99
const { Range } = require("semver") //eslint-disable-line no-unused-vars
1010
const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
1111
const getSemverRange = require("../../util/get-semver-range")
12+
const mergeVisitorsInPlace = require("../../util/merge-visitors-in-place")
1213

1314
const getOrSet = /^(?:g|s)et$/u
1415
const features = {
@@ -405,41 +406,6 @@ function normalizeScope(initialScope, node) {
405406
return scope
406407
}
407408

408-
/**
409-
* Merge two visitors.
410-
* @param {Visitor} x The visitor which is assigned.
411-
* @param {Visitor} y The visitor which is assigning.
412-
* @returns {Visitor} `x`.
413-
*/
414-
function merge(x, y) {
415-
for (const key of Object.keys(y)) {
416-
if (typeof x[key] === "function") {
417-
if (x[key]._handlers == null) {
418-
const fs = [x[key], y[key]]
419-
x[key] = dispatch.bind(null, fs)
420-
x[key]._handlers = fs
421-
} else {
422-
x[key]._handlers.push(y[key])
423-
}
424-
} else {
425-
x[key] = y[key]
426-
}
427-
}
428-
return x
429-
}
430-
431-
/**
432-
* Dispatch all given functions with a node.
433-
* @param {function[]} handlers The function list to call.
434-
* @param {Node} node The AST node to be handled.
435-
* @returns {void}
436-
*/
437-
function dispatch(handlers, node) {
438-
for (const h of handlers) {
439-
h(node)
440-
}
441-
}
442-
443409
/**
444410
* Define the visitor object as merging the rules of eslint-plugin-es.
445411
* @param {RuleContext} context The rule context.
@@ -514,7 +480,7 @@ function defineVisitor(context, options) {
514480
}
515481
},
516482
}
517-
return merge(visitor, rule.create(thisContext))
483+
return mergeVisitorsInPlace(visitor, rule.create(thisContext))
518484
}, {})
519485
)
520486
}

0 commit comments

Comments
 (0)