Skip to content

Commit

Permalink
more specific rules for get and set
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 29, 2020
1 parent ec0ec9b commit d071970
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
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
11 changes: 11 additions & 0 deletions test/markup/javascript/keyword_as_identifier.expect.txt
@@ -0,0 +1,11 @@
<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 d071970

Please sign in to comment.