Skip to content

Commit 0806dac

Browse files
authored
Recognize javascript functions & classes (#1920)
* Recognize javascript functions With this change, javascript functions will be recognized both when they are defined and when they are called. * Fix failing test With the update to the javascript lexer, functions are now recognized. This updates the test case to reflect this. * Add function name token to regex to avoid adding state * Support Unicode character function names * Correctly recognize an anonymous function * Recognize javascript classes In order to recognize classes we have to tweak the `id` regex because we want to try to differentiate between functions and classes. To recognize classes, the first letter that appears in the name must not be a lowercase character. * Added javascript visual samples Added for the new function and class (with unicode support) recognition. * Add support for javascript class inheritance
1 parent be0040e commit 0806dac

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/rouge/lexers/javascript.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ def self.id_regex
177177
push :expr_start
178178
end
179179

180+
rule %r/(class)((?:\s|\\\s)+)/ do
181+
groups Keyword::Declaration, Text
182+
push :classname
183+
end
184+
185+
rule %r/([\p{Nl}$_]*\p{Lu}[\p{Word}]*)[ \t]*(?=(\(.*\)))/m, Name::Class
186+
187+
rule %r/(function)((?:\s|\\\s)+)(#{id})/ do
188+
groups Keyword::Declaration, Text, Name::Function
189+
end
190+
191+
rule %r/function(?=(\(.*\)))/, Keyword::Declaration # For anonymous functions
192+
193+
rule %r/(#{id})[ \t]*(?=(\(.*\)))/m, Name::Function
194+
180195
rule %r/[{}]/, Punctuation, :statement
181196

182197
rule id do |m|
@@ -220,6 +235,14 @@ def self.id_regex
220235
rule %r/'/, Str::Delimiter, :pop!
221236
end
222237

238+
state :classname do
239+
rule %r/(#{id})((?:\s|\\\s)+)(extends)((?:\s|\\\s)+)/ do
240+
groups Name::Class, Text, Keyword::Declaration, Text
241+
end
242+
243+
rule id, Name::Class, :pop!
244+
end
245+
223246
# braced parts that aren't object literals
224247
state :statement do
225248
rule %r/case\b/ do

spec/formatters/html_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class InlineTheme < Rouge::CSSTheme
3939

4040
it 'should format token stream' do
4141
assert { output == '<span class="nt">&lt;meta</span> <span class="na">name=</span><span class="s">"description"</span> <span class="na">content=</span><span class="s">"foo"</span><span class="nt">&gt;</span>
42-
<span class="nt">&lt;script&gt;</span><span class="nx">alert</span><span class="p">(</span><span class="dl">"</span><span class="s2">bar</span><span class="dl">"</span><span class="p">)</span><span class="nt">&lt;/script&gt;</span>' }
42+
<span class="nt">&lt;script&gt;</span><span class="nf">alert</span><span class="p">(</span><span class="dl">"</span><span class="s2">bar</span><span class="dl">"</span><span class="p">)</span><span class="nt">&lt;/script&gt;</span>' }
4343
end
4444
end
4545

spec/visual/samples/javascript

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ Blag.ReadMore = (function($) {
148148
$(document).ready(init);
149149
})(jQuery);
150150

151+
underscoreFunc = _underscoreFunc()
152+
underscoreClass = _UnderscoreClass()
153+
151154
// evil regexes, from pygments
152155

153156
/regexp/.test(foo) || x = [/regexp/,/regexp/, /regexp/, // comment
@@ -283,6 +286,8 @@ class Œuvre {
283286
}
284287
}
285288

289+
unicodeClass = Œuvre()
290+
286291
// Nullish coalescing operator (??)
287292
let ret = "";
288293
let foo, bar;

0 commit comments

Comments
 (0)