Skip to content

Commit

Permalink
JavaScript: Add support for constants. Fix #1348
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Mar 26, 2018
1 parent 0414a2d commit 9084481
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Prism.languages.insertBefore('javascript', 'keyword', {
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
}
},
'constant': /\b[A-Z][A-Z\d_]*\b/

This comment has been minimized.

Copy link
@paladox

paladox Mar 31, 2018

Contributor

constants can also be numbers or lowercase.

This comment has been minimized.

Copy link
@Golmote

Golmote Apr 4, 2018

Author Contributor

As stated in my comment on your issue, this commit adds support for the commonly accepted convention of uppercased constants.
Also, as I told you, other uses of constants would be super tricky to highlight using only regular expressions, because Prism is not a parser, it has no knowledge of the semantics of your code or its behaviour. It's just visual improvement for code display.

});

Prism.languages.insertBefore('javascript', 'string', {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

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

2 changes: 1 addition & 1 deletion components/prism-n4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Prism.languages.n4js = Prism.languages.extend('javascript', {
'keyword': /\b(?:any|Array|boolean|break|case|catch|class|const|constructor|continue|debugger|declare|default|delete|do|else|enum|export|extends|false|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|module|new|null|number|package|private|protected|public|return|set|static|string|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)\b/
});

Prism.languages.insertBefore('n4js', 'function', {
Prism.languages.insertBefore('n4js', 'constant', {
// Annotations in N4JS spec: https://numberfour.github.io/n4js/spec/N4JSSpec.html#_annotations
'annotation': {
pattern: /@+\w+/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-n4js.min.js

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

3 changes: 2 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ Prism.languages.insertBefore('javascript', 'keyword', {
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
}
},
'constant': /\b[A-Z][A-Z\d_]*\b/
});

Prism.languages.insertBefore('javascript', 'string', {
Expand Down
21 changes: 21 additions & 0 deletions tests/languages/javascript/constant_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var FOO;
const FOO_BAR;
const BAZ42;

----------------------------------------------------

[
["keyword", "var"],
["constant", "FOO"],
["punctuation", ";"],
["keyword", "const"],
["constant", "FOO_BAR"],
["punctuation", ";"],
["keyword", "const"],
["constant", "BAZ42"],
["punctuation", ";"]
]

----------------------------------------------------

Checks for constants.
2 changes: 1 addition & 1 deletion tests/languages/markup+actionscript/xml_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var employees:XML =
["keyword", "var"],
" employees",
["punctuation", ":"],
"XML ",
["constant", "XML"],
["operator", "="],
["xml", [
["tag", [
Expand Down

0 comments on commit 9084481

Please sign in to comment.