Skip to content

Commit 10cacf9

Browse files
authored
fix: fix some bugs and improve test cases (#534)
* fix: fix some bugs and improve test cases * Apply suggestion from @ota-meshi * update
1 parent 102cb04 commit 10cacf9

File tree

75 files changed

+592
-677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+592
-677
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"markdown"
1212
],
1313
"eslint.options": {
14-
"overrideConfigFile": "./.eslintrc.for-vscode.mjs"
14+
// Use a specific ESLint config file for VSCode ESLint extension
15+
// "overrideConfigFile": "./.eslintrc.for-vscode.mjs"
1516
},
1617
"typescript.validate.enable": true,
1718
"javascript.validate.enable": false,

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
"test:debug": "npm run mocha -- \"tests/src/**/*.ts\" --reporter dot",
3333
"update": "npm run ts -- ./tools/update.ts && npm run eslint-fix && npm run cover",
3434
"new": "npm run ts -- ./tools/new-rule.ts",
35-
"predocs:watch": "npm run build:ts",
35+
"predocs:watch": "npm run build",
3636
"docs:watch": "vitepress dev docs",
37-
"docs:build": "npm run build:ts && vitepress build docs",
37+
"docs:build": "npm run build && vitepress build docs",
3838
"preversion": "npm test && git add .",
3939
"version": "env-cmd -e version -- npm run update && git add .",
4040
"version:ci": "env-cmd -e version-ci -- npm run update && changeset version",
@@ -69,7 +69,6 @@
6969
"debug": "^4.3.2",
7070
"diff-sequences": "^29.0.0",
7171
"escape-string-regexp": "5.0.0",
72-
"eslint-compat-utils": "^0.6.0",
7372
"natural-compare": "^1.4.0",
7473
"yaml-eslint-parser": "^1.2.1"
7574
},

src/configs/flat/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { ESLint, Linter } from "eslint";
22
import * as parser from "yaml-eslint-parser";
3-
import * as pluginModule from "../../index.js";
3+
import plugin from "../../index.js";
44

55
export default [
66
{
77
plugins: {
88
get yml(): ESLint.Plugin {
99
// Delayed reference to avoid circular dependency
10-
return pluginModule.default as unknown as ESLint.Plugin;
10+
return plugin;
1111
},
1212
},
1313
},
@@ -23,4 +23,4 @@ export default [
2323
"spaced-comment": "off",
2424
},
2525
},
26-
] satisfies Linter.FlatConfig[];
26+
] satisfies Linter.Config[];

src/configs/flat/prettier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ export default [
2222
"yml/quotes": "off",
2323
},
2424
},
25-
] satisfies Linter.FlatConfig[];
25+
] satisfies Linter.Config[];

src/configs/flat/recommended.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export default [
1717
"yml/vue-custom-block/no-parsing-error": "error",
1818
},
1919
},
20-
] satisfies Linter.FlatConfig[];
20+
] satisfies Linter.Config[];

src/configs/flat/standard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export default [
3333
"yml/vue-custom-block/no-parsing-error": "error",
3434
},
3535
},
36-
] satisfies Linter.FlatConfig[];
36+
] satisfies Linter.Config[];

src/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Linter } from "eslint";
22
import type { RuleDefinition } from "@eslint/core";
3-
import type { RuleModule } from "./types.js";
43
import { rules as ruleList } from "./utils/rules.js";
54
import base from "./configs/flat/base.js";
65
import recommended from "./configs/flat/recommended.js";
@@ -22,12 +21,8 @@ const configs = {
2221
"flat/prettier": prettier as Linter.Config[],
2322
};
2423

