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: rule no-unsafe-values #30

Open
wants to merge 3 commits 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
60 changes: 60 additions & 0 deletions src/rules/no-unsafe-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @fileoverview Rule to detect unsafe values in JSON.
* @author Bradley Meck Farias
*/

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

docs: {
description: "Disallow JSON values that are unsafe for interchange",
},

messages: {
unsafeNumber: "Number outside safe range found.",
loneSurrogate: "Lone surrogate '{{ surrogate }}' found.",
},
},

create(context) {
return {
Number(node) {
if (Number.isFinite(node.value) !== true) {
context.report({
loc: node.loc,
messageId: "unsafeNumber",
});
}
},
String(node) {
if (node.value.isWellFormed) {
if (node.value.isWellFormed()) {
return;
}
}
// match any high surrogate and, if it exists, a paired low surrogate
// match any low surrogate not already matched
const surrogatePattern =
/[\uD800-\uDBFF][\uDC00-\uDFFF]?|[\uDC00-\uDFFF]/gu;
let match = surrogatePattern.exec(node.value);
while (match) {
// only need to report non-paired surrogates
if (match[0].length < 2) {
context.report({
loc: node.loc,
messageId: "loneSurrogate",
Copy link
Member

Choose a reason for hiding this comment

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

Because we're not showing the actual location of the lone surrogate, can we show the surrogate itself? That would give folks a bit more information about where the problem is.

Copy link
Author

Choose a reason for hiding this comment

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

sure

data: {
surrogate: JSON.stringify(match[0]).slice(
1,
-1,
),
},
});
}
match = surrogatePattern.exec(node.value);
}
},
};
},
};
132 changes: 132 additions & 0 deletions tests/rules/no-unsafe-values.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @fileoverview Tests for no-empty-keys rule.
* @author Bradley Meck Farias
*/

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

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

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

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

ruleTester.run("no-unsafe-values", rule, {
valid: [
"123",
{
code: "1234",
language: "json/json5",
},
{
code: "12345",
language: "json/json5",
},
'"🔥"',
'"\\ud83d\\udd25"',
],
invalid: [
{
code: "2e308",
errors: [
{
messageId: "unsafeNumber",
line: 1,
column: 1,
endLine: 1,
endColumn: 6,
},
],
},
{
code: "-2e308",
errors: [
{
messageId: "unsafeNumber",
line: 1,
column: 1,
endLine: 1,
endColumn: 7,
},
],
},
{
code: '"\ud83d"',
errors: [
{
messageId: "loneSurrogate",
line: 1,
column: 1,
endLine: 1,
endColumn: 4,
},
Comment on lines +67 to +73
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 data to all test errors with messageId: "loneSurrogate"?

],
},
{
code: '"\\ud83d"',
errors: [
{
messageId: "loneSurrogate",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: '"\udd25"',
errors: [
{
messageId: "loneSurrogate",
line: 1,
column: 1,
endLine: 1,
endColumn: 4,
},
],
},
{
code: '"\\udd25"',
errors: [
{
messageId: "loneSurrogate",
line: 1,
column: 1,
endLine: 1,
endColumn: 9,
},
],
},
{
code: '"\ud83d\ud83d"',
errors: [
{
message: "Lone surrogate '\\ud83d' found.",
line: 1,
column: 1,
endLine: 1,
endColumn: 5,
},
{
message: "Lone surrogate '\\ud83d' found.",
line: 1,
column: 1,
endLine: 1,
endColumn: 5,
},
],
},
],
});