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

fix: incorrect auto-fix in yml/sort-keys rule #316

Merged
merged 2 commits into from Mar 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-frogs-smash.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": patch
---

fix: incorrect auto-fix in `yml/sort-keys` rule
1 change: 1 addition & 0 deletions .eslintrc.for-vscode.js
@@ -1,6 +1,7 @@
"use strict";

module.exports = {
root: true,
extends: [require.resolve("./.eslintrc.js")],
overrides: [
{
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -17,6 +17,7 @@
"clean": "rimraf .nyc_output dist coverage",
"lint": "eslint . --ext .js,.vue,.ts,.json,.md,.yml,.yaml",
"eslint-fix": "eslint . --ext .js,.vue,.ts,.json,.md,.yml,.yaml --fix",
"eslint-test-fix": "eslint tests/src/rules/*.ts --fix -c .eslintrc.for-vscode.js",
"pretest:base": "cross-env DEBUG=eslint-plugin-yml*",
"test:base": "npm run mocha -- \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"test": "npm run test:base",
Expand Down Expand Up @@ -90,7 +91,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-eslint-plugin": "^5.0.0",
"eslint-plugin-eslint-rule-tester": "^0.5.1",
"eslint-plugin-eslint-rule-tester": "^0.6.0",
"eslint-plugin-json-schema-validator": "^4.7.4",
"eslint-plugin-jsonc": "^2.0.0",
"eslint-plugin-markdown": "^3.0.0",
Expand Down
5 changes: 1 addition & 4 deletions src/rules/sort-keys.ts
Expand Up @@ -720,10 +720,7 @@ export default createRule("sort-keys", {
nodeLocs.range[0],
nodeLocs.range[0] + diffIndent,
)}`;
yield fixer.insertTextBeforeRange(
moveTargetLocs.range,
`${insertCode}${moveTargetLocs.loc.start.line === 1 ? "\n" : ""}`,
);
yield fixer.insertTextBeforeRange(moveTargetLocs.range, insertCode);

const removeRange: AST.Range = [
getNewlineStartIndex(nodeLocs.range[0]),
Expand Down
96 changes: 96 additions & 0 deletions tests/src/rules/sort-keys.ts
Expand Up @@ -429,6 +429,102 @@ tester.run(
"Expected mapping keys to be in specified order. 'e' should be before 'z'.",
],
},
{
code: `- b: 1
c: 2
a: 3
`,
errors: [
{
message:
"Expected mapping keys to be in ascending order. 'a' should be before 'c'.",
line: 3,
column: 3,
},
],
output: `- a: 3
b: 1
c: 2
`,
},
{
code: `
- b: 1
c: 2
a: 3
`,
errors: [
{
message:
"Expected mapping keys to be in ascending order. 'a' should be before 'c'.",
line: 4,
column: 3,
},
],
output: `
- a: 3
b: 1
c: 2
`,
},
{
code: `b: 1
c: 2
a: 3
`,
errors: [
{
message:
"Expected mapping keys to be in ascending order. 'a' should be before 'c'.",
line: 3,
column: 1,
},
],
output: `
a: 3
b: 1
c: 2
`,
},
{
code: ` b: 1
c: 2
a: 3
`,
errors: [
{
message:
"Expected mapping keys to be in ascending order. 'a' should be before 'c'.",
line: 3,
column: 2,
},
],
output: `
a: 3
b: 1
c: 2
`,
},
{
code: `
b: 1
c: 2
a: 3
`,
errors: [
{
message:
"Expected mapping keys to be in ascending order. 'a' should be before 'c'.",
line: 4,
column: 1,
},
],
output: `
a: 3
b: 1
c: 2
`,
},
],
},
),
Expand Down