diff --git a/lib/rouge/lexers/javascript.rb b/lib/rouge/lexers/javascript.rb index ce11ad728f..45a11844a2 100644 --- a/lib/rouge/lexers/javascript.rb +++ b/lib/rouge/lexers/javascript.rb @@ -190,8 +190,8 @@ def self.id_regex rule %r/function(?=(\(.*\)))/, Keyword::Declaration # For anonymous functions - rule %r/(#{id})[ \t]*(?=(\(.*\)))/m do |m| - if self.class.keywords.include? m[1] + rule %r/(#?#{id})[ \t]*(?=(\(.*\)))/m do |m| + if self.class.keywords.include?(m[1]) # "if" in "if (...)" or "switch" in "switch (...)" are recognized as keywords. token Keyword else @@ -201,7 +201,7 @@ def self.id_regex rule %r/[{}]/, Punctuation, :statement - rule id do |m| + rule %r/#?#{id}/ do |m| if self.class.keywords.include? m[0] token Keyword push :expr_start diff --git a/spec/visual/samples/javascript b/spec/visual/samples/javascript index e268af82bc..ae77e18311 100644 --- a/spec/visual/samples/javascript +++ b/spec/visual/samples/javascript @@ -297,3 +297,21 @@ ret += "\n"; let baz; baz ??= 'default'; + +class ClassWithPrivate { + #privateField; + #privateFieldWithInitializer = 42; + + #privateMethod(obj) { + if (#privateField in obj) return obj.#privateField; + + return "invalid obj" + } + + static #privateStaticField; + static #privateStaticFieldWithInitializer = 42; + + static #privateStaticMethod() { + // … + } +}