Skip to content

Commit

Permalink
feat: use eslint-compat-utils (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Oct 8, 2023
1 parent 19501c6 commit 8d503dd
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 153 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-cameras-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": minor
---

feat: use eslint-compat-utils
51 changes: 51 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/auto.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/key-name-casing.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-bigint-literals.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-binary-expression.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {};
}

Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-binary-numeric-literals.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-comments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleListener } from "../types";
import { getSourceCode } from "eslint-compat-utils";
import { createRule } from "../utils";

export default createRule("no-comments", {
Expand All @@ -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()) {
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-escape-sequence-in-identifier.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-hexadecimal-numeric-literals.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-infinity.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-nan.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-number-props.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-numeric-separators.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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") {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-octal-numeric-literals.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-parenthesized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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"]]: (
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-plus-sign.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 === "+") {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-regexp-literals.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-template-literals.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 8d503dd

Please sign in to comment.