Skip to content

Commit

Permalink
chore: small steps
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood committed Feb 23, 2024
1 parent f581424 commit 43cc2c3
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 84 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
/test.js
.eslintcache
.vscode
.idea/
.idea/

types/
4 changes: 2 additions & 2 deletions lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const path = require("path")
const fs = require("fs")
const mapTypescriptExtension = require("../util/map-typescript-extension")
const { convertTsExtensionToJs } = require("../util/map-typescript-extension")
const visitImport = require("../util/visit-import")

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports = {
const actualExt = path.extname(filePath)
const style = overrideStyle[actualExt] || defaultStyle

const expectedExt = mapTypescriptExtension(
const expectedExt = convertTsExtensionToJs(
context,
filePath,
actualExt
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ function toName(type, path) {

/**
* Parses the options.
* @param {RuleContext} context The rule context.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @returns {{version:Range,ignoredGlobalItems:Set<string>,ignoredModuleItems:Set<string>}} Parsed
* value.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unsupported-features/es-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ const keywords = Object.keys(features)

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

/**
* Define the visitor object as merging the rules of eslint-plugin-es-x.
* @param {RuleContext} context The rule context.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @param {{version:Range,ignores:Set<string>}} options The options.
* @returns {object} The defined visitor.
*/
Expand Down
19 changes: 10 additions & 9 deletions lib/util/check-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ const path = require("path")
const exists = require("./exists")
const getAllowModules = require("./get-allow-modules")
const isTypescript = require("./is-typescript")
const mapTypescriptExtension = require("../util/map-typescript-extension")
const { convertJsExtensionToTs } = require("../util/map-typescript-extension")

/**
* Reports a missing file from ImportTarget
* @param {RuleContext} context - A context to report.
* @param {import('eslint').Rule.RuleContext} context - A context to report.
* @param {import('../util/import-target.js')} target - A list of target information to check.
* @returns {void}
*/
function markMissing(context, target) {
context.report({
node: target.node,
loc: target.node.loc,
loc: /** @type {import('eslint').AST.SourceLocation} */ (
target.node.loc
),
messageId: "notFound",
data: target,
data: /** @type {Record<string, *>} */ (target),
})
}

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

if (missingFile && isTypescript(context)) {
if (target.filePath != null && isTypescript(context)) {
const parsed = path.parse(target.filePath)
const pathWithoutExt = path.resolve(parsed.dir, parsed.name)

const reversedExts = mapTypescriptExtension(
const reversedExts = convertJsExtensionToTs(
context,
target.filePath,
parsed.ext,
true
parsed.ext
)
const reversedPaths = reversedExts.map(
reversedExt => pathWithoutExt + reversedExt
Expand Down
10 changes: 6 additions & 4 deletions lib/util/check-extraneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const getPackageJson = require("./get-package-json")
*
* It reads package.json and checks the target exists in `dependencies`.
*
* @param {RuleContext} context - A context to report.
* @param {import('eslint').Rule.RuleContext} context - A context to report.
* @param {string} filePath - The current file path.
* @param {ImportTarget[]} targets - A list of target information to check.
* @param {import('./import-target.js')[]} targets - A list of target information to check.
* @returns {void}
*/
exports.checkExtraneous = function checkExtraneous(context, filePath, targets) {
Expand Down Expand Up @@ -43,9 +43,11 @@ exports.checkExtraneous = function checkExtraneous(context, filePath, targets) {
if (extraneous) {
context.report({
node: target.node,
loc: target.node.loc,
loc: /** @type {import('eslint').AST.SourceLocation} */ (
target.node.loc
),
messageId: "extraneous",
data: target,
data: /** @type {Record<string, *>} */ (target),
})
}
}
Expand Down
15 changes: 13 additions & 2 deletions lib/util/check-prefer-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
const extendTrackmapWithNodePrefix = require("./extend-trackmap-with-node-prefix")

/**
* @typedef {Object} TrackMap
* @property {Record<string, Record<string, boolean>>} globals
* @property {Record<string, Record<string, boolean | Record<string, boolean>>>} modules
*/

/**
* Verifier for `prefer-global/*` rules.
*/
class Verifier {
/**
* Initialize this instance.
* @param {RuleContext} context The rule context to report.
* @param {{modules:object,globals:object}} trackMap The track map.
* @param {import('eslint').Rule.RuleContext} context The rule context to report.
* @param {TrackMap} trackMap The track map.
*/
constructor(context, trackMap) {
this.context = context
Expand Down Expand Up @@ -67,6 +73,11 @@ class Verifier {
}
}

/**
* @param {import('eslint').Rule.RuleContext} context
* @param {TrackMap} trackMap [description]
* @returns {void}
*/
module.exports = function checkForPreferGlobal(context, trackMap) {
new Verifier(context, trackMap).verify()
}
18 changes: 8 additions & 10 deletions lib/util/check-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const getPackageJson = require("./get-package-json")
*
* It reads package.json and checks the target exists in `dependencies`.
*
* @param {RuleContext} context - A context to report.
* @param {import('eslint').Rule.RuleContext} context - A context to report.
* @param {string} filePath - The current file path.
* @param {ImportTarget[]} targets - A list of target information to check.
* @param {import('./import-target.js')[]} targets - A list of target information to check.
* @returns {void}
*/
exports.checkPublish = function checkPublish(context, filePath, targets) {
Expand All @@ -42,15 +42,13 @@ exports.checkPublish = function checkPublish(context, filePath, targets) {
}
const npmignore = getNpmignore(filePath)
const devDependencies = new Set(
Object.keys(packageInfo.devDependencies || {})
)
const dependencies = new Set(
[].concat(
Object.keys(packageInfo.dependencies || {}),
Object.keys(packageInfo.peerDependencies || {}),
Object.keys(packageInfo.optionalDependencies || {})
)
Object.keys(packageInfo.devDependencies ?? {})
)
const dependencies = new Set([
...Object.keys(packageInfo?.dependencies ?? {}),
...Object.keys(packageInfo?.peerDependencies ?? {}),
...Object.keys(packageInfo?.optionalDependencies ?? {}),
])

if (!npmignore.match(toRelative(filePath))) {
// This file is published, so this cannot import private files.
Expand Down
14 changes: 7 additions & 7 deletions lib/util/check-restricted.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const { Minimatch } = require("minimatch")

/**
* Check if matched or not.
* @param {InstanceType<Minimatch>} matcher The matcher.
* @param {Minimatch} matcher The matcher.
* @param {boolean} absolute The flag that the matcher is for absolute paths.
* @param {ImportTarget} importee The importee information.
* @param {import('./import-target.js')} importee The importee information.
*/
function match(matcher, absolute, { filePath, name }) {
if (absolute) {
Expand Down Expand Up @@ -54,7 +54,7 @@ class Restriction {

/**
* Check if a given importee is disallowed.
* @param {ImportTarget} importee The importee to check.
* @param {import('./import-target.js')} importee The importee to check.
* @returns {boolean} `true` if the importee is disallowed.
*/
match(importee) {
Expand Down Expand Up @@ -82,17 +82,17 @@ function createRestriction(def) {

/**
* Create restrictions.
* @param {(string | DefinitionData | GlobDefinition)[]} defs Definitions.
* @returns {(Restriction | GlobRestriction)[]} Created restrictions.
* @param {(string | DefinitionData)[]} defs Definitions.
* @returns {(Restriction)[]} Created restrictions.
*/
function createRestrictions(defs) {
return (defs || []).map(createRestriction)
}

/**
* Checks if given importees are disallowed or not.
* @param {RuleContext} context - A context to report.
* @param {ImportTarget[]} targets - A list of target information to check.
* @param {import('eslint').Rule.RuleContext} context - A context to report.
* @param {import('./import-target.js')[]} targets - A list of target information to check.
* @returns {void}
*/
exports.checkForRestriction = function checkForRestriction(context, targets) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const unprefixNodeColon = require("./unprefix-node-colon")

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

/**
* Verify the code to report unsupported APIs.
* @param {RuleContext} context The rule context.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @param {{modules:object,globals:object}} trackMap The map for APIs to report.
* @returns {void}
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/util/exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ module.exports = function exists(filePath) {
fs.statSync(relativePath).isFile() &&
existsCaseSensitive(relativePath)
} catch (error) {
if (error.code !== "ENOENT") {
if (
/** @type {NodeJS.ErrnoException} */ (error).code !== "ENOENT"
) {
throw error
}
result = false
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-allow-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function get(option) {
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
* 3. This returns `[]`.
*
* @param {RuleContext} context - The rule context.
* @param {import('eslint').Rule.RuleContext} context - The rule context.
* @returns {string[]} A list of extensions.
*/
module.exports = function getAllowModules(context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-convert-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function parse(option) {
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
* 3. This returns a function of identity.
*
* @param {RuleContext} context - The rule context.
* @param {import('eslint').Rule.RuleContext} context - The rule context.
* @returns {function} A function which converts a path.
*/
module.exports = function getConvertPath(context) {
Expand Down
26 changes: 15 additions & 11 deletions lib/util/get-npmignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,31 @@ function isAncestorFiles(filePath) {
}

/**
* @param {function} f - A function.
* @param {function} g - A function.
* @returns {function} A logical-and function of `f` and `g`.
* @param {(filePath: string) => boolean} f - A function.
* @param {(filePath: string) => boolean} g - A function.
* @returns {(filePath: string) => boolean} A logical-and function of `f` and `g`.
*/
function and(f, g) {
return filePath => f(filePath) && g(filePath)
}

/**
* @param {function} f - A function.
* @param {function} g - A function.
* @param {function|null} h - A function.
* @returns {function} A logical-or function of `f`, `g`, and `h`.
* @param {(filePath: string) => boolean} f - A function.
* @param {(filePath: string) => boolean} g - A function.
* @param {(filePath: string) => boolean} [h] - A function.
* @returns {(filePath: string) => boolean} A logical-or function of `f`, `g`, and `h`.
*/
function or(f, g, h) {
return filePath => f(filePath) || g(filePath) || (h && h(filePath))
if (h == null) {
return filePath => f(filePath) || g(filePath)
}

return filePath => f(filePath) || g(filePath) || h(filePath)
}

/**
* @param {function} f - A function.
* @returns {function} A logical-not function of `f`.
* @param {(filePath: string) => boolean} f - A function.
* @returns {(filePath: string) => boolean} A logical-not function of `f`.
*/
function not(f) {
return filePath => !f(filePath)
Expand All @@ -59,7 +63,7 @@ function not(f) {
* Creates a function which checks whether or not a given file is ignoreable.
*
* @param {object} p - An object of package.json.
* @returns {function} A function which checks whether or not a given file is ignoreable.
* @returns {(filePath: string) => boolean} A function which checks whether or not a given file is ignoreable.
*/
function filterNeverIgnoredFiles(p) {
const basedir = path.dirname(p.filePath)
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-resolve-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function get(option) {
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
* 3. This returns `[]`.
*
* @param {RuleContext} context - The rule context.
* @param {import('eslint').Rule.RuleContext} context - The rule context.
* @returns {string[]} A list of extensions.
*/
module.exports = function getResolvePaths(context, optionIndex = 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-try-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function get(option) {
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
* 3. This returns `[".js", ".json", ".node", ".mjs", ".cjs"]`.
*
* @param {RuleContext} context - The rule context.
* @param {import('eslint').Rule.RuleContext} context - The rule context.
* @returns {string[]} A list of extensions.
*/
module.exports = function getTryExtensions(context, optionIndex = 0) {
Expand Down

0 comments on commit 43cc2c3

Please sign in to comment.