Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"plugins": [["transform-es2015-modules-commonjs"]]
"plugins": [["@babel/plugin-transform-modules-commonjs"]]
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
"test": "jest"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-syntax-jsx": "^7.0.0",
"@babel/plugin-transform-modules-commonjs": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.4.2",
"classnames": "^2.2.5",
"eslint": "^5.4.0",
"eslint-config-prettier": "^3.0.1",
Expand All @@ -55,7 +57,7 @@
"validate-commit-msg": "^2.14.0"
},
"peerDependencies": {
"babel-core": "6.*"
"@babel/core": "7.*"
},
"engines": {
"node": ">=6"
Expand Down
43 changes: 25 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const CLASSNAMES_IMPORT = 'classnames';
const CLASSNAMES_BIND_IMPORT = 'classnames/bind';

const removeClassnamesImport = {
ImportDeclaration(path) {
const { source } = path.node;
const { type, value } = source;

if (
type !== 'StringLiteral' ||
(value !== CLASSNAMES_IMPORT && value !== CLASSNAMES_BIND_IMPORT)
) {
return;
}

path.remove();
},
};

const isIdentifierDefinedAs = (name, importName, scope) => {
const binding = scope.bindings[name];
if (binding.kind !== 'module') {
Expand Down Expand Up @@ -200,23 +216,14 @@ const BabelPluginInlineClassnames = ({ types: t }) => {
}
},

ImportDeclaration(path) {
const { source } = path.node;
const { type, value } = source;

if (
type !== 'StringLiteral' ||
(value !== CLASSNAMES_IMPORT && value !== CLASSNAMES_BIND_IMPORT)
) {
return;
}

path.remove();
},

Program() {
boundName = null;
boundSource = null;
Program: {
enter() {
boundName = null;
boundSource = null;
},
exit(path, state) {
path.traverse(removeClassnamesImport, state);
},
},

VariableDeclaration(path) {
Expand Down Expand Up @@ -249,4 +256,4 @@ const BabelPluginInlineClassnames = ({ types: t }) => {
};
};

export default BabelPluginInlineClassnames;
module.exports = BabelPluginInlineClassnames;
123 changes: 45 additions & 78 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,109 +31,82 @@ exports[`executes string-literal-mixed.js 1`] = `"global FOO BAR"`;
exports[`executes string-literals-only.js 1`] = `"foo bar ham"`;

exports[`transforms basic.js 1`] = `
"
import styles from './styles.css';

(styles.foo || '') + ' ' + (styles.bar || '');"
"import styles from './styles.css';
(styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\");"
`;

exports[`transforms bind.js 1`] = `
"
import styles from './styles.css';

(styles.foo || '') + ' ' + (styles.bar || '');"
"import styles from './styles.css';
(styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\");"
`;

exports[`transforms bind-example.js 1`] = `
"

const inline = {
foo: 'abc',
bar: 'def',
baz: 'xyz'
"const inline = {
foo: 'abc',
bar: 'def',
baz: 'xyz'
};

(inline.foo || '') + ' ' + ((inline.bar || '') + ' ' + ((1 ? inline.baz : '') || ''));"
(inline.foo || \\"\\") + \\" \\" + ((inline.bar || \\"\\") + \\" \\" + ((1 ? inline.baz : \\"\\") || \\"\\"));"
`;

exports[`transforms bind-scoped.js 1`] = `
"
import styles from './styles.css';
"import styles from './styles.css';

function scope1() {
function scope2() {
return (styles.foo || '') + ' ' + (styles.bar || '');
}
function scope2() {
return (styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\");
}

return scope2();
return scope2();
}

scope1();"
`;

exports[`transforms classnames-examples.js 1`] = `
"

'foo bar';
'foo bar';
"\\"foo bar\\";
\\"foo bar\\";
'foo-bar';
'';
'foo bar';
'foo bar';
'foo bar baz quux';
'bar ' + 1;"
\\"\\";
\\"foo bar\\";
\\"foo bar\\";
\\"foo bar baz quux\\";
\\"bar \\" + 1;"
`;

exports[`transforms custom-import-name.js 1`] = `
"
import styles from './styles.css';

(styles.foo || '') + ' ' + (styles.bar || '');"
"import styles from './styles.css';
(styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\");"
`;

exports[`transforms falsy-values-mixed.js 1`] = `
"

const nullValue = null;

'foo ' + (1 + ' ' + (nullValue || ''));"
"const nullValue = null;
\\"foo \\" + (1 + \\" \\" + (nullValue || \\"\\"));"
`;

exports[`transforms falsy-values-only.js 1`] = `
"

'';"
`;
exports[`transforms falsy-values-only.js 1`] = `"'';"`;

exports[`transforms logical-expression.js 1`] = `
"

const FALSY = false;
"const FALSY = false;
const ANOTHER_FALSY = false;

FALSY && ANOTHER_FALSY && 'foo' || '';"
FALSY && ANOTHER_FALSY && 'foo' || \\"\\";"
`;

exports[`transforms logical-expressions.js 1`] = `
"

const TRUTHY = true;
"const TRUTHY = true;
const FALSY = false;
const nullValue = null;

(TRUTHY && nullValue || '') + ' ' + ((FALSY && 'foo' || '') + ' ' + ((TRUTHY ? nullValue : 'ham') || ''));"
(TRUTHY && nullValue || \\"\\") + \\" \\" + ((FALSY && 'foo' || \\"\\") + \\" \\" + ((TRUTHY ? nullValue : 'ham') || \\"\\"));"
`;

exports[`transforms object-expression.js 1`] = `
"
import styles from './styles.css';

'foo bar';
"import styles from './styles.css';
\\"foo bar\\";
'foo-bar';
'foo bar';
'foo bar';
'foo ham';
(styles.foo || '') + ' ' + (styles.bar || '');"
\\"foo bar\\";
\\"foo bar\\";
\\"foo ham\\";
(styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\");"
`;

exports[`transforms other-classnames.js 1`] = `
Expand All @@ -145,29 +118,23 @@ classnames(styles.foo, styles.bar);"
`;

exports[`transforms scoped.js 1`] = `
"

function scope1() {
function scope2() {
return 'foo bar';
}
"function scope1() {
function scope2() {
return \\"foo bar\\";
}

return scope2();
return scope2();
}

scope1();"
`;

exports[`transforms string-literal-mixed.js 1`] = `
"
import styles from './styles.css';

'global ' + ((styles.foo || '') + ' ' + (styles.bar || ''));"
"import styles from './styles.css';
\\"global \\" + ((styles.foo || \\"\\") + \\" \\" + (styles.bar || \\"\\"));"
`;

exports[`transforms string-literals-only.js 1`] = `
"
import styles from './styles.css';

'foo bar ham';"
"import styles from './styles.css';
\\"foo bar ham\\";"
`;
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env jest */
/* eslint no-console: 0 */

const babel = require('babel-core');
const babel = require('@babel/core');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
Expand All @@ -24,7 +24,7 @@ fixtures.forEach(fixture => {
it(`executes ${test}`, () => {
const source = fs.readFileSync(__dirname + '/fixtures/' + test);
const { code } = babel.transform(source, {
plugins: ['transform-es2015-modules-commonjs', plugin],
plugins: ['@babel/plugin-transform-modules-commonjs', plugin],
});

const result = eval(code);
Expand Down
Loading