forked from eslint/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new rule that checks that the top-level item in a JSON file is either an array or an object, as suggested by RFC8259. Not turned on in the recommended config because the software that had this issue is almost entirely obsolete. Refs eslint#68.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @fileoverview Rule to ensure top-level items are either an array or ojbect. | ||
* @author Joe Hildebrand | ||
*/ | ||
|
||
export default { | ||
meta: { | ||
type: /** @type {const} */ ("problem"), | ||
|
||
docs: { | ||
description: | ||
"Disallow JSON top-level items are not an array or object", | ||
}, | ||
|
||
messages: { | ||
topLevel: | ||
'Top level item should be array or object, got "{{type}}".', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
Document(node) { | ||
const { type } = node.body; | ||
if (type !== "Object" && type !== "Array") { | ||
context.report({ | ||
loc: node.loc, | ||
messageId: "topLevel", | ||
data: { type }, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @fileoverview Tests for top-level-interop rule. | ||
* @author Joe Hildebrand | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Imports | ||
//------------------------------------------------------------------------------ | ||
|
||
import rule from "../../src/rules/top-level-interop.js"; | ||
import json from "../../src/index.js"; | ||
import { RuleTester } from "eslint"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
plugins: { | ||
json, | ||
}, | ||
language: "json/json", | ||
}); | ||
|
||
ruleTester.run("top-level-interop", rule, { | ||
valid: [ | ||
"[]", | ||
{ | ||
code: "[1]", | ||
language: "json/json5", | ||
}, | ||
{ | ||
code: "[1, 2]", | ||
language: "json/json5", | ||
}, | ||
"{}", | ||
{ | ||
code: '{"foo": 1}', | ||
language: "json/json5", | ||
}, | ||
{ | ||
code: '{"foo": 1, "foo": 2}', | ||
language: "json/json5", | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "1", | ||
errors: [ | ||
{ | ||
messageId: "topLevel", | ||
data: { | ||
type: "Number", | ||
}, | ||
line: 1, | ||
column: 1, | ||
endLine: 1, | ||
endColumn: 2, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "true", | ||
errors: [ | ||
{ | ||
messageId: "topLevel", | ||
data: { | ||
type: "Boolean", | ||
}, | ||
line: 1, | ||
column: 1, | ||
endLine: 1, | ||
endColumn: 5, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "null", | ||
errors: [ | ||
{ | ||
messageId: "topLevel", | ||
data: { | ||
type: "Null", | ||
}, | ||
line: 1, | ||
column: 1, | ||
endLine: 1, | ||
endColumn: 5, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '"foo"', | ||
errors: [ | ||
{ | ||
messageId: "topLevel", | ||
data: { | ||
type: "String", | ||
}, | ||
line: 1, | ||
column: 1, | ||
endLine: 1, | ||
endColumn: 6, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |