Skip to content

Commit

Permalink
feat: add support for flat config (#198)
Browse files Browse the repository at this point in the history
* feat: add support for flat config

* Create ten-dingos-mate.md

* fix

* fix

* update
  • Loading branch information
ota-meshi committed Mar 19, 2024
1 parent acf75bd commit e5d939f
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-dingos-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-toml": minor
---

feat: add support for flat config
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,28 @@ module.exports = {
"n/file-extension-in-import": "off",
},
},
{
files: ["*.mjs"],
parserOptions: {
sourceType: "module",
},
},
{
files: ["*.md/**", "**/*.md/**"],
rules: {
"n/no-missing-import": "off",
},
},
{
files: ["docs/.vitepress/**/*.*"],
rules: {
"eslint-plugin/require-meta-docs-description": "off",
"eslint-plugin/require-meta-docs-url": "off",
"eslint-plugin/require-meta-type": "off",
"eslint-plugin/prefer-message-ids": "off",
"eslint-plugin/prefer-object-rule": "off",
"eslint-plugin/require-meta-schema": "off",
},
},
],
};
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,38 @@ npm install --save-dev eslint eslint-plugin-toml

### Configuration

Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
#### New (ESLint>=v9) Config (Flat Config)

Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.

Example **eslint.config.js**:

```mjs
import eslintPluginToml from 'eslint-plugin-toml';
export default [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginToml.configs['flat/recommended'],
{
rules: {
// override/add rules settings here, such as:
// 'toml/rule-name': 'error'
}
}
];
```

This plugin provides configs:

- `*.configs['flat/base']` ... Configuration to enable correct TOML parsing.
- `*.configs['flat/recommended']` ... Above, plus rules to prevent errors or unintended behavior.
- `*.configs['flat/standard']` ... Above, plus rules to enforce the common stylistic conventions.

See [the rule list](https://ota-meshi.github.io/eslint-plugin-yml/rules/) to get the `rules` that this plugin provides.

#### Legacy Config (ESLint<v9)

Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.

Example **.eslintrc.js**:

Expand Down
33 changes: 32 additions & 1 deletion docs/user-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,38 @@ npm install --save-dev eslint eslint-plugin-toml

### Configuration

Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
#### New (ESLint>=v9) Config (Flat Config)

Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.

Example **eslint.config.js**:

```mjs
import eslintPluginToml from 'eslint-plugin-toml';
export default [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginToml.configs['flat/recommended'],
{
rules: {
// override/add rules settings here, such as:
// 'toml/rule-name': 'error'
}
}
];
```

This plugin provides configs:

- `*.configs['flat/base']` ... Configuration to enable correct TOML parsing.
- `*.configs['flat/recommended']` ... Above, plus rules to prevent errors or unintended behavior.
- `*.configs['flat/standard']` ... Above, plus rules to enforce the common stylistic conventions.

See [the rule list](https://ota-meshi.github.io/eslint-plugin-yml/rules/) to get the `rules` that this plugin provides.

#### Legacy Config (ESLint<v9)

Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.

Example **.eslintrc.js**:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"dependencies": {
"debug": "^4.1.1",
"eslint-compat-utils": "^0.4.0",
"eslint-compat-utils": "^0.5.0",
"lodash": "^4.17.19",
"toml-eslint-parser": "^0.9.0"
},
Expand Down Expand Up @@ -90,6 +90,7 @@
"eslint-plugin-n": "^16.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-regexp": "^2.0.0",
"eslint-plugin-toml": "^0.9.2",
"eslint-plugin-vue": "^9.0.0",
"eslint-plugin-yml": "^1.0.0",
"espree": "^9.0.0",
Expand Down
23 changes: 23 additions & 0 deletions src/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ESLint } from "eslint";
import * as parser from "toml-eslint-parser";
export default [
{
plugins: {
get toml(): ESLint.Plugin {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
return require("../../index");
},
},
},
{
files: ["*.toml", "**/*.toml"],
languageOptions: {
parser,
},
rules: {
// ESLint core rules known to cause problems with TOML.
"no-irregular-whitespace": "off",
"spaced-comment": "off",
},
},
];
16 changes: 16 additions & 0 deletions src/configs/flat/recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-toml rules
"toml/no-unreadable-number-separator": "error",
"toml/precision-of-fractional-seconds": "error",
"toml/precision-of-integer": "error",
"toml/vue-custom-block/no-parsing-error": "error",
},
},
];
31 changes: 31 additions & 0 deletions src/configs/flat/standard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-toml rules
"toml/array-bracket-newline": "error",
"toml/array-bracket-spacing": "error",
"toml/array-element-newline": "error",
"toml/comma-style": "error",
"toml/indent": "error",
"toml/inline-table-curly-spacing": "error",
"toml/key-spacing": "error",
"toml/keys-order": "error",
"toml/no-space-dots": "error",
"toml/no-unreadable-number-separator": "error",
"toml/padding-line-between-pairs": "error",
"toml/padding-line-between-tables": "error",
"toml/precision-of-fractional-seconds": "error",
"toml/precision-of-integer": "error",
"toml/quoted-keys": "error",
"toml/spaced-comment": "error",
"toml/table-bracket-spacing": "error",
"toml/tables-order": "error",
"toml/vue-custom-block/no-parsing-error": "error",
},
},
];
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import { rules as ruleList } from "./utils/rules";
import base from "./configs/base";
import recommended from "./configs/recommended";
import standard from "./configs/standard";
import flatBase from "./configs/flat/base";
import flatRecommended from "./configs/flat/recommended";
import flatStandard from "./configs/flat/standard";
import * as meta from "./meta";

