Skip to content

Commit

Permalink
feat: update eslint-compat-utils (#285)
Browse files Browse the repository at this point in the history
* feat: update eslint-compat-utils

* Create breezy-flies-beg.md

* Update breezy-flies-beg.md

* add test

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
ota-meshi committed Jan 15, 2024
1 parent 48c4fea commit 49b13e4
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-flies-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-json-schema-validator": minor
---

feat: update eslint-compat-utils
11 changes: 11 additions & 0 deletions .github/workflows/NodeCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ jobs:
run: yarn install --ignore-engines
- name: Test
run: yarn test
test-with-eslint9:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Install Target Packages
run: |+
yarn add -D eslint@^9.0.0-0 --ignore-engines
yarn --ignore-engines
- name: Test
run: yarn test
test-with-eslint6:
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@eslint-community/eslint-utils": "^4.3.0",
"ajv": "^8.0.0",
"debug": "^4.3.1",
"eslint-compat-utils": "^0.1.2",
"eslint-compat-utils": "^0.2.0",
"json-schema-migrate": "^2.0.0",
"jsonc-eslint-parser": "^2.0.0",
"minimatch": "^8.0.0",
Expand All @@ -68,7 +68,7 @@
"@changesets/cli": "^2.24.2",
"@ota-meshi/eslint-plugin": "^0.15.0",
"@types/debug": "^4.1.5",
"@types/eslint": "^8.0.0",
"@types/eslint": "^8.56.2",
"@types/eslint-scope": "^3.7.0",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/estree": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/configs/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export = {
export default {
plugins: ["json-schema-validator"],
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const baseExtend =
path.extname(`${base}`) === ".ts"
? "plugin:json-schema-validator/base"
: base;
export = {
export default {
extends: [baseExtend],
rules: {
// eslint-plugin-json-schema-validator rules
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const rules = ruleList.reduce(
{} as { [key: string]: RuleModule },
);

// @ts-expect-error -- ignore

Check failure on line 20 in src/index.ts

View workflow job for this annotation

GitHub Actions / Release

Unused '@ts-expect-error' directive.

Check failure on line 20 in src/index.ts

View workflow job for this annotation

GitHub Actions / deploy

Unused '@ts-expect-error' directive.
export = {
meta,
configs,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/http-client/sync-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function syncGet(
options?: RequestOptions,
httpModulePath?: string,
): string {
return getSync(url, options, httpModulePath);
return getSync(url, options, httpModulePath) as string;
}
34 changes: 20 additions & 14 deletions tests/src/eslint-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from "path";
import assert from "assert";
import plugin from "../../src/index";
import { getESLint } from "eslint-compat-utils";
import { Linter } from "eslint";
import semver from "semver";
import { getESLint } from "eslint-compat-utils/eslint";
// eslint-disable-next-line @typescript-eslint/naming-convention -- Class name
const ESLint = getESLint();

Expand All @@ -11,18 +13,22 @@ const ESLint = getESLint();

const TEST_CWD = path.join(__dirname, "../fixtures/integrations/eslint-plugin");

describe("Integration with eslint-plugin-json-schema-validator", () => {
it("should lint without errors", async () => {
const engine = new ESLint({
cwd: TEST_CWD,
extensions: [".js", ".json"],
plugins: { "eslint-plugin-json-schema-validator": plugin as any },
if (semver.lt(Linter.version, "9.0.0-0"))
describe("Integration with eslint-plugin-json-schema-validator", () => {
it("should lint without errors", async () => {
const engine = new ESLint({
cwd: TEST_CWD,
extensions: [".js", ".json"],
overrideConfig: {
files: ["**/*.js", "**/*.json", "*.js", "*.json"],
plugins: { "json-schema-validator": plugin },
} as any,
});
const results = await engine.lintFiles(["test01/src"]);
assert.strictEqual(results.length, 2);
assert.strictEqual(
results.reduce((s, r) => s + r.errorCount, 0),
0,
);
});
const results = await engine.lintFiles(["test01/src"]);
assert.strictEqual(results.length, 2);
assert.strictEqual(
results.reduce((s, r) => s + r.errorCount, 0),
0,
);
});
});
25 changes: 18 additions & 7 deletions tests/src/rules/no-invalid.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from "path";
import { RuleTester } from "eslint";
import { getRuleTester } from "eslint-compat-utils/rule-tester";
import rule from "../../../src/rules/no-invalid";
import { loadTestCases } from "../../utils/utils";

// eslint-disable-next-line @typescript-eslint/naming-convention -- class name
const RuleTester = getRuleTester();
const tester = new RuleTester({
parser: require.resolve("jsonc-eslint-parser"),
parserOptions: {
languageOptions: {
/* eslint @typescript-eslint/no-require-imports: 0 -- ignore */
parser: require("jsonc-eslint-parser"),
ecmaVersion: 2020,
sourceType: "module",
},
Expand All @@ -22,7 +24,10 @@ tester.run(
{
filename: path.join(__dirname, ".eslintrc.js"),
code: 'module.exports = { "extends": [ require.resolve("eslint-config-foo") ] }',
parser: require.resolve("espree"),
// @ts-expect-error -- typeerror
languageOptions: {
parser: require("espree"),
},
options: [
{
schemas: [
Expand Down Expand Up @@ -59,7 +64,10 @@ tester.run(
{
filename: path.join(__dirname, ".eslintrc.js"),
code: 'module.exports = { "extends": [ 42 ] }',
parser: require.resolve("espree"),
// @ts-expect-error -- typeerror
languageOptions: {
parser: require("espree"),
},
options: [
{
schemas: [
Expand Down Expand Up @@ -270,7 +278,10 @@ trailingComma = "es3"
tabWidth = 4
semi = false
singleQuote = true`,
parser: require.resolve("toml-eslint-parser"),
// @ts-expect-error -- typeerror
languageOptions: {
parser: require("toml-eslint-parser"),
},
options: [
{
schemas: [
Expand Down
56 changes: 35 additions & 21 deletions tests/src/utils/ast/js.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import assert from "assert";
import path from "path";
import fs from "fs";
import { Linter } from "eslint";
import { getLinter } from "eslint-compat-utils/linter";
import type { AnalyzedJsAST, PathData } from "../../../../src/utils/ast/js";
import { analyzeJsAST } from "../../../../src/utils/ast/js";
import type { ESLintExportDefaultDeclaration } from "vue-eslint-parser/ast";
import type { SourceCode } from "../../../../src/types";
import { getSourceCode } from "../../../../src/utils/compat";
// eslint-disable-next-line @typescript-eslint/naming-convention -- class name
const Linter = getLinter();

const FIXTURES_ROOT = path.join(__dirname, "../../../fixtures/utils/ast/js");

Expand All @@ -18,28 +20,40 @@ describe("AST for JS.", () => {

const linter = new Linter();
let result: any;
linter.defineRule("test", {
// @ts-expect-error -- ignore
create(context) {
return {
ExportDefaultDeclaration(node: ESLintExportDefaultDeclaration) {
result = toOutput(
analyzeJsAST(
node.declaration as never,
node.declaration.range,
context as never,
)!,
getSourceCode(context) as never,
);
const err = linter.verify(input, {
plugins: {
test: {
rules: {
test: {
// @ts-expect-error -- ignore
create(context) {
return {
ExportDefaultDeclaration(
node: ESLintExportDefaultDeclaration,
) {
result = toOutput(
analyzeJsAST(
node.declaration as never,
node.declaration.range,
context as never,
)!,
getSourceCode(context) as never,
);
},
};
},
},
},
};
},
},
});
const err = linter.verify(input, {
rules: { test: "error" },
parser: "espree",
parserOptions: { ecmaVersion: 2020, sourceType: "module" },
});
rules: { "test/test": "error" },
languageOptions: {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- test
parser: require("espree"),
ecmaVersion: 2020,
sourceType: "module",
},
} as any);
if (err.length > 0) {
throw new Error(err[0].message);
}
Expand Down
57 changes: 26 additions & 31 deletions tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import fs from "fs";
import path from "path";
import type { RuleTester } from "eslint";
import { Linter } from "eslint";
import { getLinter as getCompatLinter } from "eslint-compat-utils/linter";
import * as jsoncESLintParser from "jsonc-eslint-parser";
import * as yamlESLintParser from "yaml-eslint-parser";
import * as tomlESLintParser from "toml-eslint-parser";
import * as vueESLintParser from "vue-eslint-parser";
import semver from "semver";
// eslint-disable-next-line @typescript-eslint/no-require-imports -- tests
import plugin = require("../../src/index");
import plugin from "../../src/index";
// eslint-disable-next-line @typescript-eslint/naming-convention -- class name
const Linter = getCompatLinter();

/**
* Prevents leading spaces in a multiline template literal from appearing in the resulting string
Expand Down Expand Up @@ -180,7 +181,7 @@ function writeFixtures(
inputFile: string,
{ force }: { force?: boolean } = {},
) {
const linter = getLinter(ruleName);
const linter = new Linter();
const errorFile = inputFile.replace(
/input\.(?:js|json5?|ya?ml|toml|vue)$/u,
"errors.json",
Expand All @@ -191,15 +192,18 @@ function writeFixtures(
const result = linter.verify(
config.code,
{
plugins: {
"my-eslint-plugin": plugin,
},
rules: {
[ruleName]: ["error", ...(config.options || [])],
[`my-eslint-plugin/${ruleName}`]: ["error", ...(config.options || [])],
},
parser: getParserName(inputFile),
parserOptions: {
languageOptions: {
parser: getParser(inputFile),
ecmaVersion: 2020,
sourceType: "module",
},
},
} as any,
config.filename,
);
if (force || !fs.existsSync(errorFile)) {
Expand All @@ -215,25 +219,12 @@ function writeFixtures(
})),
null,
4,
)}\n`,
)}\n`.replace(/my-eslint-plugin\//gu, ""),
"utf8",
);
}
}

function getLinter(ruleName: string) {
const linter = new Linter();
// @ts-expect-error for test
linter.defineParser("jsonc-eslint-parser", jsoncESLintParser);
linter.defineParser("yaml-eslint-parser", yamlESLintParser as any);
linter.defineParser("toml-eslint-parser", tomlESLintParser as any);
linter.defineParser("vue-eslint-parser", vueESLintParser as any);
// @ts-expect-error for test
linter.defineRule(ruleName, plugin.rules[ruleName]);

return linter;
}

// eslint-disable-next-line complexity -- ignore
function getConfig(ruleName: string, inputFile: string) {
const filename = inputFile.slice(inputFile.indexOf(ruleName));
Expand Down Expand Up @@ -264,7 +255,7 @@ function getConfig(ruleName: string, inputFile: string) {
? `/* ${filename} */\n${code0}`
: `<!--${filename}-->\n${code0}`;
return Object.assign(
{ parser: require.resolve(getParserName(inputFile)) },
{ languageOptions: { parser: getParser(inputFile) } },
config,
{ code, filename: inputFile },
);
Expand Down Expand Up @@ -292,24 +283,28 @@ function getConfig(ruleName: string, inputFile: string) {
}

return Object.assign(
{ parser: require.resolve(getParserName(inputFile)) },
{ languageOptions: { parser: getParser(inputFile) } },
config,
{ code, filename: inputFile },
{
code,
filename: inputFile,
},
);
}

function getParserName(fileName: string): string {
function getParser(fileName: string) {
if (fileName.endsWith(".vue")) {
return "vue-eslint-parser";
return vueESLintParser;
}
if (fileName.endsWith(".yaml") || fileName.endsWith(".yml")) {
return "yaml-eslint-parser";
return yamlESLintParser;
}
if (fileName.endsWith(".toml")) {
return "toml-eslint-parser";
return tomlESLintParser;
}
if (fileName.endsWith(".js")) {
return "espree";
// eslint-disable-next-line @typescript-eslint/no-require-imports -- test
return require("espree");
}
return "jsonc-eslint-parser";
return jsoncESLintParser;
}
2 changes: 1 addition & 1 deletion tools/render-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function renderRulesTableContent(
const link = `[${rule.meta.docs.ruleId}](${buildRulePath(
rule.meta.docs.ruleName || "",
)})`;
const replacedRules = rule.meta.docs.replacedBy || [];
const replacedRules = rule.meta.replacedBy || [];
const replacedBy = replacedRules
.map(
(name) => `[json-schema-validator/${name}](${buildRulePath(name)}.md)`,
Expand Down
2 changes: 1 addition & 1 deletion tools/update-rulesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import path from "path"
const base = require.resolve("./base")
const baseExtend =
path.extname(\`\${base}\`) === ".ts" ? "plugin:json-schema-validator/base" : base
export = {
export default {
extends: [baseExtend],
rules: {
// eslint-plugin-json-schema-validator rules
Expand Down

0 comments on commit 49b13e4

Please sign in to comment.