Skip to content

Commit

Permalink
[Fix] Register export declaration in scope
Browse files Browse the repository at this point in the history
Fixes airbnb#74.

Co-authored-by: Oleg Buiar <oleg.buiar.pro@gmail.com>
Co-authored-by: Michael Vial <michaelvial@gmail.com>
  • Loading branch information
2 people authored and ljharb committed Apr 11, 2020
1 parent 673a7e7 commit 66dfa52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"@babel/cli": "^7.18.10",
"@babel/core": "^7.0.0",
"@babel/node": "^7.18.10",
"@babel/plugin-transform-typescript": "^7.9.4",
"@babel/preset-react": "^7.18.6",
"babel-preset-airbnb": "^3.3.2",
"eslint": "^8.23.0",
Expand Down
19 changes: 12 additions & 7 deletions src/index.js
Expand Up @@ -50,12 +50,13 @@ export default declare(({
}
const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts;
const { file, filename } = state;
let newPath;
if (ignorePattern) {
// Only set the ignoreRegex once:
ignoreRegex = ignoreRegex || new RegExp(ignorePattern);
// Test if we should ignore this:
if (ignoreRegex.test(importPath)) {
return;
return undefined;
}
}
// This plugin only applies for SVGs:
Expand Down Expand Up @@ -108,16 +109,17 @@ export default declare(({
opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps);
}

const svgReplacement = buildSvg(opts);
if (opts.SVG_DEFAULT_PROPS_CODE) {
const svgReplacement = buildSvg(opts);
path.replaceWithMultiple(svgReplacement);
[newPath] = path.replaceWithMultiple(svgReplacement);
} else {
const svgReplacement = buildSvg(opts);
path.replaceWith(svgReplacement);
newPath = path.replaceWith(svgReplacement);
}

file.get('ensureReact')();
file.set('ensureReact', () => {});
}
return newPath;
}

return {
Expand Down Expand Up @@ -159,11 +161,14 @@ export default declare(({
}
},
ExportNamedDeclaration(path, state) {
const { node } = path;
const { node, scope } = path;
if (node.specifiers.length > 0 && node.specifiers[0].local && node.specifiers[0].local.name === 'default') {
const exportName = node.specifiers[0].exported.name;
const filename = parseFilename(node.source.value).name;
applyPlugin(exportName, node.source.value, path, state, true, filename);
const newPath = applyPlugin(exportName, node.source.value, path, state, true, filename);
if (newPath) {
scope.registerDeclaration(newPath);
}
}
},
},
Expand Down
7 changes: 7 additions & 0 deletions test/sanity.js
@@ -1,6 +1,7 @@
import { transformFile, transform } from '@babel/core';
import fs from 'fs';
import path from 'path';
import transformTs from '@babel/plugin-transform-typescript';
import inlineReactSvgPlugin from '../src';

function assertReactImport(result) {
Expand Down Expand Up @@ -172,6 +173,12 @@ transformFile('test/fixtures/test-export-default-as.jsx', {
presets: ['airbnb'],
plugins: [
inlineReactSvgPlugin,
[transformTs, { isTSX: true }],
() => ({
pre() {
console.warn = (msg) => { throw new Error(`Got console.warn: ${msg}`); };
},
}),
],
}, (err, result) => {
if (err) throw err;
Expand Down

0 comments on commit 66dfa52

Please sign in to comment.