Skip to content

Commit 7c7b410

Browse files
authored
code: fix JavaScript rules (#34)
1 parent da5620a commit 7c7b410

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

simpleast-core/src/main/java/com/discord/simpleast/code/JavaScript.kt

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ object JavaScript {
4242
)
4343

4444
class FunctionNode<RC>(
45-
pre: String, signature: String?, params: String, scope: String,
45+
signature: String?, params: String, scope: String,
4646
codeStyleProviders: CodeStyleProviders<RC>
4747
) : Node.Parent<RC>(
48-
StyleNode.TextStyledNode(pre, codeStyleProviders.keywordStyleProvider),
4948
signature?.let { StyleNode.TextStyledNode(signature, codeStyleProviders.identifierStyleProvider) },
5049
StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider),
5150
StyleNode.TextStyledNode(scope, codeStyleProviders.defaultStyleProvider)
@@ -65,16 +64,47 @@ object JavaScript {
6564
* ```
6665
*/
6766
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()
6968

7069
fun <RC, S> createFunctionRule(codeStyleProviders: CodeStyleProviders<RC>) =
7170
object : Rule<RC, Node<RC>, S>(PATTERN_JAVASCRIPT_FUNC) {
7271
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)
78108
}
79109
}
80110
}
@@ -98,7 +128,7 @@ object JavaScript {
98128
* ```
99129
*/
100130
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_$]*)""")
102132

103133
fun <RC, S> createFieldRule(
104134
codeStyleProviders: CodeStyleProviders<RC>
@@ -132,7 +162,7 @@ object JavaScript {
132162
* ```
133163
*/
134164
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*:)""")
136166

137167
fun <RC, S> createObjectPropertyRule(
138168
codeStyleProviders: CodeStyleProviders<RC>
@@ -158,7 +188,7 @@ object JavaScript {
158188
* ```
159189
*/
160190
private val PATTERN_JAVASCRIPT_REGEX =
161-
Pattern.compile("""^/.*?/(?:\w*)?""")
191+
Pattern.compile("""^/.+(?<!\\)/[dgimsuy]*""")
162192

163193
/**
164194
* Matches against a JavaScript generic.
@@ -168,7 +198,7 @@ object JavaScript {
168198
* ```
169199
*/
170200
private val PATTERN_JAVASCRIPT_GENERIC =
171-
Pattern.compile("""^<(.*)>""")
201+
Pattern.compile("""^<.*(?<!\\)>""")
172202

173203
/**
174204
* Matches against a JavaScript comment.
@@ -204,5 +234,6 @@ object JavaScript {
204234
PATTERN_JAVASCRIPT_REGEX.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
205235
FieldNode.createFieldRule(codeStyleProviders),
206236
FunctionNode.createFunctionRule(codeStyleProviders),
237+
ArrowFunctionNode.createArrowFunctionRule(codeStyleProviders),
207238
)
208239
}

simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRuleTest.kt renamed to simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRulesTest.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,33 @@ class JavaScriptRulesTest {
107107
""".trimIndent(), TestState())
108108

109109
ast.assertNodeContents<JavaScript.FunctionNode<*>>(
110-
"function test(T) {",
111-
"function () {",
112-
"function* generator() {",
113-
"static test() {",
114-
"async fetch() {",
115-
"get tokens() {",
116-
"set constants() {")
110+
"test(T) {",
111+
"() {",
112+
"generator() {",
113+
"test() {",
114+
"fetch() {",
115+
"tokens() {",
116+
"constants() {")
117+
}
118+
119+
@Test
120+
fun arrowFunctions() {
121+
val ast = parser.parse("""
122+
```js
123+
test => {}
124+
test => h
125+
(test) => {}
126+
(test) => h
127+
(...args) => {}
128+
```
129+
""".trimIndent(), TestState())
130+
131+
ast.assertNodeContents<JavaScript.ArrowFunctionNode<*>>(
132+
"test =>",
133+
"test =>",
134+
"(test) =>",
135+
"(test) =>",
136+
"(...args) =>")
117137
}
118138

119139
@Test

0 commit comments

Comments
 (0)