const configs = {
base,
recommended,
standard,
"flat/base": flatBase,
"flat/recommended": flatRecommended,
"flat/standard": flatStandard,
};

const rules = ruleList.reduce(
Expand Down
3 changes: 2 additions & 1 deletion src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ type LineIndentStep2 = {
expectedIndent: string;
actualIndent: string;
};

/**
* Create get indentation function
*/
export function buildIndentUtility(optionValue?: number | "tab" | null): {
function buildIndentUtility(optionValue?: number | "tab" | null): {
getIndentText: (offset: number) => string;
outdent: (indent: string) => string;
} {
Expand Down
62 changes: 2 additions & 60 deletions src/rules/precision-of-integer.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,11 @@
import type { AST } from "toml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";
import type { MaxValues } from "../utils/bit";
import { maxBitToMaxValues } from "../utils/bit";

type MaxValues = {
"+": string;
"-": string; // Does not include a minus sign.
"0x": string;
"0o": string;
"0b": string;
};
const cacheMaxValues: Record<number, MaxValues> = {};

/**
* Convert the given maxBit to max hex, octal, binary and decimal number strings
*
* *Export for testing.
*/
export function maxBitToMaxValues(maxBit: number): MaxValues {
const binaryMax: number[] = [];
const minusMax: number[] = [0];
const plusMax: number[] = [0];
const hexMax: number[] = [0];
const octalMax: number[] = [0];
for (let index = 0; index < maxBit; index++) {
const binaryNum = index === 0 ? 1 : 0;
binaryMax.push(binaryNum);

processDigits(minusMax, binaryNum, 10);
processDigits(hexMax, binaryNum, 16);
processDigits(octalMax, binaryNum, 8);
if (index > 0) {
processDigits(plusMax, 1, 10);
}
}
return {
"+": plusMax.reverse().join(""),
"-": minusMax.reverse().join(""),
"0x": hexMax
.map((i) => i.toString(16))
.reverse()
.join("")
.toLowerCase(),
"0o": octalMax.reverse().join(""),
"0b": binaryMax.join(""),
};

/** Process digits */
function processDigits(
digits: number[],
binaryNum: number,
radix: 10 | 16 | 8,
) {
let num = binaryNum;
for (let place = 0; place < digits.length; place++) {
num = digits[place] * 2 + num;
digits[place] = num % radix;
num = Math.floor(num / radix);
}
while (num > 0) {
digits.push(num % radix);
num = Math.floor(num / radix);
}
}
}

/**
* Get max values
*/
Expand Down
59 changes: 59 additions & 0 deletions src/utils/bit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export type MaxValues = {
"+": string;
"-": string; // Does not include a minus sign.
"0x": string;
"0o": string;
"0b": string;
};
/**
* Convert the given maxBit to max hex, octal, binary and decimal number strings
*
* *Export for testing.
*/
export function maxBitToMaxValues(maxBit: number): MaxValues {
const binaryMax: number[] = [];
const minusMax: number[] = [0];
const plusMax: number[] = [0];
const hexMax: number[] = [0];
const octalMax: number[] = [0];
for (let index = 0; index < maxBit; index++) {
const binaryNum = index === 0 ? 1 : 0;
binaryMax.push(binaryNum);

processDigits(minusMax, binaryNum, 10);
processDigits(hexMax, binaryNum, 16);
processDigits(octalMax, binaryNum, 8);
if (index > 0) {
processDigits(plusMax, 1, 10);
}
}
return {
"+": plusMax.reverse().join(""),
"-": minusMax.reverse().join(""),
"0x": hexMax
.map((i) => i.toString(16))
.reverse()
.join("")
.toLowerCase(),
"0o": octalMax.reverse().join(""),
"0b": binaryMax.join(""),
};

/** Process digits */
function processDigits(
digits: number[],
binaryNum: number,
radix: 10 | 16 | 8,
) {
let num = binaryNum;
for (let place = 0; place < digits.length; place++) {
num = digits[place] * 2 + num;
digits[place] = num % radix;
num = Math.floor(num / radix);
}
while (num > 0) {
digits.push(num % radix);
num = Math.floor(num / radix);
}
}
}
Loading

0 comments on commit e5d939f

Please sign in to comment.