Skip to content

Commit

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

* Create soft-rules-turn.md

* fix
  • Loading branch information
ota-meshi committed Oct 8, 2023
1 parent f9b8214 commit 884b74b
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 138 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-rules-turn.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-toml": minor
---

feat: use eslint-compat-utils
8 changes: 8 additions & 0 deletions .eslintrc.js
Expand Up @@ -30,6 +30,14 @@ module.exports = {
"jsonc/array-element-newline": "off",
"jsonc/object-property-newline": "off",
"jsonc/object-curly-newline": "off",
"no-restricted-properties": [
"error",
{ object: "context", property: "getSourceCode" },
{ object: "context", property: "getFilename" },
{ object: "context", property: "getCwd" },
{ object: "context", property: "getScope" },
{ object: "context", property: "parserServices" },
],
},
overrides: [
{
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"debug": "^4.1.1",
"eslint-compat-utils": "^0.1.2",
"lodash": "^4.17.19",
"toml-eslint-parser": "^0.6.0"
},
Expand All @@ -83,7 +84,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",
Expand Down
4 changes: 3 additions & 1 deletion src/rules/comma-style.ts
Expand Up @@ -4,6 +4,7 @@ import {
defineWrapperListener,
getCoreRule,
} from "../utils";
import { getSourceCode } from "../utils/compat";
const coreRule = getCoreRule("comma-style");

export default createRule("comma-style", {
Expand All @@ -21,7 +22,8 @@ export default createRule("comma-style", {
type: coreRule.meta!.type!,
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}

Expand Down
6 changes: 3 additions & 3 deletions src/rules/indent.ts
Expand Up @@ -3,6 +3,7 @@ import { getStaticTOMLValue } from "toml-eslint-parser";
import type { TOMLToken } from "../types";
import { createRule } from "../utils";
import { isCommentToken, isEqualSign } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";
const ITERATION_OPTS = Object.freeze({
includeComments: true,
} as const);
Expand Down Expand Up @@ -80,15 +81,14 @@ export default createRule("indent", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const { getIndentText, outdent } = buildIndentUtility(context.options[0]);
const subTablesOffset: Offset = context.options[1]?.subTables ?? 0;
const keyValuePairsOffset: Offset = context.options[1]?.keyValuePairs ?? 0;

const sourceCode = context.getSourceCode();

const offsets = new Map<TOMLToken, OffsetInfo>();

/**
Expand Down
6 changes: 3 additions & 3 deletions src/rules/key-spacing.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from "toml-eslint-parser";
import type { RuleContext, RuleListener } from "../types";
import { createRule } from "../utils";
import { isEqualSign } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -258,7 +259,8 @@ export default createRule("key-spacing", {
* Create rule visitor
*/
function create(context: RuleContext): RuleListener {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
/**
Expand All @@ -276,8 +278,6 @@ function create(context: RuleContext): RuleListener {
align: alignmentOptions,
} = initOptions(options);

const sourceCode = context.getSourceCode();

/**
* Determines if the given property is key/value property.
* @param {ASTNode} property Property node to check.
Expand Down
6 changes: 3 additions & 3 deletions src/rules/keys-order.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from "toml-eslint-parser";
import { getStaticTOMLValue } from "toml-eslint-parser";
import lodash from "lodash";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

type KeyData = {
key: string;
Expand All @@ -24,12 +25,11 @@ export default createRule("keys-order", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}

const sourceCode = context.getSourceCode();

/**
* Apply new key
*/
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-mixed-type-in-array.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

type TypeMap = {
string: string;
Expand Down Expand Up @@ -51,7 +52,8 @@ export default createRule("no-mixed-type-in-array", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const typeMap: TypeMap = {
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-non-decimal-integer.ts
@@ -1,6 +1,7 @@
import type { AST } from "toml-eslint-parser";
import type { Fix, RuleFixer } from "../types";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

/**
* Convert the given string to decimal string
Expand Down Expand Up @@ -55,7 +56,8 @@ export default createRule("no-non-decimal-integer", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const allowHexadecimal = Boolean(context.options[0]?.allowHexadecimal);
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-space-dots.ts
@@ -1,4 +1,5 @@
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("no-space-dots", {
meta: {
Expand All @@ -16,10 +17,10 @@ export default createRule("no-space-dots", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

return {
TOMLKey(node) {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-unreadable-number-separator.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("no-unreadable-number-separator", {
meta: {
Expand All @@ -17,10 +18,10 @@ export default createRule("no-unreadable-number-separator", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

type CharCountData = {
count: number;
Expand Down
5 changes: 3 additions & 2 deletions src/rules/padding-line-between-pairs.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from "toml-eslint-parser";
import { getStaticTOMLValue } from "toml-eslint-parser";
import type { TOMLToken } from "../types";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("padding-line-between-pairs", {
meta: {
Expand All @@ -19,10 +20,10 @@ export default createRule("padding-line-between-pairs", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

/**
* Verify pairs
Expand Down
5 changes: 3 additions & 2 deletions src/rules/padding-line-between-tables.ts
@@ -1,6 +1,7 @@
import type { AST } from "toml-eslint-parser";
import type { TOMLToken } from "../types";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("padding-line-between-tables", {
meta: {
Expand All @@ -18,10 +19,10 @@ export default createRule("padding-line-between-tables", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

/**
* Verify tables
Expand Down
4 changes: 3 additions & 1 deletion src/rules/precision-of-fractional-seconds.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("precision-of-fractional-seconds", {
meta: {
Expand Down Expand Up @@ -27,7 +28,8 @@ export default createRule("precision-of-fractional-seconds", {
type: "problem",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const max = context.options[0]?.max ?? 3;
Expand Down
4 changes: 3 additions & 1 deletion src/rules/precision-of-integer.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

type MaxValues = {
"+": string;
Expand Down Expand Up @@ -99,7 +100,8 @@ export default createRule("precision-of-integer", {
type: "problem",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const maxBit = context.options[0]?.maxBit ?? 64;
Expand Down
4 changes: 3 additions & 1 deletion src/rules/quoted-keys.ts
@@ -1,4 +1,5 @@
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("quoted-keys", {
meta: {
Expand Down Expand Up @@ -30,7 +31,8 @@ export default createRule("quoted-keys", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/space-eq-sign.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("space-eq-sign", {
meta: {
Expand All @@ -18,10 +19,10 @@ export default createRule("space-eq-sign", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

/**
* Reports an equal sign token as a rule violation
Expand Down
6 changes: 3 additions & 3 deletions src/rules/spaced-comment.ts
@@ -1,6 +1,7 @@
import type { AST } from "toml-eslint-parser";
import lodash from "lodash";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -156,12 +157,11 @@ export default createRule("spaced-comment", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}

const sourceCode = context.getSourceCode();

// Unless the first option is never, require a space
const requireSpace = context.options[0] !== "never";

Expand Down
5 changes: 3 additions & 2 deletions src/rules/table-bracket-spacing.ts
@@ -1,5 +1,6 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("table-bracket-spacing", {
meta: {
Expand All @@ -24,10 +25,10 @@ export default createRule("table-bracket-spacing", {
type: "layout",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();
const prefer: "never" | "always" = context.options[0] || "never";

/**
Expand Down
5 changes: 3 additions & 2 deletions src/rules/tables-order.ts
Expand Up @@ -2,6 +2,7 @@ import type { AST } from "toml-eslint-parser";
import { getStaticTOMLValue } from "toml-eslint-parser";
import lodash from "lodash";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

type KeyData = {
key: string | number;
Expand Down Expand Up @@ -47,10 +48,10 @@ export default createRule("tables-order", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isTOML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isTOML) {
return {};
}
const sourceCode = context.getSourceCode();

/**
* Apply new key
Expand Down

0 comments on commit 884b74b

Please sign in to comment.