Skip to content

Commit

Permalink
feat: remove dependencies for Node built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Oct 29, 2024
1 parent 1752feb commit 55983cf
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 104 deletions.
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"jackspeak": "=2.1.1",
"jsonc-parser": "=3.2.0",
"linklocal": "^2.8.2",
"lodash.isarray": "^4.0.0",
"markdownlint-cli": "~0.35",
"mocha": "^3.5.3",
"npm-which": "^3.0.1",
Expand All @@ -110,23 +109,14 @@
},
"dependencies": {
"@rtsao/scc": "^1.1.0",
"array-includes": "^3.1.8",
"array.prototype.findlastindex": "^1.2.5",
"array.prototype.flat": "^1.3.2",
"array.prototype.flatmap": "^1.3.2",
"debug": "^3.2.7",
"doctrine": "^2.1.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-module-utils": "^2.12.0",
"hasown": "^2.0.2",
"is-core-module": "^2.15.1",
"is-glob": "^4.0.3",
"minimatch": "^3.1.2",
"object.fromentries": "^2.0.8",
"object.groupby": "^1.0.3",
"object.values": "^1.2.0",
"semver": "^6.3.1",
"string.prototype.trimend": "^1.0.8",
"tsconfig-paths": "^3.15.0"
}
}
4 changes: 2 additions & 2 deletions resolvers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ If the resolver cannot resolve `source` relative to `file`, it should just retur
Here is most of the [Node resolver] at the time of this writing. It is just a wrapper around substack/Browserify's synchronous [`resolve`]:

