Skip to content

Commit 43cc2c3

Browse files
committed
chore: small steps
1 parent f581424 commit 43cc2c3

22 files changed

+155
-84
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
/test.js
66
.eslintcache
77
.vscode
8-
.idea/
8+
.idea/
9+
10+
types/

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

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

77
const path = require("path")
88
const fs = require("fs")
9-
const mapTypescriptExtension = require("../util/map-typescript-extension")
9+
const { convertTsExtensionToJs } = require("../util/map-typescript-extension")
1010
const visitImport = require("../util/visit-import")
1111

1212
/**
@@ -79,7 +79,7 @@ module.exports = {
7979
const actualExt = path.extname(filePath)
8080
const style = overrideStyle[actualExt] || defaultStyle
8181

82-
const expectedExt = mapTypescriptExtension(
82+
const expectedExt = convertTsExtensionToJs(
8383
context,
8484
filePath,
8585
actualExt

lib/rules/no-deprecated-api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ function toName(type, path) {
674674

675675
/**
676676
* Parses the options.
677-
* @param {RuleContext} context The rule context.
677+
* @param {import('eslint').Rule.RuleContext} context The rule context.
678678
* @returns {{version:Range,ignoredGlobalItems:Set<string>,ignoredModuleItems:Set<string>}} Parsed
679679
* value.
680680
*/

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ const keywords = Object.keys(features)
429429

