Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changed to prevent crash when used with language plugins #385

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-suits-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": minor
---

feat: changed to prevent crash when used with language plugins
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.5.0",
"@eslint/json": "^0.8.0",
"@ota-meshi/eslint-plugin": "^0.17.3",
"@ota-meshi/site-kit-eslint-editor-vue": "^0.2.0",
"@types/debug": "^4.1.5",
Expand All @@ -90,7 +91,7 @@
"env-cmd": "^10.1.0",
"esbuild": "^0.24.0",
"esbuild-register": "^3.2.0",
"eslint": "^9.5.0",
"eslint": "^9.16.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-eslint-plugin": "^6.0.0",
"eslint-plugin-eslint-rule-tester": "^0.6.0",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/block-mapping-colon-indicator-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
docs: {
description: "enforce consistent line breaks after `:` indicator",
categories: [
// TODO Switch to "standard" in the major version.

Check warning on line 11 in src/rules/block-mapping-colon-indicator-newline.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Switch to "standard" in the major...'
// "standard"
],
extensionRule: false,
Expand All @@ -30,7 +30,7 @@
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const option: "never" | "always" = context.options[0] || "never";
Expand Down
2 changes: 1 addition & 1 deletion src/rules/block-mapping-question-indicator-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default createRule("block-mapping-question-indicator-newline", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const option: "never" | "always" = context.options[0] || "never";
Expand Down
2 changes: 1 addition & 1 deletion src/rules/block-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default createRule("block-mapping", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const options = parseOptions(context.options[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/block-sequence-hyphen-indicator-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default createRule("block-sequence-hyphen-indicator-newline", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const style: "never" | "always" = context.options[0] || "never";
Expand Down
2 changes: 1 addition & 1 deletion src/rules/block-sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default createRule("block-sequence", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const options = parseOptions(context.options[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/file-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default createRule("file-extension", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const expected: string = context.options[0]?.extension || "yaml";
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flow-mapping-curly-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default createRule("flow-mapping-curly-newline", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const options = normalizeOptionValue(context.options[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flow-mapping-curly-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default createRule("flow-mapping-curly-spacing", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/flow-sequence-bracket-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default createRule("flow-sequence-bracket-newline", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/flow-sequence-bracket-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default createRule("flow-sequence-bracket-spacing", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default createRule("indent", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/key-name-casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default createRule("key-name-casing", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const option: Option = { ...context.options[0] };
Expand Down
2 changes: 1 addition & 1 deletion src/rules/key-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default createRule("key-spacing", {
*/
function create(context: RuleContext): RuleListener {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default createRule("no-empty-document", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default createRule("no-empty-key", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-mapping-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default createRule("no-empty-mapping-value", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-sequence-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default createRule("no-empty-sequence-entry", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-irregular-whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default createRule("no-irregular-whitespace", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
// Module store of error indexes that we have found
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-multiple-empty-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default createRule("no-multiple-empty-lines", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-tab-indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default createRule("no-tab-indent", {
},
create(context: RuleContext) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
return {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-trailing-zeros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default createRule("no-trailing-zeros", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/plain-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default createRule("plain-scalar", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
type Option = {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default createRule("quotes", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
const objectOption = context.options[0] || {};
Expand Down
2 changes: 1 addition & 1 deletion src/rules/require-string-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default createRule("require-string-key", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/sort-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export default createRule("sort-keys", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/sort-sequence-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export default createRule("sort-sequence-values", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}
// Parse options.
Expand Down
2 changes: 1 addition & 1 deletion src/rules/spaced-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default createRule("spaced-comment", {
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface SourceCode {
ast: AST.YAMLProgram;
lines: string[];
hasBOM: boolean;
parserServices: {
parserServices?: {
isYAML?: true;
parseError?: any;
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function createRule(
create(context: Rule.RuleContext): any {
const sourceCode = getSourceCode(context);
if (
typeof sourceCode.parserServices.defineCustomBlocksVisitor ===
typeof sourceCode.parserServices?.defineCustomBlocksVisitor ===
"function" &&
path.extname(getFilename(context)) === ".vue"
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json from "@eslint/json";
import { getPlugin } from "../plugin-store.cjs";
const yaml = getPlugin()
export default [
...yaml.configs["flat/recommended"],
{
plugins: {
json,
},
},

// lint JSON files
{
files: ["**/*.json"],
language: "json/json",
rules: {
"json/no-duplicate-keys": "error",
},
},

// lint JSONC files
{
files: ["**/*.jsonc", ".vscode/*.json"],
language: "json/jsonc",
rules: {
"json/no-duplicate-keys": "error",
},
},

// lint JSON5 files
{
files: ["**/*.json5"],
language: "json/json5",
rules: {
"json/no-duplicate-keys": "error",
},
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo": "bar"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo": "bar"
24 changes: 13 additions & 11 deletions tests/src/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("Integration with eslint-plugin-yml", () => {
it(dir, async () => {
const engine = new LegacyESLint({
cwd: path.join(TEST_ROOT, dir),
// @ts-expect-error -- ignore
extensions: [".js", ".yml"],
plugins: { "eslint-plugin-yml": plugin as any },
});
Expand All @@ -60,17 +61,18 @@ describe("Integration with eslint-plugin-yml", () => {
errors: 0,
},
},
// ...(semver.satisfies(process.version, ">=18")
// ? [
// {
// dir: "with-json",
// expects: {
// files: 2,
// errors: 0,
// },
// },
// ]
// : []),
...(semver.satisfies(process.version, ">=18") &&
semver.satisfies(ESLint.version, ">=9")
? [
{
dir: "with-json",
expects: {
files: 2,
errors: 0,
},
},
]
: []),
]) {
it(dir, async () => {
const engine = new ESLint({
Expand Down
2 changes: 1 addition & 1 deletion tools/new-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
create(context) {
const sourceCode = getSourceCode(context)
if (!sourceCode.parserServices.isYAML) {
if (!sourceCode.parserServices?.isYAML) {
return {}
}

Expand Down Expand Up @@ -147,7 +147,7 @@
cp.execSync(`code "${ruleFile}"`);
cp.execSync(`code "${testFile}"`);
cp.execSync(`code "${docFile}"`);
console.log(

Check warning on line 150 in tools/new-rule.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
`npm run mocha -- "tests/src/**/${ruleId}.ts" --reporter dot --timeout 60000`,
);
})(process.argv[2]);
Loading