Skip to content

Commit

Permalink
fix: incorrect auto-fix in yml/sort-keys rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 21, 2024
1 parent 2829eb6 commit e9c54fa
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
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

0 comments on commit e9c54fa

Please sign in to comment.