Skip to content

Commit

Permalink
[JavaScript] Property assignments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ris58h committed Jul 18, 2023
1 parent 8021a21 commit 4ece215
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ package ris58h.webcalm.javascript.psi
import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptAssignmentExpression(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptExpression
class JavaScriptAssignmentExpression(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptExpression {
val left: JavaScriptExpression?
get() = firstChild as? JavaScriptExpression

val right: JavaScriptExpression?
get() = if (firstChild == lastChild) null else lastChild as? JavaScriptExpression
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptComputedPropertyExpressionAssignment(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptFunctionProperty(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,36 @@ class JavaScriptIdentifierReference(element: JavaScriptIdentifier, rangeInElemen
assignable.doWhen(
{ identifier -> callback(identifier) },
{ array ->
array.elements.filterIsInstance<JavaScriptIdentifierExpression>().forEach {
callback(it.identifier)
array.elements.forEach {
processDeclarationsInExpression(it, callback)
}
},
{ obj ->
obj.propertyAssignments.forEach {
val identifier = it.propertyShorthand?.identifier
if (identifier != null) callback(identifier)
when (it) {
is JavaScriptPropertyShorthand -> {
val expression = it.expression
if (expression != null) processDeclarationsInExpression(expression, callback)
}
else -> {
// TODO: other property assignments
}
}
}
}
)
}

private fun processDeclarationsInExpression(expression: JavaScriptExpression, callback: (PsiNamedElement) -> Unit) {
when (expression) {
is JavaScriptAssignmentExpression -> {
val left = expression.left
if (left != null) processDeclarationsInExpression(left, callback)
}
is JavaScriptIdentifierExpression -> callback(expression.identifier)
else -> {
// TODO: other expressions
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement

class JavaScriptPropertyAssignment(node: ASTNode) : ASTWrapperPsiElement(node) {
//TODO: support other types of assignment
val propertyShorthand: JavaScriptIdentifierExpression?
get() = this.findChildByClass(JavaScriptIdentifierExpression::class.java)
}
sealed interface JavaScriptPropertyAssignment : PsiElement
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptPropertyExpressionAssignment(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptPropertyGetter(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptPropertySetter(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ris58h.webcalm.javascript.psi

import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode

class JavaScriptPropertyShorthand(node: ASTNode) : ASTWrapperPsiElement(node), JavaScriptPropertyAssignment {
val expression: JavaScriptExpression?
get() = this.findChildByClass(JavaScriptExpression::class.java)
}
14 changes: 13 additions & 1 deletion src/main/kotlin/ris58h/webcalm/javascript/psi/JavaScriptTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ object JavaScriptTypes {
private val OBJECT = RULES[JavaScriptParser.RULE_objectLiteral]
private val ARRAY = RULES[JavaScriptParser.RULE_arrayLiteral]
private val PROPERTY_ASSIGNMENT = RULES[JavaScriptParser.RULE_propertyAssignment]
private val PROPERTY_EXPRESSION_ASSIGNMENT = createLabeledRuleElement("PropertyExpressionAssignment", PROPERTY_ASSIGNMENT)
private val COMPUTED_PROPERTY_EXPRESSION_ASSIGNMENT = createLabeledRuleElement("ComputedPropertyExpressionAssignment", PROPERTY_ASSIGNMENT)
private val FUNCTION_PROPERTY = createLabeledRuleElement("FunctionProperty", PROPERTY_ASSIGNMENT)
private val PROPERTY_GETTER = createLabeledRuleElement("PropertyGetter", PROPERTY_ASSIGNMENT)
private val PROPERTY_SETTER = createLabeledRuleElement("PropertySetter", PROPERTY_ASSIGNMENT)
private val PROPERTY_SHORTHAND = createLabeledRuleElement("PropertyShorthand", PROPERTY_ASSIGNMENT)
private val TEMPLATE_STRING = RULES[JavaScriptParser.RULE_templateStringLiteral]
private val ANONYMOUS_FUNCTION = RULES[JavaScriptParser.RULE_anonymousFunction]
private val CLASS_DECLARATION = RULES[JavaScriptParser.RULE_classDeclaration]
Expand Down Expand Up @@ -262,7 +268,13 @@ object JavaScriptTypes {
ARGUMENT -> JavaScriptArgument(node)
OBJECT -> JavaScriptObject(node)
ARRAY -> JavaScriptArray(node)
PROPERTY_ASSIGNMENT -> JavaScriptPropertyAssignment(node)
PROPERTY_ASSIGNMENT -> unexpectedNode(node)
PROPERTY_EXPRESSION_ASSIGNMENT -> JavaScriptPropertyExpressionAssignment(node)
COMPUTED_PROPERTY_EXPRESSION_ASSIGNMENT -> JavaScriptComputedPropertyExpressionAssignment(node)
FUNCTION_PROPERTY -> JavaScriptFunctionProperty(node)
PROPERTY_GETTER -> JavaScriptPropertyGetter(node)
PROPERTY_SETTER -> JavaScriptPropertySetter(node)
PROPERTY_SHORTHAND -> JavaScriptPropertyShorthand(node)
TEMPLATE_STRING -> JavaScriptTemplateString(node)
ANONYMOUS_FUNCTION -> JavaScriptAnonymousFunction(node)
CLASS_DECLARATION -> JavaScriptClassDeclaration(node)
Expand Down

0 comments on commit 4ece215

Please sign in to comment.