Skip to content

Commit

Permalink
Merge pull request #29 from ljharb/add_react
Browse files Browse the repository at this point in the history
Add react import
  • Loading branch information
kesne authored Nov 6, 2017
2 parents c03fd77 + 3bab74c commit 22e3b4f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"babel-cli": "^6.14.0",
"babel-core": "^6.14.0",
"babel-preset-airbnb": "^2.0.0",
"babel-preset-react": "^6.24.1",
"eslint": "^3.5.0",
"eslint-config-airbnb": "^11.1.0",
"eslint-plugin-import": "^1.15.0",
Expand Down
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import template from 'babel-template';
import traverse from 'babel-traverse';
import { parse } from 'babylon';
import resolveFrom from 'resolve-from';

import optimize from './optimize';
import escapeBraces from './escapeBraces';
import transformSvg from './transformSvg';
Expand All @@ -22,23 +23,35 @@ let ignoreRegex;

export default ({ types: t }) => ({
visitor: {
Program: {
exit({ scope, node }) {
if (!scope.hasBinding('React')) {
const reactImportDeclaration = t.importDeclaration([
t.importDefaultSpecifier(t.identifier('React')),
], t.stringLiteral('react'));

node.body.unshift(reactImportDeclaration);
}
},
},
ImportDeclaration(path, state) {
const importPath = path.node.source.value;
const { ignorePattern, caseSensitive } = state.opts;
if (ignorePattern) {
// Only set the ignoreRegex once:
ignoreRegex = ignoreRegex || new RegExp(ignorePattern);
// Test if we should ignore this:
if (ignoreRegex.test(path.node.source.value)) {
if (ignoreRegex.test(importPath)) {
return;
}
}
// This plugin only applies for SVGs:
if (extname(path.node.source.value) === '.svg') {
if (extname(importPath) === '.svg') {
// We only support the import default specifier, so let's use that identifier:
const importIdentifier = path.node.specifiers[0].local;
const iconPath = state.file.opts.filename;
const svgPath = resolveFrom(dirname(iconPath), path.node.source.value);
if(caseSensitive && !fileExistsWithCaseSync(svgPath)) {
const svgPath = resolveFrom(dirname(iconPath), importPath);
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) {
throw new Error(`File path didn't match case of file on disk: ${svgPath}`);
}
const rawSource = readFileSync(svgPath, 'utf8');
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/test-no-react.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MySvg from './close.svg';

export function MyFunctionIcon() {
return MySvg;
}
44 changes: 34 additions & 10 deletions test/sanity.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import { transformFile } from 'babel-core';

function assertReactImport(result) {
const match = result.code.match(/import React from 'react'/);
if (!match) {
throw new Error('no React import found');
}
if (match.length !== 1) {
throw new Error('more or less than one match found');
}
}

transformFile('test/fixtures/test.jsx', {
presets: ['airbnb'],
babelrc: false,
presets: ['react'],
plugins: [
'../../src/index',
],
}, (err, result) => {
if (err) throw err;
console.log(result.code);
assertReactImport(result);
console.log('test/fixtures/test.jsx', result.code);
});

transformFile('test/fixtures/test-case-sensitive.jsx', {
presets: ['airbnb'],
transformFile('test/fixtures/test-no-react.jsx', {
babelrc: false,
presets: ['react'],
plugins: [
['../../src/index',
{
"caseSensitive": true
}]
'../../src/index',
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-no-react.jsx', result.code);
assertReactImport(result);
});

transformFile('test/fixtures/test-case-sensitive.jsx', {
babelrc: false,
presets: ['react'],
plugins: [
['../../src/index', {
caseSensitive: true,
}],
],
}, (err) => {
if (err && err.message.indexOf('match case') !== -1) {
console.log("Test passed: Expected case sensitive error was thrown");
console.log('test/fixtures/test-case-sensitive.jsx', 'Test passed: Expected case sensitive error was thrown');
} else {
throw new Error("Test failed: Expected case sensitive error wasn't thrown");
}
});
});

0 comments on commit 22e3b4f

Please sign in to comment.