Skip to content

Commit

Permalink
Merge 11680db into b0a34c9
Browse files Browse the repository at this point in the history
  • Loading branch information
kjagiello committed Sep 1, 2019
2 parents b0a34c9 + 11680db commit 0b90cfe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
23 changes: 15 additions & 8 deletions djedi-react/babel-plugin.js
Expand Up @@ -65,19 +65,21 @@ module.exports = function djediBabelPlugin({ types: t }) {

// Move the uri into a variable.
const uriId = path.scope.generateUidIdentifier(URI_VAR_NAME);
program.push({ id: uriId, init: t.stringLiteral(uri) });
uriAttr.get("value").replaceWith(t.jSXExpressionContainer(uriId));
program.push({ id: t.cloneNode(uriId), init: t.stringLiteral(uri) });
uriAttr
.get("value")
.replaceWith(t.jSXExpressionContainer(t.cloneNode(uriId)));

let defaultId = undefined;

// Move the default value into a variable (if any).
if (defaultValue != null) {
defaultId = path.scope.generateUidIdentifier(DEFAULT_VAR_NAME);
program.push({
id: defaultId,
id: t.cloneNode(defaultId),
init: t.stringLiteral(dedent(defaultValue)),
});
child.replaceWith(t.jSXExpressionContainer(defaultId));
child.replaceWith(t.jSXExpressionContainer(t.cloneNode(defaultId)));
for (const otherChild of path.get("children")) {
if (otherChild !== child) {
otherChild.remove();
Expand Down Expand Up @@ -217,21 +219,26 @@ function getDefaultValue(valuePath, t) {

function makeClientImport(clientId, t) {
return t.importDeclaration(
[t.importSpecifier(clientId, t.identifier(CLIENT_NAME))],
[t.importSpecifier(t.cloneNode(clientId), t.identifier(CLIENT_NAME))],
t.stringLiteral(MODULE_NAME)
);
}

function makeReportCall(clientId, uriId, defaultId, t) {
return t.expressionStatement(
t.callExpression(
t.memberExpression(clientId, t.identifier(CLIENT_METHOD_NAME)),
t.memberExpression(
t.cloneNode(clientId),
t.identifier(CLIENT_METHOD_NAME)
),
[
t.objectExpression([
t.objectProperty(t.identifier(NODE_URI_KEY), uriId),
t.objectProperty(t.identifier(NODE_URI_KEY), t.cloneNode(uriId)),
t.objectProperty(
t.identifier(NODE_VALUE_KEY),
defaultId == null ? t.identifier("undefined") : defaultId
defaultId == null
? t.identifier("undefined")
: t.cloneNode(defaultId)
),
]),
]
Expand Down
39 changes: 30 additions & 9 deletions djedi-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions djedi-react/package.json
Expand Up @@ -43,6 +43,7 @@
"@babel/plugin-syntax-jsx": "7.2.0",
"@babel/preset-env": "7.4.5",
"@babel/preset-react": "7.0.0",
"babel-check-duplicated-nodes": "^1.0.0",
"babel-eslint": "10.0.1",
"babel-jest": "24.8.0",
"coffeescript": "1.8.0",
Expand Down
21 changes: 21 additions & 0 deletions djedi-react/test/babel-plugin.test.js
@@ -1,5 +1,6 @@
import * as babel from "@babel/core";
import jsx from "@babel/plugin-syntax-jsx";
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
import dedent from "dedent-js";
import fs from "fs";

Expand All @@ -24,6 +25,14 @@ function transform(code) {
}).code;
}

function transformToAST(code) {
return babel.transform(code, {
plugins: [jsx, babelPlugin],
ast: true,
code: false,
}).ast;
}

test("it works", () => {
// The fixture is pretty long, so it makes sense not using an inline snapshot.
const code = fs.readFileSync(require.resolve("./fixtures/nodes.js"), "utf8");
Expand Down Expand Up @@ -66,6 +75,18 @@ _djedi.reportPrefetchableNode({
`);
});

test("ensure no duplicated AST nodes are found", () => {
const code = dedent`
<>
<Node uri="uri1">value1</Node>
<Node uri="uri2">value2</Node>
</>
`;
expect(() => checkDuplicatedNodes(babel, transformToAST(code))).not.toThrow(
Error
);
});

describe("it throws helpful errors", () => {
test("duplicate uri prop", () => {
expect(() => transform('<Node uri="overwritten uri" uri />'))
Expand Down

0 comments on commit 0b90cfe

Please sign in to comment.