Skip to content

Commit

Permalink
code: fix JavaScript rules (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
VoltrexKeyva authored Jul 28, 2021
1 parent 2d76562 commit 1c84600
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.regex.Pattern
object JavaScript {

val KEYWORDS: Array<String> = arrayOf(
"import|export|default|package",
"import|from|export|default|package",
"class|enum",
"function|super|extends|implements|arguments",
"var|let|const|static|get|set|new",
Expand Down Expand Up @@ -42,12 +42,13 @@ object JavaScript {
)

class FunctionNode<RC>(
pre: String, signature: String?, params: String,
pre: 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)
) {
companion object {
/**
Expand All @@ -64,15 +65,16 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_FUNC =
"""^(function\*?|static|get|set|async)(\s+[a-zA-Z_$](?:[a-zA-Z0-9_$])*?)?( *?\(.*?\))""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern()
"""^(function\*?|static|get|set|async)(\s+[a-zA-Z_$](?:[a-zA-Z0-9_$])*?)?( *?\(.*?\))(\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)
return ParseSpec.createTerminal(FunctionNode(definition!!, signature, params!!, codeStyleProviders), state)
val scope = matcher.group(4)
return ParseSpec.createTerminal(FunctionNode(definition!!, signature, params!!, scope!!, codeStyleProviders), state)
}
}
}
Expand Down Expand Up @@ -130,7 +132,7 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_OBJECT_PROPERTY =
Pattern.compile("""^([{\[,])(\s*[a-zA-Z0-9_$]*)(:)""", Pattern.DOTALL)
Pattern.compile("""^([{\[,])(\s*[a-zA-Z0-9_$]*)(\s*:)""", Pattern.DOTALL)

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

/**
* Matches against a JavaScript generic.
Expand Down Expand Up @@ -189,7 +191,7 @@ object JavaScript {
* ```
*/
private val PATTERN_JAVASCRIPT_STRINGS =
Pattern.compile("""^('[\s\S]*?(?<!\\)'|"[\s\S]*?(?<!\\)"|`[\s\S]*?(?<!\\)`)(?=\W|\s|$)""")
Pattern.compile("""^('.*?(?<!\\)'|".*?(?<!\\)"|`[\s\S]*?(?<!\\)`)(?=\W|\s|$)""")

internal fun <RC, S> createCodeRules(
codeStyleProviders: CodeStyleProviders<RC>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ class JavaScriptRulesTest {
""".trimIndent(), TestState())

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

@Test
Expand Down

0 comments on commit 1c84600

Please sign in to comment.