Skip to content

Commit

Permalink
refactor: Move transformers into their own files (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
tleunen committed Feb 5, 2017
1 parent fc91f35 commit 7e5faf0
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 143 deletions.
187 changes: 44 additions & 143 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import path from 'path';
import glob from 'glob';
import findBabelConfig from 'find-babel-config';
import getRealPath from './getRealPath';
import transformImportCall from './transformers/import';
import transformSystemImportCall from './transformers/systemImport';
import transformJestCalls from './transformers/jest';
import transformRequireCall from './transformers/require';

const defaultBabelExtensions = ['.js', '.jsx', '.es', '.es6'];

export const defaultExtensions = defaultBabelExtensions;

export function mapModule(sourcePath, currentFile, pluginOpts, cwd) {
// Do not map source starting with a dot
if (sourcePath[0] === '.') {
Expand All @@ -18,6 +22,7 @@ export function mapModule(sourcePath, currentFile, pluginOpts, cwd) {
extensions: pluginOpts.extensions || defaultExtensions,
});
}

export function manipulatePluginOptions(pluginOpts) {
if (pluginOpts.root) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -32,153 +37,49 @@ export function manipulatePluginOptions(pluginOpts) {
return pluginOpts;
}

export default ({ types: t }) => {
function transformRequireCall(nodePath, state, cwd) {
const calleePath = nodePath.get('callee');
if (
!t.isIdentifier(calleePath.node, { name: 'require' }) &&
!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'require' })
)
) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
}
}
}

function transformImportCall(nodePath, state, cwd) {
const source = nodePath.get('source');
if (source.type === 'StringLiteral') {
const modulePath = mapModule(source.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
source.replaceWith(t.stringLiteral(modulePath));
}
}
}

function transformJestCalls(nodePath, state, cwd) {
const calleePath = nodePath.get('callee');

const jestMethods = [
'genMockFromModule',
'mock',
'unmock',
'doMock',
'dontMock',
];

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'jest' }) &&
jestMethods.some(methodName => t.isIdentifier(calleePath.node.property, { name: methodName }))
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
const newArgs = [...args].map(a => a.node);
newArgs[0] = t.stringLiteral(modulePath);
nodePath.replaceWith(t.callExpression(
calleePath.node, newArgs,
));
}
}
}

function transformSystemImportCall(nodePath, state, cwd) {
const calleePath = nodePath.get('callee');

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'System' }) &&
t.isIdentifier(calleePath.node.property, { name: 'import' })
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
}
export default ({ types: t }) => ({
manipulateOptions(babelOptions) {
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);

this.customCWD = findPluginOptions.cwd;
},

pre(file) {
let { customCWD } = this.plugin;
if (customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
customCWD = babelFile
? path.dirname(babelFile)
: null;
}
}

return {
manipulateOptions(babelOptions) {
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);

this.customCWD = findPluginOptions.cwd;
},

pre(file) {
let { customCWD } = this.plugin;
if (customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;
this.moduleResolverCWD = customCWD || process.cwd();
},

const { file: babelFile } = findBabelConfig.sync(startPath);
customCWD = babelFile
? path.dirname(babelFile)
: null;
}

this.moduleResolverCWD = customCWD || process.cwd();
},
visitor: {
CallExpression: {
exit(nodePath, state) {
if (nodePath.node.seen) {
return;
}

visitor: {
CallExpression: {
exit(nodePath, state) {
if (nodePath.node.seen) {
return;
}
transformRequireCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformJestCalls(t, nodePath, mapModule, state, this.moduleResolverCWD);
transformSystemImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);

transformRequireCall(nodePath, state, this.moduleResolverCWD);
transformJestCalls(nodePath, state, this.moduleResolverCWD);
transformSystemImportCall(nodePath, state, this.moduleResolverCWD);

// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
},
// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
},
ImportDeclaration: {
exit(nodePath, state) {
transformImportCall(nodePath, state, this.moduleResolverCWD);
},
},
ImportDeclaration: {
exit(nodePath, state) {
transformImportCall(t, nodePath, mapModule, state, this.moduleResolverCWD);
},
},
};
};
},
});
11 changes: 11 additions & 0 deletions src/transformers/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


export default function transformImportCall(t, nodePath, mapper, state, cwd) {
const source = nodePath.get('source');
if (source.type === 'StringLiteral') {
const modulePath = mapper(source.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
source.replaceWith(t.stringLiteral(modulePath));
}
}
}
36 changes: 36 additions & 0 deletions src/transformers/jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default function transformJestCalls(t, nodePath, mapper, state, cwd) {
const calleePath = nodePath.get('callee');

const jestMethods = [
'genMockFromModule',
'mock',
'unmock',
'doMock',
'dontMock',
];

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'jest' }) &&
jestMethods.some(methodName => t.isIdentifier(calleePath.node.property, { name: methodName }))
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
const newArgs = [...args].map(a => a.node);
newArgs[0] = t.stringLiteral(modulePath);
nodePath.replaceWith(t.callExpression(
calleePath.node, newArgs,
));
}
}
}
27 changes: 27 additions & 0 deletions src/transformers/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default function transformRequireCall(t, nodePath, mapper, state, cwd) {
const calleePath = nodePath.get('callee');
if (
!t.isIdentifier(calleePath.node, { name: 'require' }) &&
!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'require' })
)
) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
}
}
}
26 changes: 26 additions & 0 deletions src/transformers/systemImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function transformSystemImportCall(t, nodePath, mapper, state, cwd) {
const calleePath = nodePath.get('callee');

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'System' }) &&
t.isIdentifier(calleePath.node.property, { name: 'import' })
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)],
));
}
}
}

0 comments on commit 7e5faf0

Please sign in to comment.