Skip to content

Commit f7268de

Browse files
committed
IDE-2470: transpiler support for JSX, step 1
1 parent 3c28d02 commit f7268de

File tree

18 files changed

+25980
-25677
lines changed

18 files changed

+25980
-25677
lines changed

plugins/eu.numberfour.n4js.transpiler.es/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Require-Bundle: eu.numberfour.n4js,
1616
org.eclipse.emf.ecore.xcore;bundle-version="1.3.1",
1717
eu.numberfour.n4js.releng.utils;bundle-version="0.0.1",
1818
it.xsemantics.runtime,
19-
com.google.guava
19+
com.google.guava,
20+
eu.numberfour.n4jsx.model
2021
Export-Package: eu.numberfour.n4js.transpiler.es

plugins/eu.numberfour.n4js.transpiler.es/src/eu/numberfour/n4js/transpiler/es/EcmaScriptTranspiler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import eu.numberfour.n4js.transpiler.es.transform.FormalParameterTransformation;
3939
import eu.numberfour.n4js.transpiler.es.transform.FunctionDeclarationTransformation;
4040
import eu.numberfour.n4js.transpiler.es.transform.InterfaceDeclarationTransformation;
41+
import eu.numberfour.n4js.transpiler.es.transform.JSXTransformation;
4142
import eu.numberfour.n4js.transpiler.es.transform.MemberPatchingTransformation;
4243
import eu.numberfour.n4js.transpiler.es.transform.ModuleWrappingTransformation;
4344
import eu.numberfour.n4js.transpiler.es.transform.SanitizeImportsTransformation;
@@ -92,6 +93,8 @@ public class EcmaScriptTranspiler extends AbstractTranspiler {
9293
private Provider<ArrowFunction_Part1_Transformation> arrowFunction_Part1_TransformationProvider;
9394
@Inject
9495
private Provider<ArrowFunction_Part2_Transformation> arrowFunction_Part2_TransformationProvider;
96+
@Inject
97+
private Provider<JSXTransformation> jsxTransformationProvider;
9598

9699
@Inject
97100
private ProjectUtils projectUtils;
@@ -108,6 +111,7 @@ public class EcmaScriptTranspiler extends AbstractTranspiler {
108111
@Override
109112
protected Transformation[] computeTransformationsToBeExecuted(TranspilerState state) {
110113
return new Transformation[] {
114+
jsxTransformationProvider.get(),
111115
staticPolyfillTransformationProvider.get(),
112116
memberPatchingTransformationProvider.get(),
113117
apiImplStubGenerationTransformationProvider.get(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright (c) 2016 NumberFour AG.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* NumberFour AG - Initial API and implementation
10+
*/
11+
package eu.numberfour.n4js.transpiler.es.transform
12+
13+
import eu.numberfour.n4js.n4JS.Expression
14+
import eu.numberfour.n4js.n4JS.ParameterizedCallExpression
15+
import eu.numberfour.n4js.n4JS.PropertyNameValuePair
16+
import eu.numberfour.n4js.transpiler.Transformation
17+
import eu.numberfour.n4js.transpiler.im.IdentifierRef_IM
18+
import eu.numberfour.n4jsx.n4JSX.JSXAttribute
19+
import eu.numberfour.n4jsx.n4JSX.JSXChild
20+
import eu.numberfour.n4jsx.n4JSX.JSXElement
21+
import eu.numberfour.n4jsx.n4JSX.JSXExpression
22+
import eu.numberfour.n4jsx.n4JSX.JSXPropertyAttribute
23+
import eu.numberfour.n4jsx.n4JSX.JSXSpreadAttribute
24+
25+
import static eu.numberfour.n4js.transpiler.TranspilerBuilderBlocks.*
26+
27+
/**
28+
*
29+
*/
30+
class JSXTransformation extends Transformation {
31+
32+
33+
override assertPreConditions() {
34+
}
35+
36+
override assertPostConditions() {
37+
}
38+
39+
override analyze() {
40+
// ignore
41+
}
42+
43+
override transform() {
44+
collectNodes(state.im, JSXElement, false).forEach[transformJSXElement];
45+
}
46+
47+
def private void transformJSXElement(JSXElement elem) {
48+
replace(elem, convertJSXElement(elem));
49+
}
50+
def private ParameterizedCallExpression convertJSXElement(JSXElement elem) {
51+
return _CallExpr(
52+
_PropertyAccessExpr(steFor_React, steFor_createElement),
53+
(
54+
#[
55+
elem.tagNameFromElement,
56+
if(elem.jsxAttributes.isEmpty) {
57+
_NULL
58+
} else {
59+
_ObjLit(elem.jsxAttributes.map[convertJSXAttribute])
60+
}
61+
]
62+
+ elem.jsxChildren.map[convertJSXChild]
63+
)
64+
);
65+
}
66+
67+
def private Expression convertJSXChild(JSXChild child) {
68+
switch(child) {
69+
JSXElement:
70+
convertJSXElement(child)
71+
JSXExpression:
72+
child.expression
73+
}
74+
}
75+
76+
def private PropertyNameValuePair convertJSXAttribute(JSXAttribute attr) {
77+
switch(attr) {
78+
JSXPropertyAttribute: {
79+
_PropertyNameValuePair(
80+
attr.nameFromPropertyAttribute,
81+
attr.valueExpressionFromPropertyAttribute)
82+
}
83+
JSXSpreadAttribute:
84+
throw new UnsupportedOperationException
85+
}
86+
}
87+
88+
def private Expression getTagNameFromElement(JSXElement elem) {
89+
val nameExpr = elem.jsxElementName.expression;
90+
if(nameExpr instanceof IdentifierRef_IM) {
91+
val id = nameExpr.id_IM;
92+
if(id===null) {
93+
return _StringLiteral(nameExpr.idAsText);
94+
}
95+
}
96+
return nameExpr;
97+
}
98+
99+
def private String getNameFromPropertyAttribute(JSXPropertyAttribute attr) {
100+
val prop = attr.property;
101+
if(prop!==null && !prop.eIsProxy) {
102+
return prop.name;
103+
}
104+
return attr.propertyAsText;
105+
}
106+
def private Expression getValueExpressionFromPropertyAttribute(JSXPropertyAttribute attr) {
107+
return attr.jsxAttributeValue ?: _TRUE;
108+
}
109+
}

plugins/eu.numberfour.n4js.transpiler/src/eu/numberfour/n4js/transpiler/TranspilerBuilderBlocks.xtend

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import eu.numberfour.n4js.ts.types.TSetter
9292
import eu.numberfour.n4js.ts.utils.TypeUtils
9393
import java.math.BigDecimal
9494
import java.util.List
95+
import eu.numberfour.n4js.n4JS.NullLiteral
9596

9697
/**
9798
* Builder methods for intermediate elements.
@@ -590,6 +591,10 @@ public class TranspilerBuilderBlocks
590591
return result;
591592
}
592593

594+
public static def NullLiteral _NULL() {
595+
return N4JSFactory.eINSTANCE.createNullLiteral;
596+
}
597+
593598
public static def BooleanLiteral _TRUE() {
594599
return _BooleanLiteral(true);
595600
}

plugins/eu.numberfour.n4js.transpiler/src/eu/numberfour/n4js/transpiler/TranspilerComponent.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,18 @@ public SymbolTableEntryInternal steFor_require() {
658658
return getSymbolTableEntryInternal("require", true);
659659
}
660660

661+
// ################################################################################################################
662+
// JSX RELATED THINGS (TODO IDE-2416 remove this from n4js transpiler)
663+
664+
/** "React" - retrieve the internal symbol table entry for the symbol "React" */
665+
public SymbolTableEntryInternal steFor_React() {
666+
667+
return getSymbolTableEntryInternal("React", true);
668+
}
669+
670+
/** "createElement" - retrieve the internal symbol table entry for the symbol "createElement" */
671+
public SymbolTableEntryInternal steFor_createElement() {
672+
673+
return getSymbolTableEntryInternal("createElement", true);
674+
}
661675
}

plugins/eu.numberfour.n4jsx.ui/src-gen/eu/numberfour/n4jsx/ui/contentassist/antlr/N4JSXParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected String getRuleName(AbstractElement element) {
4141
put(grammarAccess.getJSXElementAccess().getAlternatives_3(), "rule__JSXElement__Alternatives_3");
4242
put(grammarAccess.getJSXChildAccess().getAlternatives(), "rule__JSXChild__Alternatives");
4343
put(grammarAccess.getJSXAttributeAccess().getAlternatives(), "rule__JSXAttribute__Alternatives");
44-
put(grammarAccess.getJSXPropertyAttributeAccess().getAlternatives_2(), "rule__JSXPropertyAttribute__Alternatives_2");
44+
put(grammarAccess.getJSXPropertyAttributeAccess().getAlternatives_1_1(), "rule__JSXPropertyAttribute__Alternatives_1_1");
4545
put(grammarAccess.getScriptElementAccess().getAlternatives(), "rule__ScriptElement__Alternatives");
4646
put(grammarAccess.getAnnotatedScriptElementAccess().getAlternatives_1(), "rule__AnnotatedScriptElement__Alternatives_1");
4747
put(grammarAccess.getAnnotatedScriptElementAccess().getAlternatives_1_3_0(), "rule__AnnotatedScriptElement__Alternatives_1_3_0");
@@ -151,7 +151,8 @@ protected String getRuleName(AbstractElement element) {
151151
put(grammarAccess.getJSXElementNameExpressionAccess().getGroup_1(), "rule__JSXElementNameExpression__Group_1__0");
152152
put(grammarAccess.getJSXSpreadAttributeAccess().getGroup(), "rule__JSXSpreadAttribute__Group__0");
153153
put(grammarAccess.getJSXPropertyAttributeAccess().getGroup(), "rule__JSXPropertyAttribute__Group__0");
154-
put(grammarAccess.getJSXPropertyAttributeAccess().getGroup_2_1(), "rule__JSXPropertyAttribute__Group_2_1__0");
154+
put(grammarAccess.getJSXPropertyAttributeAccess().getGroup_1(), "rule__JSXPropertyAttribute__Group_1__0");
155+
put(grammarAccess.getJSXPropertyAttributeAccess().getGroup_1_1_1(), "rule__JSXPropertyAttribute__Group_1_1_1__0");
155156
put(grammarAccess.getScriptAccess().getGroup(), "rule__Script__Group__0");
156157
put(grammarAccess.getAnnotatedScriptElementAccess().getGroup(), "rule__AnnotatedScriptElement__Group__0");
157158
put(grammarAccess.getAnnotatedScriptElementAccess().getGroup_1_0(), "rule__AnnotatedScriptElement__Group_1_0__0");
@@ -628,8 +629,8 @@ protected String getRuleName(AbstractElement element) {
628629
put(grammarAccess.getJSXAttributesAccess().getJsxAttributesAssignment(), "rule__JSXAttributes__JsxAttributesAssignment");
629630
put(grammarAccess.getJSXSpreadAttributeAccess().getExpressionAssignment_2(), "rule__JSXSpreadAttribute__ExpressionAssignment_2");
630631
put(grammarAccess.getJSXPropertyAttributeAccess().getPropertyAssignment_0(), "rule__JSXPropertyAttribute__PropertyAssignment_0");
631-
put(grammarAccess.getJSXPropertyAttributeAccess().getJsxAttributeValueAssignment_2_0(), "rule__JSXPropertyAttribute__JsxAttributeValueAssignment_2_0");
632-
put(grammarAccess.getJSXPropertyAttributeAccess().getJsxAttributeValueAssignment_2_1_1(), "rule__JSXPropertyAttribute__JsxAttributeValueAssignment_2_1_1");
632+
put(grammarAccess.getJSXPropertyAttributeAccess().getJsxAttributeValueAssignment_1_1_0(), "rule__JSXPropertyAttribute__JsxAttributeValueAssignment_1_1_0");
633+
put(grammarAccess.getJSXPropertyAttributeAccess().getJsxAttributeValueAssignment_1_1_1_1(), "rule__JSXPropertyAttribute__JsxAttributeValueAssignment_1_1_1_1");
633634
put(grammarAccess.getScriptAccess().getAnnotationsAssignment_1(), "rule__Script__AnnotationsAssignment_1");
634635
put(grammarAccess.getScriptAccess().getScriptElementsAssignment_2(), "rule__Script__ScriptElementsAssignment_2");
635636
put(grammarAccess.getAnnotatedScriptElementAccess().getDeclaredModifiersAssignment_1_2_1_0_0(), "rule__AnnotatedScriptElement__DeclaredModifiersAssignment_1_2_1_0_0");

0 commit comments

Comments
 (0)