Skip to content

Commit

Permalink
code: fix JavaScript rules (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
VoltrexKeyva authored Aug 2, 2021
1 parent da5620a commit 7c7b410
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ object JavaScript {
)

class FunctionNode<RC>(
pre: String, signature: String?, params: String, scope: String,
signature: String?, params: String, scope: String,
codeStyleProviders: CodeStyleProviders<RC>
) : Node.Parent<RC>(
StyleNode.TextStyledNode(pre, codeStyleProviders.keywordStyleProvider),
signature?.let { StyleNode.TextStyledNode(signature, codeStyleProviders.identifierStyleProvider) },
StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider),
StyleNode.TextStyledNode(scope, codeStyleProviders.defaultStyleProvider)
Expand All @@ -65,16 +64,47 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_FUNC =
"""^(function\*?|static|get|set|async)(\s+[a-zA-Z_$](?:[a-zA-Z0-9_$])*?)?( *?\(.*?\))(\s*\{)""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern()
"""^([a-zA-Z_$][a-zA-Z0-9_$]*)?(\s*\(.*\))(\s*\{)""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern()

fun <RC, S> createFunctionRule(codeStyleProviders: CodeStyleProviders<RC>) =
object : Rule<RC, Node<RC>, S>(PATTERN_JAVASCRIPT_FUNC) {
override fun parse(matcher: Matcher, parser: Parser<RC, in Node<RC>, S>, state: S): ParseSpec<RC, S> {
val definition = matcher.group(1)
val signature = matcher.group(2)
val params = matcher.group(3)
val scope = matcher.group(4)
return ParseSpec.createTerminal(FunctionNode(definition!!, signature, params!!, scope!!, codeStyleProviders), state)
val signature = matcher.group(1)
val params = matcher.group(2)
val scope = matcher.group(3)
return ParseSpec.createTerminal(FunctionNode(signature, params!!, scope!!, codeStyleProviders), state)
}
}
}
}

class ArrowFunctionNode<RC>(
params: String, arrow: String,
codeStyleProviders: CodeStyleProviders<RC>
) : Node.Parent<RC>(
StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider),
StyleNode.TextStyledNode(arrow, codeStyleProviders.defaultStyleProvider)
) {
companion object {
/**
* Matches againt a JavaScript arrow function declaration.
*
* ```
* file => {}
* file => !file
* (file) => 1
* () => {}
* ```
*/
private val PATTERN_JAVASCRIPT_ARROW_FUNC =
"""^([a-zA-Z_$][a-zA-Z0-9_$]*|\(.*\))(\s*=>)""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern()

fun <RC, S> createArrowFunctionRule(codeStyleProviders: CodeStyleProviders<RC>) =
object : Rule<RC, Node<RC>, S>(PATTERN_JAVASCRIPT_ARROW_FUNC) {
override fun parse(matcher: Matcher, parser: Parser<RC, in Node<RC>, S>, state: S): ParseSpec<RC, S> {
val params = matcher.group(1)
val arrow = matcher.group(2)
return ParseSpec.createTerminal(ArrowFunctionNode(params!!, arrow!!, codeStyleProviders), state)
}
}
}
Expand All @@ -98,7 +128,7 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_FIELD =
Pattern.compile("""^(var|let|const)(\s+[a-zA-Z_$](?:[a-zA-Z0-9_$])*)""", Pattern.DOTALL)
Pattern.compile("""^(var|let|const)(\s+[a-zA-Z_$][a-zA-Z0-9_$]*)""")

fun <RC, S> createFieldRule(
codeStyleProviders: CodeStyleProviders<RC>
Expand Down Expand Up @@ -132,7 +162,7 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_OBJECT_PROPERTY =
Pattern.compile("""^([{\[,])(\s*[a-zA-Z0-9_$]*)(\s*:)""", Pattern.DOTALL)
Pattern.compile("""^([\{\[\,])(\s*[a-zA-Z0-9_$]+)(\s*:)""")

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

/**
* Matches against a JavaScript generic.
Expand All @@ -168,7 +198,7 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_GENERIC =
Pattern.compile("""^<(.*)>""")
Pattern.compile("""^<.*(?<!\\)>""")

/**
* Matches against a JavaScript comment.
Expand Down Expand Up @@ -204,5 +234,6 @@ object JavaScript {
PATTERN_JAVASCRIPT_REGEX.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
FieldNode.createFieldRule(codeStyleProviders),
FunctionNode.createFunctionRule(codeStyleProviders),
ArrowFunctionNode.createArrowFunctionRule(codeStyleProviders),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,33 @@ class JavaScriptRulesTest {
""".trimIndent(), TestState())

ast.assertNodeContents<JavaScript.FunctionNode<*>>(
"function test(T) {",
"function () {",
"function* generator() {",
"static test() {",
"async fetch() {",
"get tokens() {",
"set constants() {")
"test(T) {",
"() {",
"generator() {",
"test() {",
"fetch() {",
"tokens() {",
"constants() {")
}

@Test
fun arrowFunctions() {
val ast = parser.parse("""
```js
test => {}
test => h
(test) => {}
(test) => h
(...args) => {}
```
""".trimIndent(), TestState())

ast.assertNodeContents<JavaScript.ArrowFunctionNode<*>>(
"test =>",
"test =>",
"(test) =>",
"(test) =>",
"(...args) =>")
}

@Test
Expand Down

0 comments on commit 7c7b410

Please sign in to comment.