Skip to content

Commit

Permalink
feat: Adds a resolved path for output (#80)
Browse files Browse the repository at this point in the history
* feat: Adds a resolved path for output

* fix

* fix: Add review fix

* fix: test for source not ast

* fix: review comments
  • Loading branch information
Pavithra Kodmad committed Mar 6, 2017
1 parent 4020043 commit 37a594d
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 22 deletions.
22 changes: 4 additions & 18 deletions lib/transformations/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils createLiteral should create basic literal 1`] = `
Object {
"comments": null,
"loc": null,
"regex": null,
"type": "Literal",
"value": "strintLiteral",
}
`;
exports[`utils createLiteral should create basic literal 1`] = `"\\"stringLiteral\\""`;

exports[`utils createLiteral should create boolean 1`] = `
Object {
"comments": null,
"loc": null,
"regex": null,
"type": "Literal",
"value": true,
}
`;
exports[`utils createLiteral should create boolean 1`] = `"true"`;

exports[`utils createOrUpdatePluginByName should add an object as an argument 1`] = `
"[new Plugin({
Expand Down Expand Up @@ -71,3 +55,5 @@ exports[`utils createProperty should create properties for non-literal keys 1`]
1: \\"bar\\"
}"
`;

exports[`utils getRequire should create a require statement 1`] = `"const filesys = require(\\"fs\\");"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`outputPath transforms correctly using "outputPath-0" data 1`] = `
"module.exports = {
output: {
path: path.join(__dirname, 'dist')
}
}
"
`;

exports[`outputPath transforms correctly using "outputPath-1" data 1`] = `
"const path = require('path');
module.exports = {
output: {
path: path.join(__dirname, 'dist')
}
}
"
`;

exports[`outputPath transforms correctly using "outputPath-2" data 1`] = `
"const p = require('path');
module.exports = {
output: {
path: p.join(__dirname, 'dist')
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
output: {
path: 'dist'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path');
module.exports = {
output: {
path: path.join(__dirname, 'dist')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const p = require('path');
module.exports = {
output: {
path: 'dist'
}
}
51 changes: 51 additions & 0 deletions lib/transformations/outputPath/outputPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const utils = require('../utils');

module.exports = function(j, ast) {
const literalOutputPath = ast
.find(j.ObjectExpression)
.filter(p => utils.safeTraverse(p, ['parentPath', 'value', 'key', 'name']) === 'output')
.find(j.Property)
.filter(p => utils.safeTraverse(p, ['value', 'key', 'name']) === 'path'
&& utils.safeTraverse(p, ['value', 'value', 'type']) === 'Literal');

if (literalOutputPath) {
let pathVarName = 'path';
let isPathPresent = false;
const pathDecalaration = ast
.find(j.VariableDeclarator)
.filter(p => utils.safeTraverse(p, ['value', 'init', 'callee', 'name']) === 'require')
.filter(p => utils.safeTraverse(p, ['value', 'init', 'arguments'])
&& p.value.init.arguments.reduce((isPresent, a) => {
return a.type === 'Literal' && a.value === 'path' || isPresent;
}, false));

if (pathDecalaration) {
isPathPresent = true;
pathDecalaration.forEach(p => {
pathVarName = utils.safeTraverse(p, ['value', 'id', 'name']);
});
}

literalOutputPath
.find(j.Literal)
.replaceWith(p => replaceWithPath(j, p, pathVarName));

if(!isPathPresent){
const pathRequire = utils.getRequire(j, 'path', 'path');
return ast.find(j.Program)
.replaceWith(p => j.program([].concat(pathRequire).concat(p.value.body)));
}
}
return ast;
};

function replaceWithPath(j, p, pathVarName) {
const convertedPath = j.callExpression(
j.memberExpression(
j.identifier(pathVarName),
j.identifier('join'),
false),
[j.identifier('__dirname'), p.value]);
return convertedPath;
}

5 changes: 5 additions & 0 deletions lib/transformations/outputPath/outputPath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const defineTest = require('../defineTest');

defineTest(__dirname, 'outputPath', 'outputPath-0');
defineTest(__dirname, 'outputPath', 'outputPath-1');
defineTest(__dirname, 'outputPath', 'outputPath-2');
25 changes: 24 additions & 1 deletion lib/transformations/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@ function findObjWithOneOfKeys (p, keyNames) {
}, false);
}

/*
* @function getRequire
*
* Returns constructed require symbol
* @param j — jscodeshift API
* @param { string } constName - Name of require
* @param { string } packagePath - path of required package
* @returns {NodePath} - the created ast
*/

function getRequire(j, constName, packagePath) {
return j.variableDeclaration('const', [
j.variableDeclarator(
j.identifier(constName),
j.callExpression(
j.identifier('require'),
[j.literal(packagePath)]
)
)
]);
}

module.exports = {
safeTraverse,
createProperty,
Expand All @@ -233,5 +255,6 @@ module.exports = {
findVariableToPlugin,
isType,
createLiteral,
findObjWithOneOfKeys
findObjWithOneOfKeys,
getRequire
};
13 changes: 10 additions & 3 deletions lib/transformations/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ var a = { plugs: [] }

describe('createLiteral', () => {
it('should create basic literal', () => {
const literal = utils.createLiteral(j, 'strintLiteral');
expect(literal).toMatchSnapshot();
const literal = utils.createLiteral(j, 'stringLiteral');
expect(j(literal).toSource()).toMatchSnapshot();
});
it('should create boolean', () => {
const literal = utils.createLiteral(j, 'true');
expect(literal).toMatchSnapshot();
expect(j(literal).toSource()).toMatchSnapshot();
});
});

Expand All @@ -157,4 +157,11 @@ var a = { plugs: [] }
.filter(p => utils.findObjWithOneOfKeys(p, ['a'])).size()).toEqual(1);
});
});

describe('getRequire', () => {
it('should create a require statement', () => {
const require = utils.getRequire(j, 'filesys', 'fs');
expect(j(require).toSource()).toMatchSnapshot();
});
});
});

0 comments on commit 37a594d

Please sign in to comment.