Skip to content

Commit b6157bf

Browse files
committed
chore: unifi TraceMap
1 parent 063d140 commit b6157bf

File tree

10 files changed

+71
-39
lines changed

10 files changed

+71
-39
lines changed

lib/eslint-utils.d.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ declare module "@eslint-community/eslint-utils" {
2828
export { ESM };
2929
}
3030
type ReferenceType = typeof READ | typeof CALL | typeof CONSTRUCT;
31-
type TraceMap<Info extends unknown = boolean> =
32-
& { [key: string]: TraceMap; }
33-
& Partial<Record<ReferenceType, Info>>;
31+
type TraceMap<Info extends unknown> = {
32+
[READ]?: Info;
33+
[CALL]?: Info;
34+
[CONSTRUCT]?: Info;
35+
[key: string]: TraceMap<Info>;
36+
}
3437
type RichNode = eslint.Rule.Node | Node;
35-
type Reference<Info extends unknown = boolean> = {
38+
type Reference<Info extends unknown> = {
3639
node: RichNode;
3740
path: string[];
3841
type: ReferenceType;

lib/rules/no-unsupported-features/node-builtins.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ const {
1616
NodeBuiltinModules,
1717
} = require("../../unsupported-features/node-builtins.js")
1818

19+
/**
20+
* @typedef {Object} TraceMap
21+
* @property {import('@eslint-community/eslint-utils').TraceMap<boolean>} globals
22+
* @property {import('@eslint-community/eslint-utils').TraceMap<boolean>} modules
23+
*/
1924
const trackMap = {
2025
globals: {
2126
queueMicrotask: {
22-
[READ]: { supported: ["12.0.0"], experimental: "11.0.0" },
27+
[READ]: { supported: ["12.0.0"], experimental: ["11.0.0"] },
2328
},
2429
require: {
2530
resolve: {

lib/rules/prefer-node-protocol.js

+37-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const mergeVisitorsInPlace = require("../util/merge-visitors-in-place")
1313

1414
const messageId = "preferNodeProtocol"
1515

16+
const supportedRangeForEsm = /** @type {import('semver').Range} */ (
17+
getSemverRange("^12.20.0 || >= 14.13.1")
18+
)
19+
const supportedRangeForCjs = /** @type {import('semver').Range} */ (
20+
getSemverRange("^14.18.0 || >= 16.0.0")
21+
)
22+
23+
/** @type {import('eslint').Rule.RuleModule} */
1624
module.exports = {
1725
meta: {
1826
docs: {
@@ -37,6 +45,13 @@ module.exports = {
3745
type: "suggestion",
3846
},
3947
create(context) {
48+
/**
49+
* @param {import('estree').Node} node
50+
* @param {object} options
51+
* @param {string} options.name
52+
* @param {number} options.argumentsLength
53+
* @returns {node is import('estree').CallExpression}
54+
*/
4055
function isCallExpression(node, { name, argumentsLength }) {
4156
if (node?.type !== "CallExpression") {
4257
return false
@@ -60,40 +75,54 @@ module.exports = {
6075
return true
6176
}
6277

78+
/**
79+
* @param {import('estree').Node} node
80+
* @returns {node is import('estree').Literal}
81+
*/
6382
function isStringLiteral(node) {
6483
return node?.type === "Literal" && typeof node.type === "string"
6584
}
6685

86+
/**
87+
* @param {import('estree').Node | undefined} node
88+
* @returns {node is import('estree').CallExpression}
89+
*/
6790
function isStaticRequire(node) {
6891
return (
92+
node != null &&
6993
isCallExpression(node, {
7094
name: "require",
7195
argumentsLength: 1,
72-
}) && isStringLiteral(node.arguments[0])
96+
}) &&
97+
isStringLiteral(node.arguments[0])
7398
)
7499
}
75100

101+
/**
102+
* @param {import('eslint').Rule.RuleContext} context
103+
* @param {import('../util/import-target.js').ModuleStyle} moduleStyle
104+
* @returns {Boolean}
105+
*/
76106
function isEnablingThisRule(context, moduleStyle) {
77107
const version = getConfiguredNodeVersion(context)
78108

79-
const supportedVersionForEsm = "^12.20.0 || >= 14.13.1"
80109
// Only check Node.js version because this rule is meaningless if configured Node.js version doesn't match semver range.
81-
if (!version.intersects(getSemverRange(supportedVersionForEsm))) {
110+
if (!version.intersects(supportedRangeForEsm)) {
82111
return false
83112
}
84113

85-
const supportedVersionForCjs = "^14.18.0 || >= 16.0.0"
86114
// Only check when using `require`
87115
if (
88116
moduleStyle === "require" &&
89-
!version.intersects(getSemverRange(supportedVersionForCjs))
117+
!version.intersects(supportedRangeForCjs)
90118
) {
91119
return false
92120
}
93121

94122
return true
95123
}
96124

125+
/** @type {import('../util/import-target.js')[]} */
97126
const targets = []
98127
return [
99128
visitImport(context, { includeCore: true }, importTargets => {
@@ -117,7 +146,7 @@ module.exports = {
117146
continue
118147
}
119148

120-
const { value } = node
149+
const { value } = /** @type {{ value: string }}*/ (node)
121150
if (
122151
typeof value !== "string" ||
123152
value.startsWith("node:") ||
@@ -134,7 +163,8 @@ module.exports = {
134163
moduleName: value,
135164
},
136165
fix(fixer) {
137-
const firstCharacterIndex = node.range[0] + 1
166+
const firstCharacterIndex =
167+
(node?.range?.[0] ?? 0) + 1
138168
return fixer.replaceTextRange(
139169
[firstCharacterIndex, firstCharacterIndex],
140170
"node:"

lib/rules/prefer-promises/dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
ReferenceTracker,
1111
} = require("@eslint-community/eslint-utils")
1212

13-
/** @type {import('@eslint-community/eslint-utils').TraceMap} */
13+
/** @type {import('@eslint-community/eslint-utils').TraceMap<boolean>} */
1414
const trackMap = {
1515
dns: {
1616
lookup: { [CALL]: true },

lib/rules/prefer-promises/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils")
88

9-
/** @type {import('@eslint-community/eslint-utils').TraceMap} */
9+
/** @type {import('@eslint-community/eslint-utils').TraceMap<boolean>} */
1010
const trackMap = {
1111
fs: {
1212
access: { [CALL]: true },

lib/unsupported-features/types.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
* @property {string[]} [supported] The node versions in which stable support was added
1515
* @property {string[]} [deprecated] The node versions in which support was removed
1616
*/
17+
18+
/**
19+
* @typedef {import('@eslint-community/eslint-utils').TraceMap<SupportInfo>} SupportVersionTree
20+
*/
1721
/**
18-
* @typedef {{ [key in UTIL_SYMBOL]?: SupportInfo } & { [key: string]: SupportVersionTree }} SupportVersionTree
22+
* @typedef {Object} SupportVersionTraceMap
23+
* @property {SupportVersionTree} globals
24+
* @property {SupportVersionTree} modules
1925
*/
2026

2127
module.exports = {}

lib/util/check-prefer-global.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
88

99
/**
10-
* @typedef {Object} TrackMap
11-
* @property {import('@eslint-community/eslint-utils').TraceMap} globals
12-
* @property {import('@eslint-community/eslint-utils').TraceMap} modules
10+
* @typedef {Object} TraceMap
11+
* @property {import('@eslint-community/eslint-utils').TraceMap<boolean>} globals
12+
* @property {import('@eslint-community/eslint-utils').TraceMap<boolean>} modules
1313
*/
1414

1515
/**
@@ -19,7 +19,7 @@ class Verifier {
1919
/**
2020
* Initialize this instance.
2121
* @param {import('eslint').Rule.RuleContext} context The rule context to report.
22-
* @param {TrackMap} trackMap The track map.
22+
* @param {TraceMap} trackMap The track map.
2323
*/
2424
constructor(context, trackMap) {
2525
this.context = context
@@ -72,7 +72,7 @@ class Verifier {
7272

7373
/**
7474
* @param {import('eslint').Rule.RuleContext} context
75-
* @param {TrackMap} trackMap [description]
75+
* @param {TraceMap} trackMap [description]
7676
* @returns {void}
7777
*/
7878
module.exports = function checkForPreferGlobal(context, trackMap) {

lib/util/check-unsupported-builtins.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ const getConfiguredNodeVersion = require("./get-configured-node-version")
1010
const getSemverRange = require("./get-semver-range")
1111
const unprefixNodeColon = require("./unprefix-node-colon")
1212

13-
/**
14-
* @typedef {Object} SupportInfo
15-
* @property {string[]} supported The stably supported version. If `null` is present, it hasn't been supported yet.
16-
* @property {string} [experimental] The added version as experimental.
17-
*/
18-
1913
/**
2014
* Parses the options.
2115
* @param {import('eslint').Rule.RuleContext} context The rule context.
@@ -34,7 +28,7 @@ function parseOptions(context) {
3428

3529
/**
3630
* Check if it has been supported.
37-
* @param {SupportInfo} info The support info.
31+
* @param {import('../unsupported-features/types.js').SupportInfo} info The support info.
3832
* @param {import('semver').Range} configured The configured version range.
3933
*/
4034
function isSupported({ supported }, configured) {
@@ -61,7 +55,7 @@ function isSupported({ supported }, configured) {
6155

6256
/**
6357
* Get the formatted text of a given supported version.
64-
* @param {SupportInfo} info The support info.
58+
* @param {import('../unsupported-features/types.js').SupportInfo} info The support info.
6559
* @returns {string | undefined}
6660
*/
6761
function supportedVersionToString({ supported }) {
@@ -80,16 +74,10 @@ function supportedVersionToString({ supported }) {
8074
return `${latest} (backported: ${backportString})`
8175
}
8276

83-
/**
84-
* @typedef {Object} TrackMap
85-
* @property {import('@eslint-community/eslint-utils').TraceMap<SupportInfo>} modules
86-
* @property {import('@eslint-community/eslint-utils').TraceMap<SupportInfo>} globals
87-
*/
88-
8977
/**
9078
* Verify the code to report unsupported APIs.
9179
* @param {import('eslint').Rule.RuleContext} context The rule context.
92-
* @param {TrackMap} trackMap The map for APIs to report.
80+
* @param {import('../unsupported-features/types.js').SupportVersionTraceMap} trackMap The map for APIs to report.
9381
* @returns {void}
9482
*/
9583
module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(

lib/util/extend-trackmap-with-node-prefix.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const isBuiltinModule = require("is-builtin-module")
44

55
/**
66
* Extend trackMap.modules with `node:` prefixed modules
7-
* @param {import('@eslint-community/eslint-utils').TraceMap} modules Like `{assert: foo}`
8-
* @returns {import('@eslint-community/eslint-utils').TraceMap} Like `{assert: foo}, "node:assert": foo}`
7+
* @param {import('@eslint-community/eslint-utils').TraceMap<boolean>} modules Like `{assert: foo}`
8+
* @returns {import('@eslint-community/eslint-utils').TraceMap<boolean>} Like `{assert: foo}, "node:assert": foo}`
99
*/
10-
module.exports = function extendTrackMapWithNodePrefix(modules) {
10+
module.exports = function extendTraceMapWithNodePrefix(modules) {
1111
const ret = {
1212
...modules,
1313
...Object.fromEntries(

lib/util/import-target.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module.exports = class ImportTarget {
9696

9797
/**
9898
* The node of a `require()` or a module declaraiton.
99-
* @type {import('estree').Node}
99+
* @type {import('estree').Node & { parent?: import('estree').Node }}
100100
*/
101101
this.node = node
102102

0 commit comments

Comments
 (0)