From 8d503ddd845d370de446d7d2dc1e2c95d22a5ce1 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 8 Oct 2023 23:00:29 +0900 Subject: [PATCH] feat: use eslint-compat-utils (#265) --- .changeset/proud-cameras-pull.md | 5 + .eslintrc.js | 51 ++++++++++ lib/rules/auto.ts | 6 +- lib/rules/key-name-casing.ts | 8 +- lib/rules/no-bigint-literals.ts | 4 +- lib/rules/no-binary-expression.ts | 4 +- lib/rules/no-binary-numeric-literals.ts | 4 +- lib/rules/no-comments.ts | 8 +- lib/rules/no-escape-sequence-in-identifier.ts | 5 +- lib/rules/no-hexadecimal-numeric-literals.ts | 4 +- lib/rules/no-infinity.ts | 4 +- lib/rules/no-nan.ts | 4 +- lib/rules/no-number-props.ts | 4 +- lib/rules/no-numeric-separators.ts | 8 +- lib/rules/no-octal-numeric-literals.ts | 4 +- lib/rules/no-parenthesized.ts | 5 +- lib/rules/no-plus-sign.ts | 5 +- lib/rules/no-regexp-literals.ts | 4 +- lib/rules/no-template-literals.ts | 4 +- lib/rules/no-undefined-value.ts | 4 +- lib/rules/no-unicode-codepoint-escapes.ts | 5 +- lib/rules/sort-array-values.ts | 6 +- lib/rules/sort-keys.ts | 6 +- lib/rules/valid-json-number.ts | 5 +- .../vue-custom-block/no-parsing-error.ts | 11 ++- lib/utils/index.ts | 11 ++- package.json | 3 +- tests/lib/as-parser.ts | 3 +- tests/lib/auto-rule.ts | 3 +- tests/lib/eslint-plugin.ts | 3 +- tools/lib/eslint-compat.ts | 97 ------------------- tools/new-rule.ts | 8 +- 32 files changed, 153 insertions(+), 153 deletions(-) create mode 100644 .changeset/proud-cameras-pull.md delete mode 100644 tools/lib/eslint-compat.ts diff --git a/.changeset/proud-cameras-pull.md b/.changeset/proud-cameras-pull.md new file mode 100644 index 00000000..223814ac --- /dev/null +++ b/.changeset/proud-cameras-pull.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-jsonc": minor +--- + +feat: use eslint-compat-utils diff --git a/.eslintrc.js b/.eslintrc.js index c798c835..6daa966e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -38,6 +38,57 @@ module.exports = { ], }, ], + "no-restricted-properties": [ + "error", + { + object: "context", + property: "getSourceCode", + message: + "Please use `eslint-compat-utils` module's `getSourceCode(context).getScope()` instead.", + }, + { + object: "context", + property: "sourceCode", + message: + "Please use `eslint-compat-utils` module's `getSourceCode(context).getScope()` instead.", + }, + { + object: "context", + property: "getFilename", + message: + "Please use `eslint-compat-utils` module's `getFilename(context)` instead.", + }, + { + object: "context", + property: "filename", + message: + "Please use `eslint-compat-utils` module's `getFilename(context)` instead.", + }, + { + object: "context", + property: "getCwd", + message: + "Please use `eslint-compat-utils` module's `getCwd(context)` instead.", + }, + { + object: "context", + property: "cwd", + message: + "Please use `eslint-compat-utils` module's `getCwd(context)` instead.", + }, + { + object: "context", + property: "getScope", + message: + "Please use `eslint-compat-utils` module's `getSourceCode(context).getScope()` instead.", + }, + { + object: "context", + property: "parserServices", + message: + "Please use `eslint-compat-utils` module's `getSourceCode(context).parserServices` instead.", + }, + ], }, overrides: [ { diff --git a/lib/rules/auto.ts b/lib/rules/auto.ts index 0abbd2b4..66a327f5 100644 --- a/lib/rules/auto.ts +++ b/lib/rules/auto.ts @@ -1,3 +1,4 @@ +import { getFilename, getSourceCode } from "eslint-compat-utils"; import type { RuleListener, RuleModule } from "../types"; import { createRule } from "../utils"; import { getAutoConfig } from "../utils/get-auto-jsonc-rules-config"; @@ -17,10 +18,11 @@ export default createRule("auto", { type: "suggestion", }, create(context, params) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } - const autoConfig = getAutoConfig(context.getFilename()); + const autoConfig = getAutoConfig(getFilename(context)); const visitor: RuleListener = {}; for (const ruleId of Object.keys(autoConfig)) { diff --git a/lib/rules/key-name-casing.ts b/lib/rules/key-name-casing.ts index 8f28787d..15bcf4a3 100644 --- a/lib/rules/key-name-casing.ts +++ b/lib/rules/key-name-casing.ts @@ -1,8 +1,8 @@ import type { AST } from "jsonc-eslint-parser"; -import type { RuleListener } from "../types"; import { createRule } from "../utils"; import type { CasingKind } from "../utils/casing"; import { getChecker, allowedCaseOptions } from "../utils/casing"; +import { getSourceCode } from "eslint-compat-utils"; type Option = { [key in CasingKind]?: boolean; @@ -61,10 +61,10 @@ export default createRule("key-name-casing", { type: "suggestion", }, create(context) { - if (!context.parserServices.isJSON) { - return {} as RuleListener; + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { + return {}; } - const sourceCode = context.getSourceCode(); const option: Option = { ...context.options[0] }; if (option.camelCase !== false) { option.camelCase = true; diff --git a/lib/rules/no-bigint-literals.ts b/lib/rules/no-bigint-literals.ts index 89c40069..6ec44a4a 100644 --- a/lib/rules/no-bigint-literals.ts +++ b/lib/rules/no-bigint-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-bigint-literals", { meta: { @@ -16,7 +17,8 @@ export default createRule("no-bigint-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-binary-expression.ts b/lib/rules/no-binary-expression.ts index 26dfbe0c..8735cc44 100644 --- a/lib/rules/no-binary-expression.ts +++ b/lib/rules/no-binary-expression.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { getStaticJSONValue } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-binary-expression", { meta: { @@ -19,7 +20,8 @@ export default createRule("no-binary-expression", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } diff --git a/lib/rules/no-binary-numeric-literals.ts b/lib/rules/no-binary-numeric-literals.ts index 51915ec4..2253dae0 100644 --- a/lib/rules/no-binary-numeric-literals.ts +++ b/lib/rules/no-binary-numeric-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; const binaryNumericLiteralPattern = /^0[Bb]/u; @@ -19,7 +20,8 @@ export default createRule("no-binary-numeric-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-comments.ts b/lib/rules/no-comments.ts index bc82eaba..ec121121 100644 --- a/lib/rules/no-comments.ts +++ b/lib/rules/no-comments.ts @@ -1,4 +1,4 @@ -import type { RuleListener } from "../types"; +import { getSourceCode } from "eslint-compat-utils"; import { createRule } from "../utils"; export default createRule("no-comments", { @@ -16,10 +16,10 @@ export default createRule("no-comments", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { - return {} as RuleListener; + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { + return {}; } - const sourceCode = context.getSourceCode(); return { Program() { for (const comment of sourceCode.getAllComments()) { diff --git a/lib/rules/no-escape-sequence-in-identifier.ts b/lib/rules/no-escape-sequence-in-identifier.ts index f693d86d..f350fc6b 100644 --- a/lib/rules/no-escape-sequence-in-identifier.ts +++ b/lib/rules/no-escape-sequence-in-identifier.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; import { PatternMatcher } from "@eslint-community/eslint-utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-escape-sequence-in-identifier", { meta: { @@ -18,10 +19,10 @@ export default createRule("no-escape-sequence-in-identifier", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } - const sourceCode = context.getSourceCode(); return { JSONIdentifier(node: AST.JSONIdentifier) { verify(node); diff --git a/lib/rules/no-hexadecimal-numeric-literals.ts b/lib/rules/no-hexadecimal-numeric-literals.ts index 6de13eb6..bbe62f7a 100644 --- a/lib/rules/no-hexadecimal-numeric-literals.ts +++ b/lib/rules/no-hexadecimal-numeric-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; const hexadecimalNumericLiteralPattern = /^0[Xx]/u; @@ -19,7 +20,8 @@ export default createRule("no-hexadecimal-numeric-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-infinity.ts b/lib/rules/no-infinity.ts index 1f1d5152..4b76c6d2 100644 --- a/lib/rules/no-infinity.ts +++ b/lib/rules/no-infinity.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { isNumberIdentifier } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-infinity", { meta: { @@ -17,7 +18,8 @@ export default createRule("no-infinity", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-nan.ts b/lib/rules/no-nan.ts index c933b483..352d51bd 100644 --- a/lib/rules/no-nan.ts +++ b/lib/rules/no-nan.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { isNumberIdentifier } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-nan", { meta: { @@ -17,7 +18,8 @@ export default createRule("no-nan", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-number-props.ts b/lib/rules/no-number-props.ts index 9151c559..a599e6bc 100644 --- a/lib/rules/no-number-props.ts +++ b/lib/rules/no-number-props.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-number-props", { meta: { @@ -17,7 +18,8 @@ export default createRule("no-number-props", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-numeric-separators.ts b/lib/rules/no-numeric-separators.ts index 43532535..1f8c2ab2 100644 --- a/lib/rules/no-numeric-separators.ts +++ b/lib/rules/no-numeric-separators.ts @@ -1,6 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; -import type { RuleListener } from "../types"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-numeric-separators", { meta: { @@ -18,10 +18,10 @@ export default createRule("no-numeric-separators", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { - return {} as RuleListener; + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { + return {}; } - const sourceCode = context.getSourceCode(); return { JSONLiteral(node: AST.JSONLiteral) { if (typeof node.value !== "number") { diff --git a/lib/rules/no-octal-numeric-literals.ts b/lib/rules/no-octal-numeric-literals.ts index 9d2145e6..55342d4e 100644 --- a/lib/rules/no-octal-numeric-literals.ts +++ b/lib/rules/no-octal-numeric-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; const octalNumericLiteralPattern = /^0[Oo]/u; @@ -19,7 +20,8 @@ export default createRule("no-octal-numeric-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-parenthesized.ts b/lib/rules/no-parenthesized.ts index c1ff8c5b..6d0f4b45 100644 --- a/lib/rules/no-parenthesized.ts +++ b/lib/rules/no-parenthesized.ts @@ -3,6 +3,7 @@ import { isParenthesized } from "@eslint-community/eslint-utils"; import type { AST } from "jsonc-eslint-parser"; import { isExpression } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-parenthesized", { meta: { @@ -21,10 +22,10 @@ export default createRule("no-parenthesized", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } - const sourceCode = context.getSourceCode(); type ExpressionHandler = { [key in AST.JSONExpression["type"]]: ( diff --git a/lib/rules/no-plus-sign.ts b/lib/rules/no-plus-sign.ts index 8c68589c..5bccc23b 100644 --- a/lib/rules/no-plus-sign.ts +++ b/lib/rules/no-plus-sign.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-plus-sign", { meta: { @@ -17,10 +18,10 @@ export default createRule("no-plus-sign", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } - const sourceCode = context.getSourceCode(); return { JSONUnaryExpression(node: AST.JSONUnaryExpression) { if (node.operator === "+") { diff --git a/lib/rules/no-regexp-literals.ts b/lib/rules/no-regexp-literals.ts index 01b6aaaa..06db9f5b 100644 --- a/lib/rules/no-regexp-literals.ts +++ b/lib/rules/no-regexp-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-regexp-literals", { meta: { @@ -16,7 +17,8 @@ export default createRule("no-regexp-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-template-literals.ts b/lib/rules/no-template-literals.ts index 7e3687c3..d03f8dca 100644 --- a/lib/rules/no-template-literals.ts +++ b/lib/rules/no-template-literals.ts @@ -1,5 +1,6 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-template-literals", { meta: { @@ -17,7 +18,8 @@ export default createRule("no-template-literals", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-undefined-value.ts b/lib/rules/no-undefined-value.ts index 59cbdeeb..15a5c043 100644 --- a/lib/rules/no-undefined-value.ts +++ b/lib/rules/no-undefined-value.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { isUndefinedIdentifier } from "jsonc-eslint-parser"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-undefined-value", { meta: { @@ -17,7 +18,8 @@ export default createRule("no-undefined-value", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } return { diff --git a/lib/rules/no-unicode-codepoint-escapes.ts b/lib/rules/no-unicode-codepoint-escapes.ts index 6075af0d..a09018aa 100644 --- a/lib/rules/no-unicode-codepoint-escapes.ts +++ b/lib/rules/no-unicode-codepoint-escapes.ts @@ -1,6 +1,7 @@ import type { AST } from "jsonc-eslint-parser"; import { createRule } from "../utils"; import { PatternMatcher } from "@eslint-community/eslint-utils"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("no-unicode-codepoint-escapes", { meta: { @@ -18,10 +19,10 @@ export default createRule("no-unicode-codepoint-escapes", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } - const sourceCode = context.getSourceCode(); return { JSONIdentifier(node: AST.JSONIdentifier) { verify(node); diff --git a/lib/rules/sort-array-values.ts b/lib/rules/sort-array-values.ts index 0a514de8..42c369b5 100644 --- a/lib/rules/sort-array-values.ts +++ b/lib/rules/sort-array-values.ts @@ -4,6 +4,7 @@ import { isCommaToken } from "@eslint-community/eslint-utils"; import type { AST } from "jsonc-eslint-parser"; import { getStaticJSONValue } from "jsonc-eslint-parser"; import type { SourceCode, AST as ESLintAST } from "eslint"; +import { getSourceCode } from "eslint-compat-utils"; type JSONValue = ReturnType; @@ -393,14 +394,13 @@ export default createRule("sort-array-values", { type: "suggestion", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } // Parse options. const parsedOptions = parseOptions(context.options); - const sourceCode = context.getSourceCode(); - /** * Verify for array element */ diff --git a/lib/rules/sort-keys.ts b/lib/rules/sort-keys.ts index 1df84890..81d5908f 100644 --- a/lib/rules/sort-keys.ts +++ b/lib/rules/sort-keys.ts @@ -3,6 +3,7 @@ import { createRule } from "../utils"; import { isCommaToken } from "@eslint-community/eslint-utils"; import type { AST } from "jsonc-eslint-parser"; import { getStaticJSONValue } from "jsonc-eslint-parser"; +import { getSourceCode } from "eslint-compat-utils"; //------------------------------------------------------------------------------ // Helpers @@ -408,14 +409,13 @@ export default createRule("sort-keys", { type: "suggestion", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } // Parse options. const parsedOptions = parseOptions(context.options); - const sourceCode = context.getSourceCode(); - /** * Verify for property */ diff --git a/lib/rules/valid-json-number.ts b/lib/rules/valid-json-number.ts index 4a4cfc8e..53e6df74 100644 --- a/lib/rules/valid-json-number.ts +++ b/lib/rules/valid-json-number.ts @@ -3,6 +3,7 @@ import type { AST } from "jsonc-eslint-parser"; import { isNumberIdentifier } from "jsonc-eslint-parser"; import type { RuleListener } from "../types"; import { createRule } from "../utils"; +import { getSourceCode } from "eslint-compat-utils"; const nonDecimalNumericLiteralPattern = /^0[\dBOXbox]/u; @@ -44,10 +45,10 @@ export default createRule("valid-json-number", { type: "problem", }, create(context) { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {} as RuleListener; } - const sourceCode = context.getSourceCode(); return { JSONUnaryExpression(node: AST.JSONUnaryExpression) { if (node.argument.type === "JSONIdentifier") { diff --git a/lib/rules/vue-custom-block/no-parsing-error.ts b/lib/rules/vue-custom-block/no-parsing-error.ts index ebb31ff0..5bb4ddfe 100644 --- a/lib/rules/vue-custom-block/no-parsing-error.ts +++ b/lib/rules/vue-custom-block/no-parsing-error.ts @@ -4,6 +4,7 @@ import { createRule } from "../../utils"; import type { RuleListener } from "../../types"; import * as jsoncESLintParser from "jsonc-eslint-parser"; import type { Rule } from "eslint"; +import { getSourceCode } from "eslint-compat-utils"; export default createRule("vue-custom-block/no-parsing-error", { meta: { @@ -21,15 +22,17 @@ export default createRule("vue-custom-block/no-parsing-error", { if (!customBlock) { return {}; } - const parseError = context.parserServices.parseError; + const sourceCode = getSourceCode(context); + // eslint-disable-next-line no-restricted-properties -- Workaround for bug in vue-eslint-parser v9.3.1 + const parserServices = context.parserServices ?? sourceCode.parserServices; + const parseError = parserServices.parseError; if (parseError) { return errorReportVisitor(context, parseError); } const parseCustomBlockElement: | ((parser: any, options: any) => any) - | undefined = context.parserServices.parseCustomBlockElement; - const customBlockElement: VElement | undefined = - context.parserServices.customBlock; + | undefined = parserServices.parseCustomBlockElement; + const customBlockElement: VElement | undefined = parserServices.customBlock; if (customBlockElement && parseCustomBlockElement) { let lang = getLang(customBlockElement); diff --git a/lib/utils/index.ts b/lib/utils/index.ts index bbec3454..7abeea00 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -4,6 +4,7 @@ import type { AST } from "jsonc-eslint-parser"; import * as jsoncESLintParser from "jsonc-eslint-parser"; import type { AST as V } from "vue-eslint-parser"; import path from "path"; +import { getFilename, getSourceCode } from "eslint-compat-utils"; /** * Define the rule. @@ -26,12 +27,13 @@ export function createRule( }, jsoncDefineRule: rule, create(context: Rule.RuleContext): any { + const sourceCode = getSourceCode(context); if ( - typeof context.parserServices.defineCustomBlocksVisitor === + typeof sourceCode.parserServices.defineCustomBlocksVisitor === "function" && - path.extname(context.getFilename()) === ".vue" + path.extname(getFilename(context)) === ".vue" ) { - return context.parserServices.defineCustomBlocksVisitor( + return sourceCode.parserServices.defineCustomBlocksVisitor( context, jsoncESLintParser, { @@ -64,7 +66,8 @@ export function defineWrapperListener( context: Rule.RuleContext, options: any[], ): RuleListener { - if (!context.parserServices.isJSON) { + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { return {}; } const listener = coreRule.create({ diff --git a/package.json b/package.json index 69e5e6f8..36aea5af 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "homepage": "https://ota-meshi.github.io/eslint-plugin-jsonc/", "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", + "eslint-compat-utils": "^0.1.2", "jsonc-eslint-parser": "^2.0.4", "natural-compare": "^1.4.0" }, @@ -95,7 +96,7 @@ "eslint-config-prettier": "^9.0.0", "eslint-plugin-eslint-comments": "^3.2.0", "eslint-plugin-eslint-plugin": "^5.0.0", - "eslint-plugin-json-schema-validator": "^4.0.0", + "eslint-plugin-json-schema-validator": "^4.6.1", "eslint-plugin-jsonc": "^2.0.0", "eslint-plugin-markdown": "^3.0.0", "eslint-plugin-n": "^16.0.0", diff --git a/tests/lib/as-parser.ts b/tests/lib/as-parser.ts index c29f5ffb..83cfa9bd 100644 --- a/tests/lib/as-parser.ts +++ b/tests/lib/as-parser.ts @@ -1,7 +1,8 @@ import path from "path"; import assert from "assert"; -import { ESLint } from "../../tools/lib/eslint-compat"; import plugin from "../../lib/index"; +import { getESLint } from "eslint-compat-utils"; +const ESLint = getESLint(); // ----------------------------------------------------------------------------- // Tests diff --git a/tests/lib/auto-rule.ts b/tests/lib/auto-rule.ts index ba459b18..0a82b6a3 100644 --- a/tests/lib/auto-rule.ts +++ b/tests/lib/auto-rule.ts @@ -1,8 +1,9 @@ import path from "path"; import fs from "fs"; import assert from "assert"; -import { ESLint } from "../../tools/lib/eslint-compat"; import plugin from "../../lib/index"; +import { getESLint } from "eslint-compat-utils"; +const ESLint = getESLint(); // ----------------------------------------------------------------------------- // Tests diff --git a/tests/lib/eslint-plugin.ts b/tests/lib/eslint-plugin.ts index 1a0b08ac..08dd795b 100644 --- a/tests/lib/eslint-plugin.ts +++ b/tests/lib/eslint-plugin.ts @@ -1,7 +1,8 @@ import path from "path"; import assert from "assert"; -import { ESLint } from "../../tools/lib/eslint-compat"; import plugin from "../../lib/index"; +import { getESLint } from "eslint-compat-utils"; +const ESLint = getESLint(); // ----------------------------------------------------------------------------- // Tests diff --git a/tools/lib/eslint-compat.ts b/tools/lib/eslint-compat.ts deleted file mode 100644 index 5dfc0ab3..00000000 --- a/tools/lib/eslint-compat.ts +++ /dev/null @@ -1,97 +0,0 @@ -// @ts-check -import * as eslint from "eslint"; - -type ESLintCLIEngine = any; - -// eslint-disable-next-line @typescript-eslint/no-namespace -- ignore -export namespace ESLint { - export type LintResult = eslint.ESLint.LintResult; -} - -export const ESLint = eslint.ESLint || getESLintClassForV6(); -// export const ESLint = getESLintClassForV6() - -/** Build the ESLint class that ESLint v6 compatible. */ -function getESLintClassForV6(): typeof eslint.ESLint { - const CLIEngine = (eslint as any).CLIEngine; - class ESLintForV6 { - private readonly engine: ESLintCLIEngine; - - public static get version() { - return CLIEngine.version; - } - - public constructor(options?: eslint.ESLint.Options) { - const { - overrideConfig: { plugins, globals, rules, ...overrideConfig } = { - plugins: [], - globals: {}, - rules: {}, - }, - fix, - reportUnusedDisableDirectives, - plugins: pluginsMap, - ...otherOptions - } = options || {}; - const newOptions: ESLintCLIEngine["Options"] = { - fix: Boolean(fix), - reportUnusedDisableDirectives: reportUnusedDisableDirectives - ? reportUnusedDisableDirectives !== "off" - : undefined, - ...otherOptions, - - globals: globals - ? Object.keys(globals).filter((n) => globals[n]) - : undefined, - plugins: plugins || [], - rules: rules - ? Object.entries(rules).reduce( - (o, [ruleId, opt]) => { - if (opt) { - o[ruleId] = opt; - } - return o; - }, - {} as NonNullable, - ) - : undefined, - ...overrideConfig, - }; - this.engine = new CLIEngine(newOptions); - - for (const [name, plugin] of Object.entries(pluginsMap || {})) { - this.engine.addPlugin(name, plugin); - } - } - - // eslint-disable-next-line @typescript-eslint/require-await -- ignore - public async lintText( - ...params: Parameters - ): ReturnType { - const result = this.engine.executeOnText(params[0], params[1]?.filePath); - return result.results; - } - - // eslint-disable-next-line @typescript-eslint/require-await -- ignore - public async lintFiles( - ...params: Parameters - ): ReturnType { - const result = this.engine.executeOnFiles( - Array.isArray(params[0]) ? params[0] : [params[0]], - ); - return result.results; - } - - // eslint-disable-next-line @typescript-eslint/require-await -- ignore - public static async outputFixes( - ...params: Parameters<(typeof eslint.ESLint)["outputFixes"]> - ): ReturnType<(typeof eslint.ESLint)["outputFixes"]> { - return CLIEngine.outputFixes({ - results: params[0], - } as any); - } - } - - const eslintClass = ESLintForV6 as never; - return eslintClass; -} diff --git a/tools/new-rule.ts b/tools/new-rule.ts index 56e6ad20..7bf06bfd 100644 --- a/tools/new-rule.ts +++ b/tools/new-rule.ts @@ -24,6 +24,7 @@ const logger = console; ruleFile, ` import type { AST } from "jsonc-eslint-parser" +import { getSourceCode } from "eslint-compat-utils"; import { createRule, defineWrapperListener, getCoreRule } from "../utils" const coreRule = getCoreRule("${ruleId}") @@ -40,9 +41,10 @@ export default createRule("${ruleId}", { type: coreRule.meta!.type!, }, create(context) { - // if (!context.parserServices.isJSON) { - // return {} - // } + const sourceCode = getSourceCode(context); + if (!sourceCode.parserServices.isJSON) { + return {}; + } return defineWrapperListener(coreRule, context, context.options) }, })