Skip to content

Commit c75e343

Browse files
committed
chore: Remove (almost) all type casting
1 parent 0379658 commit c75e343

20 files changed

+155
-136
lines changed

lib/configs/_commons.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use strict"
22

3-
module.exports.commonRules = /** @type {const} */ ({
3+
/**
4+
* @type {import('eslint').Linter.RulesRecord}
5+
*/
6+
module.exports.commonRules = {
47
"n/no-deprecated-api": "error",
58
"n/no-extraneous-import": "error",
69
"n/no-extraneous-require": "error",
@@ -16,4 +19,4 @@ module.exports.commonRules = /** @type {const} */ ({
1619
"n/no-unsupported-features/node-builtins": "error",
1720
"n/process-exit-as-throw": "error",
1821
"n/hashbang": "error",
19-
})
22+
}

lib/eslint-utils.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
declare module "eslint-plugin-es-x" {
2-
// @ts-ignore
32
export const rules: NonNullable<import('eslint').ESLint.Plugin["rules"]>;
43
}
54

65
declare module "@eslint-community/eslint-utils" {
7-
// @ts-ignore
86
import * as estree from 'estree';
9-
// @ts-ignore
107
import * as eslint from 'eslint';
118

129
type Node = estree.Node | estree.Expression;

lib/rules/exports-style.js

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
*/
55
"use strict"
66

7-
/**
8-
* @typedef {import('estree').Node & { parent?: Node }} Node
9-
*/
7+
const { hasParentNode } = require("../util/has-parent-node.js")
108

119
/*istanbul ignore next */
1210
/**
1311
* This function is copied from https://github.com/eslint/eslint/blob/2355f8d0de1d6732605420d15ddd4f1eee3c37b6/lib/ast-utils.js#L648-L684
1412
*
15-
* @param {Node} node - The node to get.
13+
* @param {import('estree').Node} node - The node to get.
1614
* @returns {string | null | undefined} The property name if static. Otherwise, null.
1715
* @private
1816
*/
@@ -44,12 +42,7 @@ function getStaticPropertyName(node) {
4442
break
4543

4644
case "Identifier":
47-
if (
48-
!(
49-
/** @type {import('estree').MemberExpression} */ (node)
50-
.computed
51-
)
52-
) {
45+
if (node.type === "MemberExpression" && node.computed === false) {
5346
return prop.name
5447
}
5548
break
@@ -63,11 +56,12 @@ function getStaticPropertyName(node) {
6356
/**
6457
* Checks whether the given node is assignee or not.
6558
*
66-
* @param {Node} node - The node to check.
59+
* @param {import('estree').Node} node - The node to check.
6760
* @returns {boolean} `true` if the node is assignee.
6861
*/
6962
function isAssignee(node) {
7063
return (
64+
hasParentNode(node) &&
7165
node.parent?.type === "AssignmentExpression" &&
7266
node.parent.left === node
7367
)
@@ -79,15 +73,16 @@ function isAssignee(node) {
7973
* This is used to distinguish 2 assignees belong to the same assignment.
8074
* If the node is not an assignee, this returns null.
8175
*
82-
* @param {Node} leafNode - The node to get.
83-
* @returns {Node|null} The top assignment expression node, or null.
76+
* @param {import('estree').Node} leafNode - The node to get.
77+
* @returns {import('estree').Node | null} The top assignment expression node, or null.
8478
*/
8579
function getTopAssignment(leafNode) {
8680
let node = leafNode
8781

8882
// Skip MemberExpressions.
8983
while (
90-
node.parent?.type === "MemberExpression" &&
84+
hasParentNode(node) &&
85+
node.parent.type === "MemberExpression" &&
9186
node.parent.object === node
9287
) {
9388
node = node.parent
@@ -99,7 +94,7 @@ function getTopAssignment(leafNode) {
9994
}
10095

10196
// Find the top.
102-
while (node.parent?.type === "AssignmentExpression") {
97+
while (hasParentNode(node) && node.parent.type === "AssignmentExpression") {
10398
node = node.parent
10499
}
105100

@@ -109,35 +104,41 @@ function getTopAssignment(leafNode) {
109104
/**
110105
* Gets top assignment nodes of the given node list.
111106
*
112-
* @param {Node[]} nodes - The node list to get.
113-
* @returns {Node[]} Gotten top assignment nodes.
107+
* @param {import('estree').Node[]} nodes - The node list to get.
108+
* @returns {import('estree').Node[]} Gotten top assignment nodes.
114109
*/
115110
function createAssignmentList(nodes) {
116-
return /** @type {Node[]} */ (nodes.map(getTopAssignment).filter(Boolean))
111+
return nodes.map(getTopAssignment).filter(input => input != null)
117112
}
118113

119114
/**
120115
* Gets the reference of `module.exports` from the given scope.
121116
*
122117
* @param {import('eslint').Scope.Scope} scope - The scope to get.
123-
* @returns {Node[]} Gotten MemberExpression node list.
118+
* @returns {import('estree').Node[]} Gotten MemberExpression node list.
124119
*/
125120
function getModuleExportsNodes(scope) {
126121
const variable = scope.set.get("module")
127122
if (variable == null) {
128123
return []
129124
}
130-
return variable.references
131-
.map(
132-
reference =>
133-
/** @type {Node & { parent: Node }} */ (reference.identifier)
134-
.parent
135-
)
136-
.filter(
137-
node =>
138-
node?.type === "MemberExpression" &&
139-
getStaticPropertyName(node) === "exports"
140-
)
125+
126+
/** @type {import('estree').Node[]} */
127+
const nodes = []
128+
129+
for (const reference of variable.references) {
130+
if (hasParentNode(reference.identifier) === false) {
131+
continue
132+
}
133+
const node = reference.identifier.parent
134+
if (
135+
node.type === "MemberExpression" &&
136+
getStaticPropertyName(node) === "exports"
137+
) {
138+
nodes.push(node)
139+
}
140+
}
141+
return nodes
141142
}
142143

143144
/**
@@ -156,7 +157,7 @@ function getExportsNodes(scope) {
156157
}
157158

158159
/**
159-
* @param {Node} property
160+
* @param {import('estree').Node} property
160161
* @param {import('eslint').SourceCode} sourceCode
161162
* @returns {string | null}
162163
*/
@@ -210,31 +211,36 @@ function getReplacementForProperty(property, sourceCode) {
210211

211212
/**
212213
* Check for a top level module.exports = { ... }
213-
* @param {Node} node
214+
* @param {import('estree').Node} node
214215
* @returns {node is {parent: import('estree').AssignmentExpression & {parent: import('estree').ExpressionStatement, right: import('estree').ObjectExpression}}}
215216
*/
216217
function isModuleExportsObjectAssignment(node) {
217218
return (
219+
hasParentNode(node) &&
218220
node.parent?.type === "AssignmentExpression" &&
221+
hasParentNode(node.parent) &&
219222
node.parent?.parent?.type === "ExpressionStatement" &&
223+
hasParentNode(node.parent.parent) &&
220224
node.parent.parent.parent?.type === "Program" &&
221225
node.parent.right.type === "ObjectExpression"
222226
)
223227
}
224228

225229
/**
226230
* Check for module.exports.foo or module.exports.bar reference or assignment
227-
* @param {Node} node
231+
* @param {import('estree').Node} node
228232
* @returns {node is import('estree').MemberExpression}
229233
*/
230234
function isModuleExportsReference(node) {
231235
return (
232-
node.parent?.type === "MemberExpression" && node.parent.object === node
236+
hasParentNode(node) &&
237+
node.parent?.type === "MemberExpression" &&
238+
node.parent.object === node
233239
)
234240
}
235241

236242
/**
237-
* @param {Node} node
243+
* @param {import('estree').Node} node
238244
* @param {import('eslint').SourceCode} sourceCode
239245
* @param {import('eslint').Rule.RuleFixer} fixer
240246
* @returns {import('eslint').Rule.Fix | null}
@@ -307,16 +313,17 @@ module.exports = {
307313
* module.exports = foo
308314
* ^^^^^^^^^^^^^^^^
309315
*
310-
* @param {Node} node - The node of `exports`/`module.exports`.
311-
* @returns {import('estree').SourceLocation} The location info of reports.
316+
* @param {import('estree').Node} node - The node of `exports`/`module.exports`.
317+
* @returns {import('estree').SourceLocation | undefined} The location info of reports.
312318
*/
313319
function getLocation(node) {
314320
const token = sourceCode.getTokenAfter(node)
321+
if (node.loc?.start == null || token?.loc?.end == null) {
322+
return
323+
}
315324
return {
316-
start: /** @type {import('estree').SourceLocation} */ (node.loc)
317-
.start,
318-
end: /** @type {import('estree').SourceLocation} */ (token?.loc)
319-
?.end,
325+
start: node.loc?.start,
326+
end: token?.loc?.end,
320327
}
321328
}
322329

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ module.exports = {
9595
messageId: "requireExt",
9696
data: { ext: expectedExt },
9797
fix(fixer) {
98-
const index =
99-
/** @type {[number, number]} */ (node.range)[1] - 1
98+
if (node.range == null) {
99+
return null
100+
}
101+
102+
const index = node.range[1] - 1
100103
return fixer.insertTextBeforeRange(
101104
[index, index],
102105
expectedExt
@@ -121,8 +124,11 @@ module.exports = {
121124

122125
if (otherExtensions.length === 1) {
123126
descriptor.fix = fixer => {
127+
if (node.range == null) {
128+
return null
129+
}
130+
124131
const index = name.lastIndexOf(currentExt)
125-
// @ts-expect-error - Range is defined...
126132
const start = node.range[0] + 1 + index
127133
const end = start + currentExt.length
128134
return fixer.removeRange([start, end])

lib/rules/global-require.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ module.exports = {
6767
sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
6868

6969
if (
70-
/** @type {import('estree').Identifier} */ (node.callee)
71-
.name === "require" &&
70+
node.callee.type === "Identifier" &&
71+
node.callee.name === "require" &&
7272
!isShadowed(currentScope, node.callee)
7373
) {
7474
const isGoodRequire = (

lib/rules/no-callback-literal.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ module.exports = {
2828
return {
2929
CallExpression(node) {
3030
const errorArg = node.arguments[0]
31-
const calleeName = /** @type {import('estree').Identifier} */ (
32-
node.callee
33-
).name
3431

3532
if (
3633
errorArg &&
3734
!couldBeError(errorArg) &&
38-
callbackNames.includes(calleeName)
35+
node.callee.type === "Identifier" &&
36+
callbackNames.includes(node.callee.name)
3937
) {
4038
context.report({
4139
node,

lib/rules/no-deprecated-api.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,7 @@ const globals = {
686686
replacedBy: [{ name: "'global'", supported: "0.1.27" }],
687687
},
688688
},
689-
process:
690-
/** @type {import('../unsupported-features/types.js').DeprecatedInfoTraceMap} */ (
691-
modules.process
692-
),
689+
process: modules.process,
693690
}
694691

695692
/**
@@ -704,14 +701,14 @@ function toReplaceMessage(replacedBy, version) {
704701

705702
if (Array.isArray(replacedBy)) {
706703
message = replacedBy
707-
.filter(
708-
({ supported }) =>
709-
!version.intersects(
710-
/** @type {import('semver').Range} */ (
711-
getSemverRange(`<${supported}`)
712-
)
713-
)
714-
)
704+
.filter(({ supported }) => {
705+
const range = getSemverRange(`<${supported}`)
706+
if (range == null) {
707+
return false
708+
}
709+
710+
return !version.intersects(range)
711+
})
715712
.map(({ name }) => name)
716713
.join(" or ")
717714
}
@@ -818,9 +815,6 @@ module.exports = {
818815

819816
context.report({
820817
node,
821-
loc: /** @type {NonNullable<import('estree').Node["loc"]>} */ (
822-
node.loc
823-
),
824818
messageId,
825819
data,
826820
})

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ module.exports = {
137137

138138
context.report({
139139
node: target.node,
140-
loc: /** @type {NonNullable<import('estree').Node["loc"]>} */ (
141-
target.node.loc
142-
),
143140
messageId: "unexpectedImport",
144141
data: {
145142
name: path

lib/rules/no-mixed-requires.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,11 @@ module.exports = {
237237
const found = {}
238238

239239
for (const declaration of declarations) {
240-
if (getDeclarationType(declaration.init) === DECL_REQUIRE) {
241-
found[
242-
inferModuleType(
243-
/** @type {import('estree').Expression} */ (
244-
declaration.init
245-
)
246-
)
247-
] = true
240+
if (
241+
declaration.init != null &&
242+
getDeclarationType(declaration.init) === DECL_REQUIRE
243+
) {
244+
found[inferModuleType(declaration.init)] = true
248245
}
249246
}
250247

0 commit comments

Comments
 (0)