Skip to content

Commit

Permalink
Validate babel output against duplicated AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jun 17, 2018
1 parent 74e8a9b commit 7875276
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 591 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"**/react-dom": "^16.2.0"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.51",
"@babel/plugin-external-helpers": "7.0.0-beta.51",
"@babel/core": "7.0.0-beta.40",
"@babel/plugin-external-helpers": "7.0.0-beta.40",
"@babel/preset-env": "7.0.0-beta.40",
"@babel/preset-flow": "7.0.0-beta.51",
"@babel/preset-react": "7.0.0-beta.51",
"@babel/preset-stage-0": "7.0.0-beta.51",
"@babel/preset-flow": "7.0.0-beta.40",
"@babel/preset-react": "7.0.0-beta.40",
"@babel/preset-stage-0": "7.0.0-beta.40",
"babel-check-duplicated-nodes": "^1.0.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"clean": "rimraf lib"
},
"dependencies": {
"@babel/helper-module-imports": "7.0.0-beta.51",
"@babel/helper-module-imports": "7.0.0-beta.40",
"@emotion/babel-utils": "^0.5.3",
"@emotion/hash": "^0.6.2",
"@emotion/memoize": "^0.6.1",
Expand All @@ -28,7 +28,7 @@
"touch": "^1.0.0"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.51",
"@babel/core": "7.0.0-beta.40",
"babel-check-duplicated-nodes": "^1.0.0",
"babel-cli": "^6.24.1",
"npm-run-all": "^4.0.2",
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-plugin-emotion/src/babel-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { hashArray } from './index'
import type { BabelPath, EmotionBabelPluginPass } from './index'
import type { Types, Identifier } from 'babel-flow-types'

function cloneNode(t, node) {
return (typeof t.cloneNode === 'function' ? t.cloneNode : t.cloneDeep)(node)
}

function getDeclaratorName(path: BabelPath, t: Types) {
// $FlowFixMe
const parent = path.findParent(p => p.isVariableDeclarator())
Expand Down Expand Up @@ -66,7 +70,7 @@ export function buildMacroRuntimeNode(
importName
] = path.scope.generateUidIdentifier(path.node.name)
}
return state.emotionImports[runtimeImportPath][importName]
return cloneNode(t, state.emotionImports[runtimeImportPath][importName])
}

export function addRuntimeImports(state: EmotionMacroPluginPass, t: Types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ var _emotion = require(\\"emotion\\");
var _react = _interopRequireWildcard(require(\\"react\\"));
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
class Home extends _react.Component {
render() {
Expand Down
14 changes: 11 additions & 3 deletions packages/babel-plugin-emotion/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jest.mock('fs')
fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

const isBabel7 = babel => parseInt(babel.version.split('.')[0], 10) === 7

const createInlineTester = babel => opts => {
if (!opts.opts) opts.opts = {}
const { code, ast } = babel.transform(opts.code, {
Expand All @@ -42,7 +44,9 @@ const createInlineTester = babel => opts => {
babelrc: false,
ast: true
})
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
if (isBabel7(babel)) {
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
}
expect(code).toMatchSnapshot()
}

Expand Down Expand Up @@ -73,7 +77,9 @@ const createExtractTester = babel => opts => {
babelrc: false,
ast: true
})
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
if (isBabel7(babel)) {
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
}
if (extract) {
expect(
code +
Expand Down Expand Up @@ -110,7 +116,9 @@ const createMacroTester = babel => opts => {
filename: opts.filename || __filename,
ast: true
})
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
if (isBabel7(babel)) {
expect(() => checkDuplicatedNodes(babel, ast)).not.toThrow()
}
expect(code).toMatchSnapshot()
}

Expand Down
Loading

0 comments on commit 7875276

Please sign in to comment.