25-
const rules = ruleList.reduce(
26-
(obj, r) => {
27-
obj[r.meta.docs.ruleName] = r;
28-
return obj;
29-
},
30-
{} as { [key: string]: RuleModule },
24+
const rules = Object.fromEntries(
25+
ruleList.map((r) => [r.meta.docs.ruleName, r]),
3126
) as Record<string, RuleDefinition>;
3227

3328
const languages = {

src/language/token-store.ts

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,42 @@ function getLastIndex(
5858
/**
5959
* Normalizes the options for cursor methods.
6060
*/
61-
function normalizeOptions(options: CursorWithSkipOptions | undefined): {
62-
includeComments: boolean;
63-
filter: FilterPredicate | null;
61+
function normalizeSkipOptions(options: CursorWithSkipOptions | undefined): {
62+
filter: FilterPredicate;
6463
skip: number;
6564
} {
6665
if (typeof options === "number") {
67-
return { includeComments: false, filter: null, skip: options };
66+
return { filter: isNotComment, skip: options };
6867
}
6968
if (typeof options === "function") {
70-
return { includeComments: false, filter: options, skip: 0 };
69+
return {
70+
filter: (n) => {
71+
if (isComment(n)) {
72+
return false;
73+
}
74+
return options(n);
75+
},
76+
skip: 0,
77+
};
78+
}
79+
let filter: FilterPredicate;
80+
if (options?.includeComments) {
81+
filter = options?.filter ?? (() => true);
82+
} else {
83+
if (options?.filter) {
84+
const baseFilter = options?.filter;
85+
filter = (token) => {
86+
if (isComment(token)) {
87+
return false;
88+
}
89+
return baseFilter(token);
90+
};
91+
} else {
92+
filter = isNotComment;
93+
}
7194
}
7295
return {
73-
includeComments: options?.includeComments ?? false,
74-
filter: options?.filter ?? null,
96+
filter,
7597
skip: options?.skip ?? 0,
7698
};
7799
}
@@ -94,7 +116,7 @@ function isNotComment(token: YAMLToken): boolean {
94116
* Normalizes the options for cursor methods with count.
95117
*/
96118
function normalizeCountOptions(options: CursorWithCountOptions | undefined): {
97-
filter: FilterPredicate | null;
119+
filter: FilterPredicate;
98120
count: number;
99121
} {
100122
if (typeof options === "number") {
@@ -111,11 +133,13 @@ function normalizeCountOptions(options: CursorWithCountOptions | undefined): {
111133
count: 0,
112134
};
113135
}
114-
let filter = options?.filter;
115-
if (!options?.includeComments) {
116-
if (filter) {
117-
const baseFilter = filter;
118-
filter = (token: YAMLToken) => {
136+
let filter: FilterPredicate;
137+
if (options?.includeComments) {
138+
filter = options?.filter ?? (() => true);
139+
} else {
140+
if (options?.filter) {
141+
const baseFilter = options?.filter;
142+
filter = (token) => {
119143
if (isComment(token)) {
120144
return false;
121145
}
@@ -126,7 +150,7 @@ function normalizeCountOptions(options: CursorWithCountOptions | undefined): {
126150
}
127151
}
128152
return {
129-
filter: filter ?? null,
153+
filter,
130154
count: options?.count ?? 0,
131155
};
132156
}
@@ -165,7 +189,7 @@ export class TokenStore {
165189
node: YAMLSyntaxElement,
166190
options?: CursorWithSkipOptions,
167191
): YAMLToken | null {
168-
const { filter, skip } = normalizeOptions(options);
192+
const { filter, skip } = normalizeSkipOptions(options);
169193
const startIndex = getFirstIndex(
170194
this.allTokens,
171195
this.tokenStartToIndex,
@@ -199,7 +223,7 @@ export class TokenStore {
199223
node: YAMLSyntaxElement,
200224
options?: CursorWithSkipOptions,
201225
): YAMLToken | null {
202-
const { filter, skip } = normalizeOptions(options);
226+
const { filter, skip } = normalizeSkipOptions(options);
203227
const startIndex = getFirstIndex(
204228
this.allTokens,
205229
this.tokenStartToIndex,
@@ -234,7 +258,7 @@ export class TokenStore {
234258
right: YAMLSyntaxElement,
235259
options?: CursorWithSkipOptions,
236260
): YAMLToken | null {
237-
const { filter, skip } = normalizeOptions(options);
261+
const { filter, skip } = normalizeSkipOptions(options);
238262
const startIndex = getFirstIndex(
239263
this.allTokens,
240264
this.tokenStartToIndex,
@@ -268,7 +292,7 @@ export class TokenStore {
268292
node: YAMLSyntaxElement,
269293
options?: CursorWithSkipOptions,
270294
): YAMLToken | null {
271-
const { filter, skip } = normalizeOptions(options);
295+
const { filter, skip } = normalizeSkipOptions(options);
272296
const endIndex = getLastIndex(
273297
this.allTokens,
274298
this.tokenStartToIndex,
@@ -297,7 +321,7 @@ export class TokenStore {
297321
node: YAMLSyntaxElement,
298322
options?: CursorWithSkipOptions,
299323
): YAMLToken | null {
300-
const { filter, skip } = normalizeOptions(options);
324+
const { filter, skip } = normalizeSkipOptions(options);
301325
const startIndex = getFirstIndex(
302326
this.allTokens,
303327
this.tokenStartToIndex,

src/rules/block-mapping-colon-indicator-newline.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { AST } from "yaml-eslint-parser";
22
import { createRule } from "../utils/index.js";
33
import { isColon } from "../utils/ast-utils.js";
4-
import { getSourceCode } from "../utils/compat.js";
54

65
export default createRule("block-mapping-colon-indicator-newline", {
76
meta: {
@@ -26,7 +25,7 @@ export default createRule("block-mapping-colon-indicator-newline", {
2625
type: "layout",
2726
},
2827
create(context) {
29-
const sourceCode = getSourceCode(context);
28+
const sourceCode = context.sourceCode;
3029
if (!sourceCode.parserServices?.isYAML) {
3130
return {};
3231
}

src/rules/block-mapping-question-indicator-newline.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createRule } from "../utils/index.js";
22
import { isQuestion } from "../utils/ast-utils.js";
3-
import { getSourceCode } from "../utils/compat.js";
43

54
export default createRule("block-mapping-question-indicator-newline", {
65
meta: {
@@ -25,7 +24,7 @@ export default createRule("block-mapping-question-indicator-newline", {
2524
type: "layout",
2625
},
2726
create(context) {
28-
const sourceCode = getSourceCode(context);
27+
const sourceCode = context.sourceCode;
2928
if (!sourceCode.parserServices?.isYAML) {
3029
return {};
3130
}

0 commit comments

Comments
 (0)