Skip to content

Commit 37a594d

Browse files
author
Pavithra Kodmad
authored
feat: Adds a resolved path for output (#80)
* feat: Adds a resolved path for output * fix * fix: Add review fix * fix: test for source not ast * fix: review comments
1 parent 4020043 commit 37a594d

File tree

9 files changed

+141
-22
lines changed

9 files changed

+141
-22
lines changed

lib/transformations/__snapshots__/utils.test.js.snap

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`utils createLiteral should create basic literal 1`] = `
4-
Object {
5-
"comments": null,
6-
"loc": null,
7-
"regex": null,
8-
"type": "Literal",
9-
"value": "strintLiteral",
10-
}
11-
`;
3+
exports[`utils createLiteral should create basic literal 1`] = `"\\"stringLiteral\\""`;
124

13-
exports[`utils createLiteral should create boolean 1`] = `
14-
Object {
15-
"comments": null,
16-
"loc": null,
17-
"regex": null,
18-
"type": "Literal",
19-
"value": true,
20-
}
21-
`;
5+
exports[`utils createLiteral should create boolean 1`] = `"true"`;
226

237
exports[`utils createOrUpdatePluginByName should add an object as an argument 1`] = `
248
"[new Plugin({
@@ -71,3 +55,5 @@ exports[`utils createProperty should create properties for non-literal keys 1`]
7155
1: \\"bar\\"
7256
}"
7357
`;
58+
59+
exports[`utils getRequire should create a require statement 1`] = `"const filesys = require(\\"fs\\");"`;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`outputPath transforms correctly using "outputPath-0" data 1`] = `
4+
"module.exports = {
5+
output: {
6+
path: path.join(__dirname, 'dist')
7+
}
8+
}
9+
"
10+
`;
11+
12+
exports[`outputPath transforms correctly using "outputPath-1" data 1`] = `
13+
"const path = require('path');
14+
module.exports = {
15+
output: {
16+
path: path.join(__dirname, 'dist')
17+
}
18+
}
19+
"
20+
`;
21+
22+
exports[`outputPath transforms correctly using "outputPath-2" data 1`] = `
23+
"const p = require('path');
24+
module.exports = {
25+
output: {
26+
path: p.join(__dirname, 'dist')
27+
}
28+
}
29+
"
30+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
output: {
3+
path: 'dist'
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require('path');
2+
module.exports = {
3+
output: {
4+
path: path.join(__dirname, 'dist')
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const p = require('path');
2+
module.exports = {
3+
output: {
4+
path: 'dist'
5+
}
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const utils = require('../utils');
2+
3+
module.exports = function(j, ast) {
4+
const literalOutputPath = ast
5+
.find(j.ObjectExpression)
6+
.filter(p => utils.safeTraverse(p, ['parentPath', 'value', 'key', 'name']) === 'output')
7+
.find(j.Property)
8+
.filter(p => utils.safeTraverse(p, ['value', 'key', 'name']) === 'path'
9+
&& utils.safeTraverse(p, ['value', 'value', 'type']) === 'Literal');
10+
11+
if (literalOutputPath) {
12+
let pathVarName = 'path';
13+
let isPathPresent = false;
14+
const pathDecalaration = ast
15+
.find(j.VariableDeclarator)
16+
.filter(p => utils.safeTraverse(p, ['value', 'init', 'callee', 'name']) === 'require')
17+
.filter(p => utils.safeTraverse(p, ['value', 'init', 'arguments'])
18+
&& p.value.init.arguments.reduce((isPresent, a) => {
19+
return a.type === 'Literal' && a.value === 'path' || isPresent;
20+
}, false));
21+
22+
if (pathDecalaration) {
23+
isPathPresent = true;
24+
pathDecalaration.forEach(p => {
25+
pathVarName = utils.safeTraverse(p, ['value', 'id', 'name']);
26+
});
27+
}
28+
29+
literalOutputPath
30+
.find(j.Literal)
31+
.replaceWith(p => replaceWithPath(j, p, pathVarName));
32+
33+
if(!isPathPresent){
34+
const pathRequire = utils.getRequire(j, 'path', 'path');
35+
return ast.find(j.Program)
36+
.replaceWith(p => j.program([].concat(pathRequire).concat(p.value.body)));
37+
}
38+
}
39+
return ast;
40+
};
41+
42+
function replaceWithPath(j, p, pathVarName) {
43+
const convertedPath = j.callExpression(
44+
j.memberExpression(
45+
j.identifier(pathVarName),
46+
j.identifier('join'),
47+
false),
48+
[j.identifier('__dirname'), p.value]);
49+
return convertedPath;
50+
}
51+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const defineTest = require('../defineTest');
2+
3+
defineTest(__dirname, 'outputPath', 'outputPath-0');
4+
defineTest(__dirname, 'outputPath', 'outputPath-1');
5+
defineTest(__dirname, 'outputPath', 'outputPath-2');

lib/transformations/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,28 @@ function findObjWithOneOfKeys (p, keyNames) {
224224
}, false);
225225
}
226226

227+
/*
228+
* @function getRequire
229+
*
230+
* Returns constructed require symbol
231+
* @param j — jscodeshift API
232+
* @param { string } constName - Name of require
233+
* @param { string } packagePath - path of required package
234+
* @returns {NodePath} - the created ast
235+
*/
236+
237+
function getRequire(j, constName, packagePath) {
238+
return j.variableDeclaration('const', [
239+
j.variableDeclarator(
240+
j.identifier(constName),
241+
j.callExpression(
242+
j.identifier('require'),
243+
[j.literal(packagePath)]
244+
)
245+
)
246+
]);
247+
}
248+
227249
module.exports = {
228250
safeTraverse,
229251
createProperty,
@@ -233,5 +255,6 @@ module.exports = {
233255
findVariableToPlugin,
234256
isType,
235257
createLiteral,
236-
findObjWithOneOfKeys
258+
findObjWithOneOfKeys,
259+
getRequire
237260
};

lib/transformations/utils.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ var a = { plugs: [] }
136136

137137
describe('createLiteral', () => {
138138
it('should create basic literal', () => {
139-
const literal = utils.createLiteral(j, 'strintLiteral');
140-
expect(literal).toMatchSnapshot();
139+
const literal = utils.createLiteral(j, 'stringLiteral');
140+
expect(j(literal).toSource()).toMatchSnapshot();
141141
});
142142
it('should create boolean', () => {
143143
const literal = utils.createLiteral(j, 'true');
144-
expect(literal).toMatchSnapshot();
144+
expect(j(literal).toSource()).toMatchSnapshot();
145145
});
146146
});
147147

@@ -157,4 +157,11 @@ var a = { plugs: [] }
157157
.filter(p => utils.findObjWithOneOfKeys(p, ['a'])).size()).toEqual(1);
158158
});
159159
});
160+
161+
describe('getRequire', () => {
162+
it('should create a require statement', () => {
163+
const require = utils.getRequire(j, 'filesys', 'fs');
164+
expect(j(require).toSource()).toMatchSnapshot();
165+
});
166+
});
160167
});

0 commit comments

Comments
 (0)