Skip to content

Commit

Permalink
Support JSON5 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Sep 3, 2024
1 parent 31bbec6 commit e9cd9c6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A [remark-lint](https://github.com/remarkjs/remark-lint) rule to check language
- JavaScript
- JSON
- JSONC
- JSON5
- YAML
- CSS

Expand Down
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { default as swc } from "@swc/core";
import { default as yaml } from "js-yaml";
import { default as postcss } from "postcss";
import { default as jsonc } from "jsonc-parser";
import { default as JSON5 } from "json5";

const remarkLintCodeBlockSyntax = lintRule("remark-lint:code-block-syntax", codeSyntax);
export default remarkLintCodeBlockSyntax;

function codeSyntax(tree, file) {
const supportedLangs = ["js", "javascript", "json", "jsonc", "yaml", "yml", "css"];
const supportedLangs = ["js", "javascript", "json", "jsonc", "json5", "yaml", "yml", "css"];
const test = supportedLangs.map((lang) => ({ type: "code", lang }));

visit(tree, test, visitor);
Expand Down Expand Up @@ -44,6 +45,13 @@ function codeSyntax(tree, file) {
}
break;
}
case "json5": {
const reason = checkJson5(value);
if (reason) {
report(reason, "JSON5");
}
break;
}
case "yaml":
case "yml": {
const reason = checkYaml(value);
Expand Down Expand Up @@ -101,6 +109,15 @@ function codeSyntax(tree, file) {
}
}

function checkJson5(code) {
try {
JSON5.parse(code);
return null;
} catch (e) {
return e.message;
}
}

function checkYaml(code) {
try {
yaml.load(code);
Expand Down
18 changes: 18 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ describe("JSONC", () => {
});
});

describe("JSON5", () => {
test("valid", () => {
assert.deepEqual(run("json5", "{} // comment"), []);
});

test("invalid", () => {
assert.deepEqual(run("json5", "{[\n}}"), [
{
column: 1,
line: 1,
message: "Invalid JSON5: JSON5: invalid character '[' at 1:2",
ruleId: "code-block-syntax",
source: "remark-lint",
},
]);
});
});

describe("JavaScript", () => {
test("valid", () => {
assert.deepEqual(run("js", "let a=1"), []);
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@swc/core": "^1.7.22",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"jsonc-parser": "^3.3.1",
"postcss": "^8.4.43",
"unified-lint-rule": "^3.0.0",
Expand Down

0 comments on commit e9cd9c6

Please sign in to comment.