From 379bc775edab2d223f55c64ad8f7d52bf10ccb22 Mon Sep 17 00:00:00 2001 From: Bogdan Savluk Date: Thu, 13 May 2021 03:15:31 +0200 Subject: [PATCH] convert @babel/plugin-transform-react-jsx to typescript (#13221) * babel-plugin-transform-react-jsx flowts rename * babel-plugin-transform-react-jsx flowts convert * babel-plugin-transform-react-jsx * babel-plugin-transform-react-jsx fix * make generate-tsconfig * yarn install * babel-plugin-transform-react-jsx remove unused typecast --- .../package.json | 3 ++- .../{create-plugin.js => create-plugin.ts} | 20 +++++++++++++------ .../src/{development.js => development.ts} | 0 .../src/{index.js => index.ts} | 0 tsconfig.json | 4 ++++ yarn.lock | 2 ++ 6 files changed, 22 insertions(+), 7 deletions(-) rename packages/babel-plugin-transform-react-jsx/src/{create-plugin.js => create-plugin.ts} (97%) rename packages/babel-plugin-transform-react-jsx/src/{development.js => development.ts} (100%) rename packages/babel-plugin-transform-react-jsx/src/{index.js => index.ts} (100%) diff --git a/packages/babel-plugin-transform-react-jsx/package.json b/packages/babel-plugin-transform-react-jsx/package.json index e97ed6e0a9d3..2374ca382cfd 100644 --- a/packages/babel-plugin-transform-react-jsx/package.json +++ b/packages/babel-plugin-transform-react-jsx/package.json @@ -28,6 +28,7 @@ }, "devDependencies": { "@babel/core": "workspace:*", - "@babel/helper-plugin-test-runner": "workspace:*" + "@babel/helper-plugin-test-runner": "workspace:*", + "@babel/traverse": "workspace:*" } } diff --git a/packages/babel-plugin-transform-react-jsx/src/create-plugin.js b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts similarity index 97% rename from packages/babel-plugin-transform-react-jsx/src/create-plugin.js rename to packages/babel-plugin-transform-react-jsx/src/create-plugin.ts index e7d257b6dc2f..430a867c3657 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.js +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -149,7 +149,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, let pragmaFragSet = !!options.pragmaFrag; if (file.ast.comments) { - for (const comment of (file.ast.comments: Array)) { + for (const comment of file.ast.comments) { const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec( comment.value, ); @@ -313,6 +313,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, if (node.name === "this" && t.isReferenced(node, parent)) { return t.thisExpression(); } else if (t.isValidIdentifier(node.name, false)) { + // @ts-expect-error todo(flow->ts) node.type = "Identifier"; } else { return t.stringLiteral(node.name); @@ -416,7 +417,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, const openingPath = path.get("openingElement"); const args = [getTag(openingPath)]; - let attribs = []; + const attribsArray = []; const extracted = Object.create(null); // for React.jsx, key, __source (dev), and __self (dev) is passed in as @@ -443,17 +444,23 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, break; } default: - attribs.push(attr); + attribsArray.push(attr); } } else { - attribs.push(attr); + attribsArray.push(attr); } } const children = t.react.buildChildren(path.node); - if (attribs.length || children.length) { - attribs = buildJSXOpeningElementAttributes(attribs, file, children); + let attribs: t.ObjectExpression; + + if (attribsArray.length || children.length) { + attribs = buildJSXOpeningElementAttributes( + attribsArray, + file, + children, + ); } else { // attributes should never be null attribs = t.objectExpression([]); @@ -555,6 +562,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, if (t.isIdentifier(tagExpr)) { tagName = tagExpr.name; } else if (t.isLiteral(tagExpr)) { + // @ts-expect-error todo(flow->ts) value in missing for NullLiteral tagName = tagExpr.value; } diff --git a/packages/babel-plugin-transform-react-jsx/src/development.js b/packages/babel-plugin-transform-react-jsx/src/development.ts similarity index 100% rename from packages/babel-plugin-transform-react-jsx/src/development.js rename to packages/babel-plugin-transform-react-jsx/src/development.ts diff --git a/packages/babel-plugin-transform-react-jsx/src/index.js b/packages/babel-plugin-transform-react-jsx/src/index.ts similarity index 100% rename from packages/babel-plugin-transform-react-jsx/src/index.js rename to packages/babel-plugin-transform-react-jsx/src/index.ts diff --git a/tsconfig.json b/tsconfig.json index 8ac0e03d54b7..88a1fbf2cd1c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,6 +25,7 @@ "./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts", "./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts", "./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts", + "./packages/babel-plugin-transform-react-jsx/src/**/*.ts", "./packages/babel-template/src/**/*.ts", "./packages/babel-traverse/src/**/*.ts", "./packages/babel-types/src/**/*.ts" @@ -100,6 +101,9 @@ "@babel/plugin-syntax-async-do-expressions": [ "./packages/babel-plugin-syntax-async-do-expressions/src" ], + "@babel/plugin-transform-react-jsx": [ + "./packages/babel-plugin-transform-react-jsx/src" + ], "@babel/template": [ "./packages/babel-template/src" ], diff --git a/yarn.lock b/yarn.lock index d5a2da8f7161..506cefb067fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -643,6 +643,7 @@ __metadata: resolution: "@babel/helper-module-transforms@condition:BABEL_8_BREAKING?:workspace:^7.14.2#5131c1" dependencies: "@babel/helper-module-transforms-BABEL_8_BREAKING-false": "npm:@babel/helper-module-transforms@workspace:^7.14.2" + checksum: 99b7a76ae03cc33a6d7a2e970aa72831e096f15bc2c58be10fb297e3fa411d9ba344cd0f93590c1b802a1b4450b7ea29db9aeeb7aeb7b2f9241e751c20a5079c languageName: node linkType: hard @@ -2750,6 +2751,7 @@ __metadata: "@babel/helper-plugin-test-runner": "workspace:*" "@babel/helper-plugin-utils": "workspace:^7.13.0" "@babel/plugin-syntax-jsx": "workspace:^7.12.13" + "@babel/traverse": "workspace:*" "@babel/types": "workspace:^7.13.12" peerDependencies: "@babel/core": ^7.0.0-0