Skip to content

Commit

Permalink
Add custom calls option (#185)
Browse files Browse the repository at this point in the history
Add an option `transformFunctions` for the user to provide a list of functions which will have their first argument transformed based on the plugin root and alias
  • Loading branch information
fatfisz authored and tleunen committed Jun 6, 2017
1 parent 7163659 commit e25fc96
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
- `cwd`: By default, the working directory is the one used for the resolver, but you can override it for your project.
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
- `transformFunctions`: Array of functions and methods that will have their first argument transformed. By default those methods are: `require`, `require.resolve`, `System.import`, `jest.genMockFromModule`, `jest.mock`, `jest.unmock`, `jest.doMock`, `jest.dontMock`.

### Regular expression alias

Expand Down
19 changes: 19 additions & 0 deletions src/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import pkgUp from 'pkg-up';


const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
const defaultTransformedMethods = [
'require',
'require.resolve',
'System.import',
'jest.genMockFromModule',
'jest.mock',
'jest.unmock',
'jest.doMock',
'jest.dontMock',
];

function isRegExp(string) {
return string.startsWith('^') || string.endsWith('$');
Expand Down Expand Up @@ -90,10 +100,19 @@ function normalizeAlias(opts) {
}
}

function normalizeTransformedMethods(opts) {
if (opts.transformFunctions) {
opts.transformFunctions = [...defaultTransformedMethods, ...opts.transformFunctions];
} else {
opts.transformFunctions = defaultTransformedMethods;
}
}

export default function normalizeOptions(opts, file) {
normalizeCwd(opts, file); // This has to go first because other options rely on cwd
normalizeRoot(opts);
normalizeAlias(opts);
normalizeTransformedMethods(opts);

if (!opts.extensions) {
opts.extensions = defaultExtensions;
Expand Down
15 changes: 3 additions & 12 deletions src/transformers/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ import {
} from '../utils';


const patterns = [
'require',
'require.resolve',
'System.import',
'jest.genMockFromModule',
'jest.mock',
'jest.unmock',
'jest.doMock',
'jest.dontMock',
];

export default function transformCall(nodePath, state) {
const calleePath = nodePath.get('callee');
const isNormalCall = patterns.some(pattern => matchesPattern(state.types, calleePath, pattern));
const isNormalCall = state.opts.transformFunctions.some(
pattern => matchesPattern(state.types, calleePath, pattern),
);

if (isNormalCall || isImportCall(state.types, nodePath)) {
mapPathString(nodePath.get('arguments.0'), state);
Expand Down
84 changes: 84 additions & 0 deletions test/custom-call.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { transform } from 'babel-core';
import plugin from '../src';


const calls = [
'customMethod.something',
];

describe('custom calls', () => {
const transformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './test/testproject/src',
alias: {
test: './test/testproject/test',
},
transformFunctions: [
'customMethod.something',
],
}],
],
};

calls.forEach((name) => {
describe(name, () => {
it('should resolve the path based on the root config', () => {
const code = `${name}("components/Header/SubHeader", ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}("./test/testproject/src/components/Header/SubHeader", ...args);`);
});

it('should alias the path', () => {
const code = `${name}("test", ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}("./test/testproject/test", ...args);`);
});

it('should not change a relative path', () => {
const code = `${name}("./utils", ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}("./utils", ...args);`);
});

it('should handle no arguments', () => {
const code = `${name}();`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}();`);
});

it('should handle the first argument not being a string literal', () => {
const code = `${name}(path, ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}(path, ...args);`);
});

it('should handle an empty path', () => {
const code = `${name}('', ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}('', ...args);`);
});

it('should ignore the call if the method name is not fully matched (suffix)', () => {
const code = `${name}.after("components/Sidebar/Footer", ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`${name}.after("components/Sidebar/Footer", ...args);`);
});

it('should ignore the call if the method name is not fully matched (prefix)', () => {
const code = `before.${name}("components/Sidebar/Footer", ...args);`;
const result = transform(code, transformerOpts);

expect(result.code).toBe(`before.${name}("components/Sidebar/Footer", ...args);`);
});
});
});
});

0 comments on commit e25fc96

Please sign in to comment.