Added test for js-ast-utils/getJSXAttribute.ts
ADD
internal/js-ast-utils/getJSXAttribute.test.ts
+import {test} from "rome";
+import {parseJS} from "@internal/js-parser";
+import {dedent} from "@internal/string-utils";
+import {
+ jsArrowFunctionExpression,
+ jsBlockStatement,
+ jsBooleanLiteral,
+ jsCallExpression,
+ jsExpressionStatement,
+ jsObjectExpression,
+ jsReferenceIdentifier,
+ jsStringLiteral,
+ jsxAttribute,
+ jsxElement,
+ jsxExpressionContainer,
+} from "@internal/ast";
+import {getJSXAttribute} from "@internal/js-ast-utils/getJSXAttribute";
+
+test(
+ "verify attribute resolution",
+ async (t) => {
+ const jsx = jsxElement.assert(
+ jsExpressionStatement.assert(
+ parseJS({
+ path: "unknown",
+ input: dedent`
+ <div className="foo" onClick={() => {alert("hello")}} custom={true} other={{one: "first", two: "second"}}/>
+ `,
+ }).body[0],
+ ).expression,
+ );
+
+ t.is(
+ jsStringLiteral.assert(
+ jsxAttribute.assert(getJSXAttribute(jsx, "className")).value,
+ ).value,
+ "foo",
+ );
+
+ t.is(
+ jsReferenceIdentifier.assert(
+ jsCallExpression.assert(
+ jsExpressionStatement.assert(
+ jsBlockStatement.assert(
+ jsArrowFunctionExpression.assert(
+ jsxExpressionContainer.assert(
+ jsxAttribute.assert(getJSXAttribute(jsx, "onClick")).value,
+ ).expression,
+ ).body,
+ ).body[0],
+ ).expression,
+ ).callee,
+ ).name,
+ "alert",
+ );
+
+ t.is(
+ jsBooleanLiteral.assert(
+ jsxExpressionContainer.assert(
+ jsxAttribute.assert(getJSXAttribute(jsx, "custom")).value,
+ ).expression,
+ ).value,
+ true,
+ );
+
+ t.is(
+ jsObjectExpression.assert(
+ jsxExpressionContainer.assert(
+ jsxAttribute.assert(getJSXAttribute(jsx, "other")).value,
+ ).expression,
+ ).properties.length,
+ 2,
+ );
+ },
+);

MODIFY
internal/js-ast-utils/getRequireSource.ts
internal/js-ast-utils/getRequireSource.ts
@@ -42,12 +42,12 @@ export function getRequireSource(
callee.name === "require" &&
scope.getBinding("require") === undefined;
- const validRomeRequreCallee =
+ const validRomeRequireCallee =
(doesNodeMatchPattern(callee, "Rome.requireDefault") ||
doesNodeMatchPattern(callee, "Rome.requireNamespace")) &&
scope.getBinding("Rome") === undefined;
- if (validRequireCallee || validRomeRequreCallee) {
+ if (validRequireCallee || validRomeRequireCallee) {
return firstArg.value;
}

Added test for js-ast-utils/getJSXAttribute.ts (#1121)

Co-authored-by: Sebastian <853712+sebmck@users.noreply.github.com>