@@ -42,10 +42,9 @@ object JavaScript {
42
42
)
43
43
44
44
class FunctionNode <RC >(
45
- pre : String , signature : String? , params : String , scope : String ,
45
+ signature : String? , params : String , scope : String ,
46
46
codeStyleProviders : CodeStyleProviders <RC >
47
47
) : Node.Parent<RC>(
48
- StyleNode .TextStyledNode (pre, codeStyleProviders.keywordStyleProvider),
49
48
signature?.let { StyleNode .TextStyledNode (signature, codeStyleProviders.identifierStyleProvider) },
50
49
StyleNode .TextStyledNode (params, codeStyleProviders.paramsStyleProvider),
51
50
StyleNode .TextStyledNode (scope, codeStyleProviders.defaultStyleProvider)
@@ -65,16 +64,47 @@ object JavaScript {
65
64
* ```
66
65
*/
67
66
private val PATTERN_JAVASCRIPT_FUNC =
68
- """ ^(function\*?|static|get|set|async)(\s+ [a-zA-Z_$](?: [a-zA-Z0-9_$])*? )?( *? \(.*? \))(\s*\{)""" .toRegex(RegexOption .DOT_MATCHES_ALL ).toPattern()
67
+ """ ^([a-zA-Z_$][a-zA-Z0-9_$]* )?(\s* \(.*\))(\s*\{)""" .toRegex(RegexOption .DOT_MATCHES_ALL ).toPattern()
69
68
70
69
fun <RC , S > createFunctionRule (codeStyleProviders : CodeStyleProviders <RC >) =
71
70
object : Rule <RC , Node <RC >, S > (PATTERN_JAVASCRIPT_FUNC ) {
72
71
override fun parse (matcher : Matcher , parser : Parser <RC , in Node <RC >, S >, state : S ): ParseSpec <RC , S > {
73
- val definition = matcher.group(1 )
74
- val signature = matcher.group(2 )
75
- val params = matcher.group(3 )
76
- val scope = matcher.group(4 )
77
- return ParseSpec .createTerminal(FunctionNode (definition!! , signature, params!! , scope!! , codeStyleProviders), state)
72
+ val signature = matcher.group(1 )
73
+ val params = matcher.group(2 )
74
+ val scope = matcher.group(3 )
75
+ return ParseSpec .createTerminal(FunctionNode (signature, params!! , scope!! , codeStyleProviders), state)
76
+ }
77
+ }
78
+ }
79
+ }
80
+
81
+ class ArrowFunctionNode <RC >(
82
+ params : String , arrow : String ,
83
+ codeStyleProviders : CodeStyleProviders <RC >
84
+ ) : Node.Parent<RC>(
85
+ StyleNode .TextStyledNode (params, codeStyleProviders.paramsStyleProvider),
86
+ StyleNode .TextStyledNode (arrow, codeStyleProviders.defaultStyleProvider)
87
+ ) {
88
+ companion object {
89
+ /* *
90
+ * Matches againt a JavaScript arrow function declaration.
91
+ *
92
+ * ```
93
+ * file => {}
94
+ * file => !file
95
+ * (file) => 1
96
+ * () => {}
97
+ * ```
98
+ */
99
+ private val PATTERN_JAVASCRIPT_ARROW_FUNC =
100
+ """ ^([a-zA-Z_$][a-zA-Z0-9_$]*|\(.*\))(\s*=>)""" .toRegex(RegexOption .DOT_MATCHES_ALL ).toPattern()
101
+
102
+ fun <RC , S > createArrowFunctionRule (codeStyleProviders : CodeStyleProviders <RC >) =
103
+ object : Rule <RC , Node <RC >, S > (PATTERN_JAVASCRIPT_ARROW_FUNC ) {
104
+ override fun parse (matcher : Matcher , parser : Parser <RC , in Node <RC >, S >, state : S ): ParseSpec <RC , S > {
105
+ val params = matcher.group(1 )
106
+ val arrow = matcher.group(2 )
107
+ return ParseSpec .createTerminal(ArrowFunctionNode (params!! , arrow!! , codeStyleProviders), state)
78
108
}
79
109
}
80
110
}
@@ -98,7 +128,7 @@ object JavaScript {
98
128
* ```
99
129
*/
100
130
private val PATTERN_JAVASCRIPT_FIELD =
101
- Pattern .compile(""" ^(var|let|const)(\s+[a-zA-Z_$](?: [a-zA-Z0-9_$]) *)""" , Pattern . DOTALL )
131
+ Pattern .compile(""" ^(var|let|const)(\s+[a-zA-Z_$][a-zA-Z0-9_$]*)""" )
102
132
103
133
fun <RC , S > createFieldRule (
104
134
codeStyleProviders : CodeStyleProviders <RC >
@@ -132,7 +162,7 @@ object JavaScript {
132
162
* ```
133
163
*/
134
164
private val PATTERN_JAVASCRIPT_OBJECT_PROPERTY =
135
- Pattern .compile(""" ^([{\[,])(\s*[a-zA-Z0-9_$]* )(\s*:)""" , Pattern . DOTALL )
165
+ Pattern .compile(""" ^([\ {\[\ ,])(\s*[a-zA-Z0-9_$]+ )(\s*:)""" )
136
166
137
167
fun <RC , S > createObjectPropertyRule (
138
168
codeStyleProviders : CodeStyleProviders <RC >
@@ -158,7 +188,7 @@ object JavaScript {
158
188
* ```
159
189
*/
160
190
private val PATTERN_JAVASCRIPT_REGEX =
161
- Pattern .compile(""" ^/.*?/(?:\w*)? """ )
191
+ Pattern .compile(""" ^/.+(?<!\\)/[dgimsuy]* """ )
162
192
163
193
/* *
164
194
* Matches against a JavaScript generic.
@@ -168,7 +198,7 @@ object JavaScript {
168
198
* ```
169
199
*/
170
200
private val PATTERN_JAVASCRIPT_GENERIC =
171
- Pattern .compile(""" ^<(.* )>""" )
201
+ Pattern .compile(""" ^<.*(?<!\\ )>""" )
172
202
173
203
/* *
174
204
* Matches against a JavaScript comment.
@@ -204,5 +234,6 @@ object JavaScript {
204
234
PATTERN_JAVASCRIPT_REGEX .toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
205
235
FieldNode .createFieldRule(codeStyleProviders),
206
236
FunctionNode .createFunctionRule(codeStyleProviders),
237
+ ArrowFunctionNode .createArrowFunctionRule(codeStyleProviders),
207
238
)
208
239
}
0 commit comments