Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ota-meshi/eslint-compat-utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.3
Choose a base ref
...
head repository: ota-meshi/eslint-compat-utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.4.0
Choose a head ref
  • 2 commits
  • 9 files changed
  • 3 contributors

Commits on Jan 16, 2024

  1. feat: improve getLinter/getRuleTester (#30)

    * feat: improve getLinter/getRuleTester
    
    * Create mighty-chairs-hunt.md
    ota-meshi authored Jan 16, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    zhukovgreen zhukovgreen
    Copy the full SHA
    60608cc View commit details
  2. chore: release eslint-compat-utils (#31)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Jan 16, 2024
    Copy the full SHA
    4d9183f View commit details
Showing with 104 additions and 27 deletions.
  1. +2 −0 .eslintrc.js
  2. +6 −0 CHANGELOG.md
  3. +1 −1 package.json
  4. +3 −1 src/eslint.ts
  5. +1 −19 src/lib/convert-config.ts
  6. +65 −0 src/lib/convert-option.ts
  7. +19 −0 src/lib/require.ts
  8. +5 −2 src/linter.ts
  9. +2 −4 src/rule-tester.ts
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@ module.exports = {
rules: {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/unbound-method": "off",
"func-style": "off",
},
overrides: [
{
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# eslint-compat-utils

## 0.4.0

### Minor Changes

- feat: improve getLinter/getRuleTester ([#30](https://github.com/ota-meshi/eslint-compat-utils/pull/30))

## 0.3.3

### Patch Changes
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-compat-utils",
"version": "0.3.3",
"version": "0.4.0",
"description": "Provides an API for ESLint custom rules that is compatible with the latest ESLint even when using older ESLint.",
"engines": {
"node": ">=12"
4 changes: 3 additions & 1 deletion src/eslint.ts
Original file line number Diff line number Diff line change
@@ -70,7 +70,9 @@ function getESLintClassFromLegacyESLint(
const { plugins, ...overrideConfig } = originalOverrideConfig;
// Remove unsupported options
delete overrideConfig.files;
delete overrideConfig.processor;
if (typeof overrideConfig.processor !== "string")
// Remove unsupported options
delete overrideConfig.processor;

newOptions.overrideConfig = convertConfigToRc(overrideConfig);

20 changes: 1 addition & 19 deletions src/lib/convert-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as eslint from "eslint";
import * as semver from "semver";
import type { LinterConfigForV8 } from "../v8-props";
import { createRequire } from "module";
import { safeRequire, safeRequireResolve } from "./require";

/** Convert to eslintrc config from v9 config */
export function convertConfigToRc(
@@ -76,24 +76,6 @@ function getParserName(parser: any) {
return safeRequireResolve(name);
}

/** Get module */
function safeRequire(name: string) {
try {
return createRequire(`${process.cwd()}/__placeholder__.js`)(name);
} catch {
return undefined;
}
}

/** Get module path */
function safeRequireResolve(name: string) {
try {
return createRequire(`${process.cwd()}/__placeholder__.js`).resolve(name);
} catch {
return name;
}
}

/** Get latest ecmaVersion */
function getLatestEcmaVersion() {
const eslintVersion = eslint.Linter.version;
65 changes: 65 additions & 0 deletions src/lib/convert-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type * as eslint from "eslint";

/** Convert to legacy verify option */
export function convertOptionToLegacy(
processor: string | eslint.Linter.Processor | undefined,
verifyOption: string | eslint.Linter.LintOptions | undefined,
config: eslint.Linter.FlatConfig,
): string | eslint.Linter.LintOptions | undefined {
if (processor == null) return verifyOption;
if (typeof processor === "string") {
return convertOptionToLegacy(
findProcessor(processor, config),
verifyOption,
config,
);
}

const filename =
(typeof verifyOption === "string"
? verifyOption
: verifyOption?.filename) ?? "<input>";

/** preprocess for linter */
const preprocess = function (code: string) {
const result = processor.preprocess?.(code, filename);
return result ? (result as string[]) : [code];
};
/** postprocess for linter */
const postprocess = function (messages: eslint.Linter.LintMessage[][]) {
const result = processor.postprocess?.(messages, filename);
return result ? result : messages[0];
};

if (verifyOption == null) {
return { preprocess, postprocess };
}
if (typeof verifyOption === "string") {
return { filename: verifyOption, preprocess, postprocess };
}
return { ...verifyOption, preprocess, postprocess };
}

/** Find processor by processor name */
function findProcessor(processor: string, config: eslint.Linter.FlatConfig) {
let pluginName: string, processorName: string;

const splitted = processor.split("/")[0];
if (splitted.length === 2) {
pluginName = splitted[0];
processorName = splitted[1];
} else if (splitted.length === 3 && splitted[0].startsWith("@")) {
pluginName = `${splitted[0]}/${splitted[1]}`;
processorName = splitted[2];
} else {
throw new Error(`Could not resolve processor: ${processor}`);
}

const plugin = config.plugins?.[pluginName];

const resolved = plugin?.processors?.[processorName];
if (!resolved) {
throw new Error(`Could not resolve processor: ${processor}`);
}
return resolved;
}
19 changes: 19 additions & 0 deletions src/lib/require.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createRequire } from "module";

/** Load module */
export function safeRequire(name: string): any {
try {
return createRequire(`${process.cwd()}/__placeholder__.js`)(name);
} catch {
return undefined;
}
}

/** Get module path or name */
export function safeRequireResolve(name: string): string {
try {
return createRequire(`${process.cwd()}/__placeholder__.js`).resolve(name);
} catch {
return name;
}
}
7 changes: 5 additions & 2 deletions src/linter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as eslint from "eslint";
import * as semver from "semver";
import { convertConfigToRc } from "./lib/convert-config";
import { convertOptionToLegacy } from "./lib/convert-option";

let cacheLinter: typeof eslint.Linter | undefined;

@@ -31,8 +32,10 @@ function getLinterClassFromLegacyLinter(): typeof eslint.Linter {
config: any,
option: any,
): eslint.Linter.LintMessage[] {
const newConfig = convertConfigToRc(config, this);
return super.verify(code, newConfig as any, option);
const { processor, ...otherConfig } = config || {};
const newConfig = convertConfigToRc(otherConfig, this);
const newOption = convertOptionToLegacy(processor, option, config || {});
return super.verify(code, newConfig as any, newOption as any);
}
};
}
6 changes: 2 additions & 4 deletions src/rule-tester.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import * as eslint from "eslint";
import * as semver from "semver";
import { convertConfigToRc } from "./lib/convert-config";
import { getUnsupported } from "./lib/get-unsupported";
import { convertOptionToLegacy } from "./lib/convert-option";

let cacheRuleTester: typeof eslint.RuleTester | undefined;
let cachePrefix = "";
@@ -110,10 +111,7 @@ function getRuleTesterClassFromLegacyRuleTester() {
}
return {
...converted,
filename: {
...(config.filename != null ? { filename: config.filename } : {}),
...processor,
},
filename: convertOptionToLegacy(processor, config.filename, config),
};
}
}