Upgrade to ESLint 9
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
#!/usr/bin/env tsx
|
||||
import assert from 'node:assert';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve, extname } from 'node:path';
|
||||
import type { Loader, Plugin } from 'esbuild';
|
||||
import { extname, resolve } from 'node:path';
|
||||
|
||||
import * as babel from '@babel/core';
|
||||
import type { types as t, types } from '@babel/core';
|
||||
import { createMacro, type MacroHandler } from 'babel-plugin-macros';
|
||||
import babelMacros, { type MacroHandler } from 'babel-plugin-macros';
|
||||
import type { Loader, Plugin } from 'esbuild';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import * as polyfill from '../src/polyfill';
|
||||
|
||||
const polyfills = Object.keys(polyfill);
|
||||
@ -15,7 +18,7 @@ class HandlerMap {
|
||||
|
||||
set(names: string | string[], handler: MacroHandler) {
|
||||
names = Array.isArray(names) ? names : [names];
|
||||
const macro = createMacro(handler);
|
||||
const macro = babelMacros.createMacro(handler);
|
||||
for (const name of names) {
|
||||
this.map.set(name, macro);
|
||||
}
|
||||
@ -23,7 +26,7 @@ class HandlerMap {
|
||||
}
|
||||
|
||||
get keys() {
|
||||
return Array.from(this.map.keys());
|
||||
return [...this.map.keys()];
|
||||
}
|
||||
|
||||
resolvePath = (module: string) => module;
|
||||
@ -96,14 +99,14 @@ const map = new HandlerMap()
|
||||
'object.groupby',
|
||||
replace(t =>
|
||||
t.memberExpression(
|
||||
t.callExpression(t.identifier('require'), [t.stringLiteral('lodash')]),
|
||||
t.callExpression(t.identifier('require'), [t.stringLiteral('lodash-es')]),
|
||||
t.identifier('groupBy'),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// es-iterator-helpers/Iterator.prototype.*
|
||||
const polyfillPath = resolve(__dirname, '../src/polyfill.ts');
|
||||
const polyfillPath = resolve(import.meta.dirname, '../src/polyfill.ts');
|
||||
const requirePolyfill = (t: typeof types, name: string) =>
|
||||
t.memberExpression(
|
||||
t.callExpression(t.identifier('require'), [t.stringLiteral(polyfillPath)]),
|
||||
@ -127,15 +130,15 @@ map.set(
|
||||
|
||||
function replace(getReplacement: (types: typeof t) => t.Expression): MacroHandler {
|
||||
return ({ references, babel: { types: t } }) => {
|
||||
references.default.forEach(referencePath => {
|
||||
for (const referencePath of references.default) {
|
||||
referencePath.replaceWith(getReplacement(t));
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function proto(getProperty: (types: typeof t) => t.Expression): MacroHandler {
|
||||
return ({ references, babel: { types: t } }) => {
|
||||
references.default.forEach(referencePath => {
|
||||
for (const referencePath of references.default) {
|
||||
const { parent, parentPath } = referencePath;
|
||||
assert(t.isCallExpression(parent));
|
||||
const [callee, ...rest] = parent.arguments;
|
||||
@ -145,7 +148,7 @@ function proto(getProperty: (types: typeof t) => t.Expression): MacroHandler {
|
||||
rest,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -160,21 +163,14 @@ export const babelPlugin: Plugin = {
|
||||
return null;
|
||||
}
|
||||
|
||||
let source = readFileSync(path, 'utf-8')
|
||||
const source = readFileSync(path, 'utf8')
|
||||
.replaceAll("require('object.hasown/polyfill')()", 'Object.hasOwn')
|
||||
.replaceAll("require('object.fromentries/polyfill')()", 'Object.fromEntries')
|
||||
.replaceAll(
|
||||
"Object.keys(require('prop-types'))",
|
||||
JSON.stringify(Object.keys(require('prop-types'))),
|
||||
JSON.stringify(Object.keys(PropTypes)),
|
||||
);
|
||||
|
||||
if (
|
||||
path.includes('packages/eslint-plugin-import/src/rules/') ||
|
||||
path.includes('packages/eslint-plugin-import/config/')
|
||||
) {
|
||||
source = source.replace('\nmodule.exports = {', '\nexport default {');
|
||||
}
|
||||
|
||||
const isFlow = source.includes('@flow');
|
||||
const loader = extname(path).slice(1) as Loader;
|
||||
|
||||
|
Reference in New Issue
Block a user