-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add support for JSON5 as extension of json.js. #4259
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
Conversation
src/lib/modes.js
Outdated
@@ -10,6 +10,7 @@ export const IDENT_RE = '[a-zA-Z]\\w*'; | |||
export const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*'; | |||
export const NUMBER_RE = '\\b\\d+(\\.\\d+)?'; | |||
export const C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)'; // 0x..., 0..., decimal, float | |||
export const EXTENDED_NUMBER_RE = '([-+]?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)|NaN|[-+]?Infinity'; // 0x..., 0..., decimal, float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this all valid ECMAScript? If so perhaps this belongs in lib/ecmascript
, but I don't think we want to add another global here since this is a very specific use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's all valid ECMAScript. JSON5 maintains the ability, like standard JSON, to be parsed as JavaScript. I moved both EXTENDED_NUMBER_RE
and EXTENDED_NUMBER_MODE
to lib/ecmascript
, as I think moving EXTENDED_NUMBER_RE
alone would possibly lead to a circular dependency.
types/index.d.ts
Outdated
@@ -74,6 +74,7 @@ declare module 'highlight.js' { | |||
HASH_COMMENT_MODE: Mode | |||
NUMBER_MODE: Mode | |||
C_NUMBER_MODE: Mode | |||
EXTENDED_NUMBER_MODE: Mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the code passes tests without that line, so I guess so. I was just following what seemed to be an established pattern, thinking everything like that should be declared there.
src/languages/lib/ecmascript.js
Outdated
|
||
export const EXTENDED_NUMBER_MODE = { | ||
scope: 'number', | ||
begin: EXTENDED_NUMBER_RE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
begin: EXTENDED_NUMBER_RE, | |
match: EXTENDED_NUMBER_RE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is done as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, almost there!
Build Size ReportChanges to minified artifacts in 6 files changedTotal change +199 B View Changes
|
Addition of JSON5 handling to json.js.
Resolves #4260
Changes
In addition to adding 'json5' to json.js, I noticed a suspicious looking regex in javascript.js. I can make this fix and all tests still pass, so I think including this change is a good idea. The suspicious regex, at line 460, is this:
It seems highly doubtful this was meant to be the range from uppercase A to lowercase z, which isn't just letters, but the punctuation " [ \ ] _ ` " as well.
Checklist
CHANGES.md
SUPPORTED_LANGUAGES.md