Skip to content

Commit

Permalink
add validation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmews committed Feb 13, 2024
1 parent 0e4609b commit d8b1cba
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.n4js.n4JS.JSXPropertyAttribute;
import org.eclipse.n4js.n4JS.JSXSpreadAttribute;
import org.eclipse.n4js.n4JS.NamespaceImportSpecifier;
import org.eclipse.n4js.n4JS.ObjectLiteral;
import org.eclipse.n4js.n4JS.ParameterizedCallExpression;
import org.eclipse.n4js.n4JS.PropertyAssignment;
import org.eclipse.n4js.n4JS.PropertyNameValuePair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,10 @@ public enum IssueCodes {
JSX_JSXSPROPERTYATTRIBUTE_NOT_DECLARED_IN_PROPS(WARNING,
"Attribute {0} is not a declared property in the props of {1}."),

/** No parameter */
JSX_JSXSPROPERTYATTRIBUTE_CHILDREN(WARNING,
"Attribute 'children' will be overwritten by jsx child elements."),

/** 0: JSX element in non-JSX file */
JSX_JSXELEMENT_IN_NON_JSX_RESOURCE(ERROR, "JSX element is expected to be placed in JSX like resource, was {0}."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class N4JSXValidator extends AbstractN4JSDeclarativeValidator {

val firstJSXAbstractElement = script.eAllContents.findFirst[it instanceof JSXAbstractElement]
if (firstJSXAbstractElement !== null && reactHelper.getJsxBackendModule(script.eResource) === null)
addIssue(firstJSXAbstractElement, JSX_REACT_NOT_RESOLVED.toIssueItem());
addIssue(firstJSXAbstractElement, JSX_REACT_NOT_RESOLVED);
}

/** Make sure the namespace to react module is React. */
Expand All @@ -111,7 +111,7 @@ class N4JSXValidator extends AbstractN4JSDeclarativeValidator {
val importedModule = importSpecifier.importedModule;
if (reactModule !== null && importedModule === reactModule) {
if (importSpecifier.alias != ReactHelper.REACT_NAMESPACE_NAME) {
addIssue(importSpecifier, JSX_REACT_NAMESPACE_NOT_ALLOWED.toIssueItem());
addIssue(importSpecifier, JSX_REACT_NAMESPACE_NOT_ALLOWED);
}
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ class N4JSXValidator extends AbstractN4JSDeclarativeValidator {
val actualTypeRef = TypeUtils.createTypeRef(tclass, TypingStrategy.DEFAULT, true);
val resultSubType = ts.subtype(G, actualTypeRef, expectedTypeRef)
if (resultSubType.failure) {
addIssue(expr, JSX_REACT_ELEMENT_CLASS_NOT_REACT_ELEMENT_ERROR.toIssueItem());
addIssue(expr, JSX_REACT_ELEMENT_CLASS_NOT_REACT_ELEMENT_ERROR);
}
}

Expand Down Expand Up @@ -287,6 +287,23 @@ class N4JSXValidator extends AbstractN4JSDeclarativeValidator {
}
}

@Check
def public void checkChildrenJSXPropertyAttribute(JSXPropertyAttribute propertyAttribute) {
if (!ReactHelper.REACT_ELEMENT_PROPERTY_CHILDREN_NAME.equals(propertyAttribute.propertyAsText)) {
return;
}
val jsxElem = propertyAttribute.eContainer as JSXElement;
if (jsxElem.jsxChildren.isEmpty) {
return;
}

addIssue(
propertyAttribute,
JSX_PROPERTY_ATTRIBUTE__PROPERTY,
JSX_JSXSPROPERTYATTRIBUTE_CHILDREN.toIssueItem()
);
}

/**
* Check the type conformity of types of spread operator's attributes against "props" types
* See Req. IDE-241119
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public class SpecXtTest {

@XtFolder
static String getFolder() {
return "xt-tests";
return "xt-tests/react_transpile";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2016 NumberFour AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* NumberFour AG - Initial API and implementation
*/

/* XPECT_SETUP org.eclipse.n4js.spec.tests.SpecXtTest
Workspace {
Project "N4JSXSpecTest" {
Folder "src" {
ThisFile {}
}
File "package.json" { from="../../package_default.json" }
}
Project "react" {
File "index.n4jsd" { from = "../react/index.n4jsd" }
File "index.js" { from = "../react/index.js" }
File "jsx-runtime.js" { from = "../react/jsx-runtime.js" }
File "package.json" { from = "../react/package.json" }
}
}
END_SETUP
*/


// XPECT noerrors -->
// XPECT nowarnings -->
const MyDiv1 = <div children={"Hello World"}></div>;
MyDiv1;

// XPECT warnings --> "Attribute 'children' will be overwritten by jsx child elements." at "children"
const MyDiv2 = <div children={"Hello World"}>{"Guten Tag"}</div>;
MyDiv2;

// XPECT warnings --> "Attribute 'children' will be overwritten by jsx child elements." at "children"
const MyDiv3 = <div children={"Hello World"}><h2></h2></div>;
MyDiv3;




/* XPECT compileResult ---
// Generated by N4JS transpiler; for copyright see original N4JS source file.
import 'n4js-runtime'
import {jsx as $jsx} from 'react/jsx-runtime.js'
const MyDiv1 = $jsx('div', {
children: "Hello World"
});
MyDiv1;
const MyDiv2 = $jsx('div', Object.assign({
children: "Hello World"
}, {
children: "Guten Tag"
}));
MyDiv2;
const MyDiv3 = $jsx('div', Object.assign({
children: "Hello World"
}, {
children: $jsx('h2', {})
}));
MyDiv3;
//# sourceMappingURL=jsx_transpile_children.map
--- */

0 comments on commit d8b1cba

Please sign in to comment.