Skip to content

Commit

Permalink
fix(javascript) prevent get/set variables conflicting with keywords (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 29, 2020
1 parent 381a5c6 commit d93c13e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
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() {
}
}

0 comments on commit d93c13e

Please sign in to comment.