430430
/**
431431
* Parses the options.
432-
* @param {RuleContext} context The rule context.
432+
* @param {import('eslint').Rule.RuleContext} context The rule context.
433433
* @returns {{version:Range,ignores:Set<string>}} Parsed value.
434434
*/
435435
function parseOptions(context) {
@@ -458,7 +458,7 @@ function normalizeScope(initialScope, node) {
458458

459459
/**
460460
* Define the visitor object as merging the rules of eslint-plugin-es-x.
461-
* @param {RuleContext} context The rule context.
461+
* @param {import('eslint').Rule.RuleContext} context The rule context.
462462
* @param {{version:Range,ignores:Set<string>}} options The options.
463463
* @returns {object} The defined visitor.
464464
*/

lib/util/check-existence.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ const path = require("path")
88
const exists = require("./exists")
99
const getAllowModules = require("./get-allow-modules")
1010
const isTypescript = require("./is-typescript")
11-
const mapTypescriptExtension = require("../util/map-typescript-extension")
11+
const { convertJsExtensionToTs } = require("../util/map-typescript-extension")
1212

1313
/**
1414
* Reports a missing file from ImportTarget
15-
* @param {RuleContext} context - A context to report.
15+
* @param {import('eslint').Rule.RuleContext} context - A context to report.
1616
* @param {import('../util/import-target.js')} target - A list of target information to check.
1717
* @returns {void}
1818
*/
1919
function markMissing(context, target) {
2020
context.report({
2121
node: target.node,
22-
loc: target.node.loc,
22+
loc: /** @type {import('eslint').AST.SourceLocation} */ (
23+
target.node.loc
24+
),
2325
messageId: "notFound",
24-
data: target,
26+
data: /** @type {Record<string, *>} */ (target),
2527
})
2628
}
2729

@@ -31,7 +33,7 @@ function markMissing(context, target) {
3133
* It looks up the target according to the logic of Node.js.
3234
* See Also: https://nodejs.org/api/modules.html
3335
*
34-
* @param {RuleContext} context - A context to report.
36+
* @param {import('eslint').Rule.RuleContext} context - A context to report.
3537
* @param {import('../util/import-target.js')[]} targets - A list of target information to check.
3638
* @returns {void}
3739
*/
@@ -55,15 +57,14 @@ exports.checkExistence = function checkExistence(context, targets) {
5557
let missingFile =
5658
target.filePath == null ? false : !exists(target.filePath)
5759

58-
if (missingFile && isTypescript(context)) {
60+
if (target.filePath != null && isTypescript(context)) {
5961
const parsed = path.parse(target.filePath)
6062
const pathWithoutExt = path.resolve(parsed.dir, parsed.name)
6163

62-
const reversedExts = mapTypescriptExtension(
64+
const reversedExts = convertJsExtensionToTs(
6365
context,
6466
target.filePath,
65-
parsed.ext,
66-
true
67+
parsed.ext
6768
)
6869
const reversedPaths = reversedExts.map(
6970
reversedExt => pathWithoutExt + reversedExt

lib/util/check-extraneous.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const getPackageJson = require("./get-package-json")
1212
*
1313
* It reads package.json and checks the target exists in `dependencies`.
1414
*
15-
* @param {RuleContext} context - A context to report.
15+
* @param {import('eslint').Rule.RuleContext} context - A context to report.
1616
* @param {string} filePath - The current file path.
17-
* @param {ImportTarget[]} targets - A list of target information to check.
17+
* @param {import('./import-target.js')[]} targets - A list of target information to check.
1818
* @returns {void}
1919
*/
2020
exports.checkExtraneous = function checkExtraneous(context, filePath, targets) {
@@ -43,9 +43,11 @@ exports.checkExtraneous = function checkExtraneous(context, filePath, targets) {
4343
if (extraneous) {
4444
context.report({
4545
node: target.node,
46-
loc: target.node.loc,
46+
loc: /** @type {import('eslint').AST.SourceLocation} */ (
47+
target.node.loc
48+
),
4749
messageId: "extraneous",
48-
data: target,
50+
data: /** @type {Record<string, *>} */ (target),
4951
})
5052
}
5153
}

lib/util/check-prefer-global.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
88
const extendTrackmapWithNodePrefix = require("./extend-trackmap-with-node-prefix")
99

10+
/**
11+
* @typedef {Object} TrackMap
12+
* @property {Record<string, Record<string, boolean>>} globals
13+
* @property {Record<string, Record<string, boolean | Record<string, boolean>>>} modules
14+
*/
15+
1016
/**
1117
* Verifier for `prefer-global/*` rules.
1218
*/
1319
class Verifier {
1420
/**
1521
* Initialize this instance.
16-
* @param {RuleContext} context The rule context to report.
17-
* @param {{modules:object,globals:object}} trackMap The track map.
22+
* @param {import('eslint').Rule.RuleContext} context The rule context to report.
23+
* @param {TrackMap} trackMap The track map.
1824
*/
1925
constructor(context, trackMap) {
2026
this.context = context
@@ -67,6 +73,11 @@ class Verifier {
6773
}
6874
}
6975

76+
/**
77+
* @param {import('eslint').Rule.RuleContext} context
78+
* @param {TrackMap} trackMap [description]
79+
* @returns {void}
80+
*/
7081
module.exports = function checkForPreferGlobal(context, trackMap) {
7182
new Verifier(context, trackMap).verify()
7283
}

lib/util/check-publish.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const getPackageJson = require("./get-package-json")
1515
*
1616
* It reads package.json and checks the target exists in `dependencies`.
1717
*
18-
* @param {RuleContext} context - A context to report.
18+
* @param {import('eslint').Rule.RuleContext} context - A context to report.
1919
* @param {string} filePath - The current file path.
20-
* @param {ImportTarget[]} targets - A list of target information to check.
20+
* @param {import('./import-target.js')[]} targets - A list of target information to check.
2121
* @returns {void}
2222
*/
2323
exports.checkPublish = function checkPublish(context, filePath, targets) {
@@ -42,15 +42,13 @@ exports.checkPublish = function checkPublish(context, filePath, targets) {
4242
}
4343
const npmignore = getNpmignore(filePath)
4444
const devDependencies = new Set(
45-
Object.keys(packageInfo.devDependencies || {})
46-
)
47-
const dependencies = new Set(
48-
[].concat(
49-
Object.keys(packageInfo.dependencies || {}),
50-
Object.keys(packageInfo.peerDependencies || {}),
51-
Object.keys(packageInfo.optionalDependencies || {})
52-
)
45+
Object.keys(packageInfo.devDependencies ?? {})
5346
)
47+
const dependencies = new Set([
48+
...Object.keys(packageInfo?.dependencies ?? {}),
49+
...Object.keys(packageInfo?.peerDependencies ?? {}),
50+
...Object.keys(packageInfo?.optionalDependencies ?? {}),
51+
])
5452

5553
if (!npmignore.match(toRelative(filePath))) {
5654
// This file is published, so this cannot import private files.

lib/util/check-restricted.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const { Minimatch } = require("minimatch")
1616

1717
/**
1818
* Check if matched or not.
19-
* @param {InstanceType<Minimatch>} matcher The matcher.
19+
* @param {Minimatch} matcher The matcher.
2020
* @param {boolean} absolute The flag that the matcher is for absolute paths.
21-
* @param {ImportTarget} importee The importee information.
21+
* @param {import('./import-target.js')} importee The importee information.
2222
*/
2323
function match(matcher, absolute, { filePath, name }) {
2424
if (absolute) {
@@ -54,7 +54,7 @@ class Restriction {
5454

5555
/**
5656
* Check if a given importee is disallowed.
57-
* @param {ImportTarget} importee The importee to check.
57+
* @param {import('./import-target.js')} importee The importee to check.
5858
* @returns {boolean} `true` if the importee is disallowed.
5959
*/
6060
match(importee) {
@@ -82,17 +82,17 @@ function createRestriction(def) {
8282

8383
/**
8484
* Create restrictions.
85-
* @param {(string | DefinitionData | GlobDefinition)[]} defs Definitions.
86-
* @returns {(Restriction | GlobRestriction)[]} Created restrictions.
85+
* @param {(string | DefinitionData)[]} defs Definitions.
86+
* @returns {(Restriction)[]} Created restrictions.
8787
*/
8888
function createRestrictions(defs) {
8989
return (defs || []).map(createRestriction)
9090
}
9191

9292
/**
9393
* Checks if given importees are disallowed or not.
94-
* @param {RuleContext} context - A context to report.
95-
* @param {ImportTarget[]} targets - A list of target information to check.
94+
* @param {import('eslint').Rule.RuleContext} context - A context to report.
95+
* @param {import('./import-target.js')[]} targets - A list of target information to check.
9696
* @returns {void}
9797
*/
9898
exports.checkForRestriction = function checkForRestriction(context, targets) {

lib/util/check-unsupported-builtins.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const unprefixNodeColon = require("./unprefix-node-colon")
1818

1919
/**
2020
* Parses the options.
21-
* @param {RuleContext} context The rule context.
21+
* @param {import('eslint').Rule.RuleContext} context The rule context.
2222
* @returns {{version:Range,ignores:Set<string>}} Parsed value.
2323
*/
2424
function parseOptions(context) {
@@ -75,7 +75,7 @@ function supportedVersionToString({ supported }) {
7575

7676
/**
7777
* Verify the code to report unsupported APIs.
78-
* @param {RuleContext} context The rule context.
78+
* @param {import('eslint').Rule.RuleContext} context The rule context.
7979
* @param {{modules:object,globals:object}} trackMap The map for APIs to report.
8080
* @returns {void}
8181
*/

lib/util/exists.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ module.exports = function exists(filePath) {
5050
fs.statSync(relativePath).isFile() &&
5151
existsCaseSensitive(relativePath)
5252
} catch (error) {
53-
if (error.code !== "ENOENT") {
53+
if (
54+
/** @type {NodeJS.ErrnoException} */ (error).code !== "ENOENT"
55+
) {
5456
throw error
5557
}
5658
result = false

lib/util/get-allow-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function get(option) {
2626
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
2727
* 3. This returns `[]`.
2828
*
29-
* @param {RuleContext} context - The rule context.
29+
* @param {import('eslint').Rule.RuleContext} context - The rule context.
3030
* @returns {string[]} A list of extensions.
3131
*/
3232
module.exports = function getAllowModules(context) {

lib/util/get-convert-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function parse(option) {
139139
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
140140
* 3. This returns a function of identity.
141141
*
142-
* @param {RuleContext} context - The rule context.
142+
* @param {import('eslint').Rule.RuleContext} context - The rule context.
143143
* @returns {function} A function which converts a path.
144144
*/
145145
module.exports = function getConvertPath(context) {

lib/util/get-npmignore.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,31 @@ function isAncestorFiles(filePath) {
2929
}
3030

3131
/**
32-
* @param {function} f - A function.
33-
* @param {function} g - A function.
34-
* @returns {function} A logical-and function of `f` and `g`.
32+
* @param {(filePath: string) => boolean} f - A function.
33+
* @param {(filePath: string) => boolean} g - A function.
34+
* @returns {(filePath: string) => boolean} A logical-and function of `f` and `g`.
3535
*/
3636
function and(f, g) {
3737
return filePath => f(filePath) && g(filePath)
3838
}
3939

4040
/**
41-
* @param {function} f - A function.
42-
* @param {function} g - A function.
43-
* @param {function|null} h - A function.
44-
* @returns {function} A logical-or function of `f`, `g`, and `h`.
41+
* @param {(filePath: string) => boolean} f - A function.
42+
* @param {(filePath: string) => boolean} g - A function.
43+
* @param {(filePath: string) => boolean} [h] - A function.
44+
* @returns {(filePath: string) => boolean} A logical-or function of `f`, `g`, and `h`.
4545
*/
4646
function or(f, g, h) {
47-
return filePath => f(filePath) || g(filePath) || (h && h(filePath))
47+
if (h == null) {
48+
return filePath => f(filePath) || g(filePath)
49+
}
50+
51+
return filePath => f(filePath) || g(filePath) || h(filePath)
4852
}
4953

5054
/**
51-
* @param {function} f - A function.
52-
* @returns {function} A logical-not function of `f`.
55+
* @param {(filePath: string) => boolean} f - A function.
56+
* @returns {(filePath: string) => boolean} A logical-not function of `f`.
5357
*/
5458
function not(f) {
5559
return filePath => !f(filePath)
@@ -59,7 +63,7 @@ function not(f) {
5963
* Creates a function which checks whether or not a given file is ignoreable.
6064
*
6165
* @param {object} p - An object of package.json.
62-
* @returns {function} A function which checks whether or not a given file is ignoreable.
66+
* @returns {(filePath: string) => boolean} A function which checks whether or not a given file is ignoreable.
6367
*/
6468
function filterNeverIgnoredFiles(p) {
6569
const basedir = path.dirname(p.filePath)

lib/util/get-resolve-paths.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function get(option) {
2626
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
2727
* 3. This returns `[]`.
2828
*
29-
* @param {RuleContext} context - The rule context.
29+
* @param {import('eslint').Rule.RuleContext} context - The rule context.
3030
* @returns {string[]} A list of extensions.
3131
*/
3232
module.exports = function getResolvePaths(context, optionIndex = 0) {

lib/util/get-try-extensions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function get(option) {
4545
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
4646
* 3. This returns `[".js", ".json", ".node", ".mjs", ".cjs"]`.
4747
*
48-
* @param {RuleContext} context - The rule context.
48+
* @param {import('eslint').Rule.RuleContext} context - The rule context.
4949
* @returns {string[]} A list of extensions.
5050
*/
5151
module.exports = function getTryExtensions(context, optionIndex = 0) {

0 commit comments

Comments
 (0)