Skip to content

Commit

Permalink
fix(babel): use @babel/types
Browse files Browse the repository at this point in the history
  • Loading branch information
lttb committed May 15, 2019
1 parent 848d425 commit 5ad6fe8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
85 changes: 42 additions & 43 deletions packages/babel/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const t = require('@babel/types');
const template = require('@babel/template').default;
const syntaxJsx = require('@babel/plugin-syntax-jsx').default;
const {addDefault} = require('@babel/helper-module-imports');
Expand All @@ -12,58 +13,56 @@ const buildClassName = template.expression(`
NAME.styles["ELEMENT"]
`);

const defaultOptions = {
target: 'react',
postcss: false,
elementFallback: true,
files: false,
stringStyle: false,
};

module.exports = (babel, pluginOptions = {}) => {
const {types: t} = babel;

const toObjectExpression = obj =>
t.objectExpression(
Object.entries(obj).map(([key, value]) =>
t.objectProperty(
t.stringLiteral(key),
t.templateLiteral(
[
t.templateElement({
raw: value,
cooked: value,
}),
],
[],
),
const toObjectExpression = obj =>
t.objectExpression(
Object.entries(obj).map(([key, value]) =>
t.objectProperty(
t.stringLiteral(key),
t.templateLiteral(
[
t.templateElement({
raw: value,
cooked: value,
}),
],
[],
),
),
);
),
);

/**
* Basic React.Fragment check
* We can improve it by checking import aliases if needs
*/
const isReactFragment = node => {
if (!node) return false;
/**
* Basic React.Fragment check
* We can improve it by checking import aliases if needs
*/
const isReactFragment = node => {
if (!node) return false;

if (t.isJSXFragment(node)) return true;
if (t.isJSXFragment(node)) return true;

const [element] = node.arguments || [];
const [element] = node.arguments || [];

if (t.isIdentifier(element)) return element.name === 'Fragment';
if (t.isIdentifier(element)) return element.name === 'Fragment';

if (t.isMemberExpression(element)) {
return (
element.object.name === 'React' &&
element.property.name === 'Fragment'
);
}
if (t.isMemberExpression(element)) {
return (
element.object.name === 'React' &&
element.property.name === 'Fragment'
);
}

return false;
};
return false;
};

const defaultOptions = {
target: 'react',
postcss: false,
elementFallback: true,
files: false,
stringStyle: false,
};

module.exports = (babel, pluginOptions = {}) => {
const options = Object.assign({}, defaultOptions, pluginOptions);

if (options.target === 'preact') {
Expand Down
1 change: 1 addition & 0 deletions packages/babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/helper-module-imports": "^7.0.0",
"@babel/plugin-syntax-jsx": "^7.2.0",
"@babel/template": "^7.2.2",
"@babel/types": "^7.3.0",
"@reshadow/postcss": "^0.0.1-alpha.21",
"@reshadow/utils": "^0.0.1-alpha.21",
"common-tags": "1.8.0",
Expand Down

0 comments on commit 5ad6fe8

Please sign in to comment.