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

feat: add rule no-unnormalized-keys #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/rules/no-unnormalized-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @fileoverview Rule to detect unnormalized keys in JSON.
* @author Bradley Meck Farias
*/

export default {
meta: {
type: "problem",

docs: {
description: "Disallow JSON keys that are not normalized",
},

messages: {
unnormalizedKey: "Unnormalized key found.",
},

schema: {
type: "array",
minItems: 0,
maxItems: 1,
items: {
enum: ["NFC", "NFD", "NFKC", "NFKD"],
},
},
Comment on lines +18 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
schema: {
type: "array",
minItems: 0,
maxItems: 1,
items: {
enum: ["NFC", "NFD", "NFKC", "NFKD"],
},
},
schema: [
{
enum: ["NFC", "NFD", "NFKC", "NFKD"],
},
],

This can be simplified by using the shorthand form (https://eslint.org/docs/latest/extend/custom-rules#options-schemas, the first format).

},

create(context) {
const normalization = context.options.length
? s => s.normalize(context.options[0])
: s => s.normalize();
return {
Member(node) {
const key =
node.name.type === "String"
? node.name.value
: node.name.name;
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an invalid test where node.name is an Identifier node?


if (normalization(key) !== key) {
context.report({
loc: node.name.loc,
messageId: "unnormalizedKey",
});
}
},
};
},
};
61 changes: 61 additions & 0 deletions tests/rules/no-unnormalized-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @fileoverview Tests for no-empty-keys rule.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @fileoverview Tests for no-empty-keys rule.
* @fileoverview Tests for no-unnormalized-keys rule.

* @author Bradley Meck Farias
*/

//------------------------------------------------------------------------------
// Imports
//------------------------------------------------------------------------------

import rule from "../../src/rules/no-unnormalized-keys.js";
import json from "../../src/index.js";
import { RuleTester } from "eslint";

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
plugins: {
json,
},
language: "json/json",
});

const o = "\u1E9B\u0323";

ruleTester.run("no-unnormalized-keys", rule, {
valid: [
`{"${o}":"NFC"}`,
{
code: `{"${o}":"NFC"}`,
options: ["NFC"],
},
{
code: `{"${o.normalize("NFD")}":"NFD"}`,
options: ["NFD"],
},
{
code: `{"${o.normalize("NFKC")}":"NFKC"}`,
options: ["NFKC"],
},
{
code: `{"${o.normalize("NFKD")}":"NFKD"}`,
options: ["NFKD"],
},
],
invalid: [
{
code: `{"${o.normalize("NFD")}":"NFD"}`,
errors: [
{
messageId: "unnormalizedKey",
line: 1,
column: 2,
endLine: 1,
endColumn: 7,
},
],
},
],
});