```js
var { isBuiltin } = require('node:module');
var resolve = require('resolve/sync');
var isCoreModule = require('is-core-module');
exports.resolve = function (source, file, config) {
if (isCoreModule(source)) return { found: true, path: null };
if (isBuiltin(source)) return { found: true, path: null };
try {
return { found: true, path: resolve(source, opts(file, config)) };
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { isBuiltin } = require('node:module');
const resolve = require('resolve/sync');
const isCoreModule = require('is-core-module');
const path = require('path');

const log = require('debug')('eslint-plugin-import:resolver:node');
Expand Down Expand Up @@ -49,7 +49,7 @@ exports.resolve = function (source, file, config) {
log('Resolving:', source, 'from:', file);
let resolvedPath;

if (isCoreModule(source)) {
if (isBuiltin(source)) {
log('resolved to core');
return { found: true, path: null };
}
Expand Down
1 change: 0 additions & 1 deletion resolvers/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"homepage": "https://github.com/import-js/eslint-plugin-import",
"dependencies": {
"debug": "^3.2.7",
"is-core-module": "^2.13.0",
"resolve": "^1.22.4"
},
"devDependencies": {
Expand Down
24 changes: 10 additions & 14 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

const findRoot = require('find-root');
const path = require('path');
const { isBuiltin } = require('node:module');
const isEqual = require('lodash/isEqual');
const interpret = require('interpret');
const existsSync = require('fs').existsSync;
const isCore = require('is-core-module');
const resolve = require('resolve/sync');
const semver = require('semver');
const hasOwn = require('hasown');
const isRegex = require('is-regex');
const isArray = Array.isArray;
const keys = Object.keys;
const assign = Object.assign;

const log = require('debug')('eslint-plugin-import:resolver:webpack');

Expand All @@ -22,7 +18,7 @@ function registerCompiler(moduleDescriptor) {
if (moduleDescriptor) {
if (typeof moduleDescriptor === 'string') {
require(moduleDescriptor);
} else if (!isArray(moduleDescriptor)) {
} else if (!Array.isArray(moduleDescriptor)) {
moduleDescriptor.register(require(moduleDescriptor.module));
} else {
for (let i = 0; i < moduleDescriptor.length; i++) {
Expand All @@ -38,7 +34,7 @@ function registerCompiler(moduleDescriptor) {
}

function findConfigPath(configPath, packageDir) {
const extensions = keys(interpret.extensions).sort(function (a, b) {
const extensions = Object.keys(interpret.extensions).sort(function (a, b) {
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
});
let extension;
Expand Down Expand Up @@ -79,7 +75,7 @@ function findExternal(source, externals, context, resolveSync) {
if (typeof externals === 'string') { return source === externals; }

// array: recurse
if (isArray(externals)) {
if (Array.isArray(externals)) {
return externals.some(function (e) { return findExternal(source, e, context, resolveSync); });
}

Expand Down Expand Up @@ -133,7 +129,7 @@ function findExternal(source, externals, context, resolveSync) {

// else, vanilla object
for (const key in externals) {
if (hasOwn(externals, key) && source === key) {
if (Object.hasOwn(externals, key) && source === key) {
return true;
}
}
Expand All @@ -156,7 +152,7 @@ const webpack2DefaultResolveConfig = {
function createWebpack2ResolveSync(webpackRequire, resolveConfig) {
const EnhancedResolve = webpackRequire('enhanced-resolve');

return EnhancedResolve.create.sync(assign({}, webpack2DefaultResolveConfig, resolveConfig));
return EnhancedResolve.create.sync(Object.assign({}, webpack2DefaultResolveConfig, resolveConfig));
}

/**
Expand All @@ -178,7 +174,7 @@ function makeRootPlugin(ModulesInRootPlugin, name, root) {
if (typeof root === 'string') {
return new ModulesInRootPlugin(name, root);
}
if (isArray(root)) {
if (Array.isArray(root)) {
return function () {
root.forEach(function (root) {
this.apply(new ModulesInRootPlugin(name, root));
Expand Down Expand Up @@ -238,7 +234,7 @@ function createWebpack1ResolveSync(webpackRequire, resolveConfig, plugins) {
if (
plugin.constructor
&& plugin.constructor.name === 'ResolverPlugin'
&& isArray(plugin.plugins)
&& Array.isArray(plugin.plugins)
) {
resolvePlugins.push.apply(resolvePlugins, plugin.plugins);
}
Expand Down Expand Up @@ -401,7 +397,7 @@ exports.resolve = function (source, file, settings) {
webpackConfig = webpackConfig(env, argv);
}

if (isArray(webpackConfig)) {
if (Array.isArray(webpackConfig)) {
webpackConfig = webpackConfig.map((cfg) => {
if (typeof cfg === 'function') {
return cfg(env, argv);
Expand Down Expand Up @@ -448,7 +444,7 @@ exports.resolve = function (source, file, settings) {
try {
return { found: true, path: resolveSync(path.dirname(file), source) };
} catch (err) {
if (isCore(source)) {
if (isBuiltin(source)) {
return { found: true, path: null };
}

Expand Down
2 changes: 0 additions & 2 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
"debug": "^3.2.7",
"enhanced-resolve": "^0.9.1",
"find-root": "^1.1.0",
"hasown": "^2.0.0",
"interpret": "^1.4.0",
"is-core-module": "^2.13.1",
"is-regex": "^1.1.4",
"lodash": "^4.17.21",
"resolve": "^2.0.0-next.5",
Expand Down
4 changes: 2 additions & 2 deletions src/core/importType.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isBuiltin } from 'node:module';
import { isAbsolute as nodeIsAbsolute, relative, resolve as nodeResolve } from 'path';
import isCoreModule from 'is-core-module';

import resolve from 'eslint-module-utils/resolve';
import { getContextPackagePath } from './packagePath';
Expand Down Expand Up @@ -32,7 +32,7 @@ export function isBuiltIn(name, settings, path) {
if (path || !name) { return false; }
const base = baseModule(name);
const extras = settings && settings['import/core-modules'] || [];
return isCoreModule(base) || extras.indexOf(base) > -1;
return isBuiltin(base) || extras.indexOf(base) > -1;
}

const moduleRegExp = /^\w/;
Expand Down
5 changes: 2 additions & 3 deletions src/exportMap/visitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import includes from 'array-includes';
import { SourceCode } from 'eslint';
import { availableDocStyleParsers, captureDoc } from './doc';
import Namespace from './namespace';
Expand Down Expand Up @@ -121,11 +120,11 @@ export default class ImportExportVisitorBuilder {
'TSAbstractClassDeclaration',
'TSModuleDeclaration',
];
const exportedDecls = this.ast.body.filter(({ type, id, declarations }) => includes(declTypes, type) && (
const exportedDecls = this.ast.body.filter(({ type, id, declarations }) => declTypes.includes(type) && (
id && id.name === exportedName || declarations && declarations.find((d) => d.id.name === exportedName)
));
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
// Export is not referencing any local declaration, must be re-exported
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, astNode));
return;
}
Expand Down
5 changes: 2 additions & 3 deletions src/rules/export.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ExportMapBuilder from '../exportMap/builder';
import recursivePatternCapture from '../exportMap/patternCapture';
import docsUrl from '../docsUrl';
import includes from 'array-includes';

/*
Notes on TypeScript namespaces aka TSModuleDeclaration:
Expand Down Expand Up @@ -157,10 +156,10 @@ module.exports = {
const isTypeVariableDecl = node.declaration.kind === 'type';

if (node.declaration.id != null) {
if (includes([
if ([
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
], node.declaration.type)) {
].includes(node.declaration.type)) {
addNamed(node.declaration.id.name, node.declaration.id, parent, true);
} else {
addNamed(node.declaration.id.name, node.declaration.id, parent, isTypeVariableDecl);
Expand Down
4 changes: 1 addition & 3 deletions src/rules/exports-last.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import findLastIndex from 'array.prototype.findlastindex';

import docsUrl from '../docsUrl';

function isNonExportStatement({ type }) {
Expand All @@ -22,7 +20,7 @@ module.exports = {
create(context) {
return {
Program({ body }) {
const lastNonExportStatementIndex = findLastIndex(body, isNonExportStatement);
const lastNonExportStatementIndex = body.findLastIndex(isNonExportStatement);

if (lastNonExportStatementIndex !== -1) {
body.slice(0, lastNonExportStatementIndex).forEach((node) => {
Expand Down
12 changes: 6 additions & 6 deletions src/rules/group-exports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import docsUrl from '../docsUrl';
import values from 'object.values';
import flat from 'array.prototype.flat';

const meta = {
type: 'suggestion',
Expand Down Expand Up @@ -107,8 +105,9 @@ function create(context) {
}

// Report multiple `aggregated exports` from the same module (ES2015 modules)
flat(values(nodes.modules.sources)
.filter((nodesWithSource) => Array.isArray(nodesWithSource) && nodesWithSource.length > 1))
Object.values(nodes.modules.sources)
.filter((nodesWithSource) => Array.isArray(nodesWithSource) && nodesWithSource.length > 1)
.flat()
.forEach((node) => {
context.report({
node,
Expand All @@ -127,8 +126,9 @@ function create(context) {
}

// Report multiple `aggregated type exports` from the same module (FLOW ES2015 modules)
flat(values(nodes.types.sources)
.filter((nodesWithSource) => Array.isArray(nodesWithSource) && nodesWithSource.length > 1))
Object.values(nodes.types.sources)
.filter((nodesWithSource) => Array.isArray(nodesWithSource) && nodesWithSource.length > 1)
.flat()
.forEach((node) => {
context.report({
node,
Expand Down
8 changes: 2 additions & 6 deletions src/rules/no-anonymous-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* @author Duncan Beevers
*/

import hasOwn from 'hasown';
import values from 'object.values';
import fromEntries from 'object.fromentries';

import docsUrl from '../docsUrl';

const defs = {
Expand Down Expand Up @@ -60,12 +56,12 @@ const defs = {
},
};

const schemaProperties = fromEntries(values(defs).map((def) => [def.option, {
const schemaProperties = Object.fromEntries(Object.values(defs).map((def) => [def.option, {
description: def.description,
type: 'boolean',
}]));

const defaults = fromEntries(values(defs).map((def) => [def.option, hasOwn(def, 'default') ? def.default : false]));
const defaults = Object.fromEntries(Object.values(defs).map((def) => [def.option, Object.hasOwn(def, 'default') ? def.default : false]));

module.exports = {
meta: {
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';
import resolve from 'eslint-module-utils/resolve';
import semver from 'semver';
import flatMap from 'array.prototype.flatmap';

import docsUrl from '../docsUrl';

Expand Down Expand Up @@ -95,7 +94,7 @@ function getFix(first, rest, sourceCode, context) {
}

const defaultImportNames = new Set(
flatMap([].concat(first, rest || []), (x) => getDefaultImportName(x) || []),
[].concat(first, rest || []).flatMap((x) => getDefaultImportName(x) || []),
);

// Bail if there are multiple different default import names – it's up to the
Expand Down
12 changes: 4 additions & 8 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import resolve from 'eslint-module-utils/resolve';
import visit from 'eslint-module-utils/visit';
import { dirname, join, resolve as resolvePath } from 'path';
import readPkgUp from 'eslint-module-utils/readPkgUp';
import values from 'object.values';
import includes from 'array-includes';
import flatMap from 'array.prototype.flatmap';

import { walkSync } from '../core/fsWalk';
import ExportMapBuilder from '../exportMap/builder';
Expand Down Expand Up @@ -97,8 +94,7 @@ function listFilesWithLegacyFunctions(src, extensions) {
listFilesToProcess: originalListFilesToProcess,
} = require('eslint/lib/util/glob-util');
const patterns = src.concat(
flatMap(
src,
src.flatMap(
(pattern) => extensions.map((extension) => (/\*\*|\*\./).test(pattern) ? pattern : `${pattern}/**/*${extension}`),
),
);
Expand Down Expand Up @@ -310,7 +306,7 @@ function resolveFiles(src, ignoreExports, context) {
// prepare list of source files, don't consider files from node_modules
const resolvedFiles = srcFileList.length && typeof srcFileList[0] === 'string'
? srcFileList.filter((filePath) => !isNodeModule(filePath))
: flatMap(srcFileList, ({ filename }) => isNodeModule(filename) ? [] : filename);
: srcFileList.flatMap(({ filename }) => isNodeModule(filename) ? [] : filename);

return new Set(resolvedFiles);
}
Expand Down Expand Up @@ -494,9 +490,9 @@ const fileIsInPkg = (file) => {
};

const checkPkgFieldObject = (pkgField) => {
const pkgFieldFiles = flatMap(values(pkgField), (value) => typeof value === 'boolean' ? [] : join(basePath, value));
const pkgFieldFiles = Object.values(pkgField).flatMap((value) => typeof value === 'boolean' ? [] : join(basePath, value));

if (includes(pkgFieldFiles, file)) {
if (pkgFieldFiles.includes(file)) {
return true;
}
};
Expand Down
Loading

0 comments on commit 55983cf

Please sign in to comment.