Skip to content

Commit

Permalink
build(ts): fix performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 17, 2022
1 parent f5abbc9 commit adcc52a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
30 changes: 25 additions & 5 deletions chapi-ast-typescript/src/main/antlr/TypeScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ breakStatement

returnStatement
: Return ({this.notLineTerminator()}? expressionSequence)? eos
| {isJsx()}? Return '(' htmlElements ')' eos
| Return htmlAssign eos
;


Expand Down Expand Up @@ -866,7 +866,7 @@ singleExpression
| objectLiteral # ObjectLiteralExpression
| templateStringLiteral # TemplateStringExpression
| singleExpression As asExpression # CastAsExpression
| {isJsx()}? htmlElements # HtmlElementExpression
| singleExpression '=' htmlAssign # HtmlAssignmentExpression
;

yieldStatement
Expand All @@ -891,7 +891,8 @@ arrowFunctionParameters
;

arrowFunctionBody
: '{' statementList? '}'
: htmlAssign
| '{' statementList? '}'
| singleExpression
;

Expand Down Expand Up @@ -1083,8 +1084,19 @@ eos
| {this.closeBrace()}?
;

htmlElements
: htmlElement+
//htmlElements
// : htmlElement+
// ;

// case 1: const element = <h1>{title}</h1>;
// case 2:
// const element = (
// <h1 className="greeting">
// Hello, world!
// </h1>
//);
htmlAssign
:'('? '<' htmlTagCenter '>' ')'?
;

htmlElement
Expand All @@ -1096,6 +1108,14 @@ htmlElement
| '<' htmlTagName htmlAttribute* '>'
;

htmlTagCenter
: htmlTagStartName htmlAttribute* '>' htmlContent '<''/' htmlTagClosingName
| '>' htmlContent '<''/'
| htmlTagName htmlAttribute* htmlContent '/'
| htmlTagName htmlAttribute* '/'
| htmlTagName htmlAttribute*
;

htmlContent
: htmlChardata? ((htmlElement) htmlChardata?)*
| htmlChardata? ((htmlElement | objectExpressionSequence) htmlChardata?)*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chapi.domain.core.*
import chapi.infra.Stack
import org.antlr.v4.runtime.tree.TerminalNodeImpl

class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
class TypeScriptFullIdentListener(val node: TSIdentify) : TypeScriptAstListener() {
private var hasHtmlElement: Boolean = false
private var filePath: String = node.filePath

Expand Down Expand Up @@ -596,13 +596,12 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
private fun parseStatement(context: TypeScriptParser.StatementContext) {
when (val child = context.getChild(0)) {
is TypeScriptParser.ReturnStatementContext -> {
hasHtmlElement = false

if (child.expressionSequence() != null) {
child.expressionSequence().singleExpression().forEach(::parseSingleExpression)
}

if (hasHtmlElement) {
if (node.isJsxFile()) {
currentFunc.IsReturnHtml = true
}
}
Expand Down Expand Up @@ -731,10 +730,10 @@ class TypeScriptFullIdentListener(node: TSIdentify) : TypeScriptAstListener() {
CodeProperty(TypeValue = subSingle.text, TypeType = "object", ObjectValue = objectLiteral)
}

is TypeScriptParser.HtmlElementExpressionContext -> {
hasHtmlElement = true
// is TypeScriptParser.HtmlElementExpressionContext -> {
// hasHtmlElement = true
// println("todo -> HtmlElementExpressionContext: $simpleName, text: ${subSingle.text}")
}
// }

is TypeScriptParser.ArgumentsExpressionContext -> {
parseArguments(subSingle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const BadSmellThreshold = () => {
val innerFunctions = defaultStruct.Functions[0].InnerFunctions
assertEquals(3, innerFunctions.size)

assertEquals(2, innerFunctions[0].FunctionCalls.size)
assertEquals(1, innerFunctions[0].FunctionCalls.size)
assertEquals(1, innerFunctions[1].FunctionCalls.size)
assertEquals(1, innerFunctions[2].FunctionCalls.size)
}
Expand Down

0 comments on commit adcc52a

Please sign in to comment.