From 05b4bd7de812b29b16f9a080a0c5f083d2e52264 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 28 Feb 2020 22:08:57 -0500 Subject: [PATCH] more specific rules for get and set --- src/languages/javascript.js | 28 +++++++++++++------ .../keyword_as_identifier.expect.txt | 9 ++++++ .../javascript/keyword_as_identifier.txt | 9 ++++++ 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 test/markup/javascript/keyword_as_identifier.expect.txt create mode 100644 test/markup/javascript/keyword_as_identifier.txt diff --git a/src/languages/javascript.js b/src/languages/javascript.js index 7ff30465b6..9636f508da 100644 --- a/src/languages/javascript.js +++ b/src/languages/javascript.js @@ -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', @@ -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: /\[|%/ }, @@ -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: /#(?!!)/ diff --git a/test/markup/javascript/keyword_as_identifier.expect.txt b/test/markup/javascript/keyword_as_identifier.expect.txt new file mode 100644 index 0000000000..dab606e707 --- /dev/null +++ b/test/markup/javascript/keyword_as_identifier.expect.txt @@ -0,0 +1,9 @@ +const set = new Set([1, 2]); +for (const e of set) + +class A { + set value(x) { + } + get valid() { + } +} diff --git a/test/markup/javascript/keyword_as_identifier.txt b/test/markup/javascript/keyword_as_identifier.txt new file mode 100644 index 0000000000..e3fcfcb236 --- /dev/null +++ b/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() { + } +}