8000 Add support for JSON5 as extension of json.js. by kshetline · Pull Request #4259 · highlightjs/highlight.js · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 6 commits into from
May 30, 2025

Conversation

kshetline
Copy link
Contributor
@kshetline kshetline commented May 12, 2025

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:

    illegal: /#(?![$_A-z])/,

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

  • Added markup tests
  • Updated the changelog at CHANGES.md
  • Updated aliases listed for JSON at SUPPORTED_LANGUAGES.md

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
Copy link
Member

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.

8000 Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

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

Dead code?

Copy link
Contributor Author

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.


export const EXTENDED_NUMBER_MODE = {
scope: 'number',
begin: EXTENDED_NUMBER_RE,
Copy link
Member
< 8000 div hidden="hidden" data-view-component="true" class="js-comment-show-on-error flash flash-error flash-full">

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.

Suggested change
begin: EXTENDED_NUMBER_RE,
match: EXTENDED_NUMBER_RE,

Copy link
Contributor Author

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.

Copy link
Member
@joshgoebel joshgoebel left a 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!

@kshetline kshetline requested a review from joshgoebel May 30, 2025 02:31
Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

6 files changed

Total change +199 B

View Changes
file base pr diff
es/languages/json.min.js 382 B 484 B +102 B
es/languages/typescript.min.js 3.21 KB 3.21 KB -1 B
highlight.min.js 8.23 KB 8.23 KB +1 B
languages/javascript.min.js 2.75 KB 2.75 KB -1 B
languages/json.min.js 388 B 488 B +100 B
languages/typescript.min.js 3.22 KB 3.22 KB -2 B

@joshgoebel joshgoebel merged commit 3ec4894 into highlightjs:main May 30, 2025
19 checks passed
@kshetline kshetline deleted the main-json5 branch May 30, 2025 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] Add support for JSON5 as extension of current JSON support
2 participants
0