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

fix(javascript) prevent get/set variables conflicting with keywords #2440

Merged
merged 3 commits into from Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -17,6 +17,7 @@ Core Changes:

Language Improvements:

- fix(javascript) prevent get/set variables conflicting with keywords (#2440) [Josh Goebel][]
- bug(clojure) Now highlights `defn-` properly (#2438) [Josh Goebel][]
- enh(clojure) Add support for global definitions name (#2347) [Alexandre Grison][]
- enh(fortran) Support Fortran 77 style comments (#2416) [Josh Goebel][]
Expand Down
28 changes: 20 additions & 8 deletions src/languages/javascript.js
Expand Up @@ -93,6 +93,13 @@ export default function(hljs) {
hljs.C_BLOCK_COMMENT_MODE,
hljs.C_LINE_COMMENT_MODE
]);
var PARAMS = {
className: 'params',
begin: /\(/, end: /\)/,
excludeBegin: true,
excludeEnd: true,
contains: PARAMS_CONTAINS
};

return {
name: 'JavaScript',
Expand Down Expand Up @@ -220,13 +227,7 @@ export default function(hljs) {
beginKeywords: 'function', end: /\{/, excludeEnd: true,
contains: [
hljs.inherit(hljs.TITLE_MODE, {begin: IDENT_RE}),
{
className: 'params',
begin: /\(/, end: /\)/,
excludeBegin: true,
excludeEnd: true,
contains: PARAMS_CONTAINS
}
PARAMS
],
illegal: /\[|%/
},
Expand All @@ -245,7 +246,18 @@ export default function(hljs) {
]
},
{
beginKeywords: 'constructor get set', end: /\{/, excludeEnd: true
beginKeywords: 'constructor', end: /\{/, excludeEnd: true
},
{
begin:'(get|set)\\s*(?=' + IDENT_RE+ '\\()',
end: /{/,
keywords: "get set",
contains: [
hljs.inherit(hljs.TITLE_MODE, {begin: IDENT_RE}),
{ begin: /\(\)/ }, // eat to avoid empty params
PARAMS
]

}
],
illegal: /#(?!!)/
Expand Down
9 changes: 9 additions & 0 deletions test/markup/javascript/keyword_as_identifier.expect.txt
@@ -0,0 +1,9 @@
<span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> e <span class="hljs-keyword">of</span> set)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span> </span>{
<span class="hljs-keyword">set</span> <span class="hljs-title">value</span>(<span class="hljs-params">x</span>) {
}
<span class="hljs-keyword">get</span> <span class="hljs-title">valid</span>() {
}
}
9 changes: 9 additions & 0 deletions test/markup/javascript/keyword_as_identifier.txt
@@ -0,0 +1,9 @@
const set = new Set([1, 2]);
for (const e of set)

class A {
set value(x) {
}
get valid() {
}
}