diff --git a/.changeset/giant-wolves-tie.md b/.changeset/giant-wolves-tie.md new file mode 100644 index 00000000..3cae96ba --- /dev/null +++ b/.changeset/giant-wolves-tie.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-json-schema-validator": patch +--- + +fix: crash on empty yaml diff --git a/src/utils/ast/yaml.ts b/src/utils/ast/yaml.ts index 6e5e9867..ce951c05 100644 --- a/src/utils/ast/yaml.ts +++ b/src/utils/ast/yaml.ts @@ -126,12 +126,13 @@ export function getYAMLNodeFromPath( ): NodeData { let data: NodeData = { key: (sourceCode) => { + const doc = node.body[0]; if (node.body.length > 1) { - return sourceCode.getFirstToken(node.body[0]).range!; + return (sourceCode.getFirstToken(doc) || doc).range!; } - const dataNode = node.body[0].content; + const dataNode = doc.content; if (dataNode == null) { - return sourceCode.getFirstToken(node.body[0]).range!; + return (sourceCode.getFirstToken(doc) || doc).range!; } if (dataNode.type === "YAMLMapping" || dataNode.type === "YAMLSequence") { return sourceCode.getFirstToken(dataNode).range!; diff --git a/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-errors.json b/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-errors.json new file mode 100644 index 00000000..ac09a1b1 --- /dev/null +++ b/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-errors.json @@ -0,0 +1,9 @@ +[ + { + "message": "Root must be object.", + "line": 1, + "column": 1, + "endLine": 2, + "endColumn": 8 + } +] diff --git a/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-input.yaml b/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-input.yaml new file mode 100644 index 00000000..1bb8bf6d --- /dev/null +++ b/tests/fixtures/rules/no-invalid/invalid/yaml-test/test-empty01-input.yaml @@ -0,0 +1 @@ +# empty