Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds a resolved path for output #80

Merged
merged 5 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion lib/transformations/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object {
"loc": null,
"regex": null,
"type": "Literal",
"value": "strintLiteral",
"value": "stringLiteral",
}
`;

Expand Down Expand Up @@ -71,3 +71,47 @@ exports[`utils createProperty should create properties for non-literal keys 1`]
1: \\"bar\\"
}"
`;

exports[`utils getRequire should create a require statement 1`] = `
Object {
"comments": null,
"declarations": Array [
Object {
"comments": null,
"id": Object {
"comments": null,
"loc": null,
"name": "filesys",
"type": "Identifier",
"typeAnnotation": null,
},
"init": Object {
"arguments": Array [
Object {
"comments": null,
"loc": null,
"regex": null,
"type": "Literal",
"value": "fs",
},
],
"callee": Object {
"comments": null,
"loc": null,
"name": "require",
"type": "Identifier",
"typeAnnotation": null,
},
"comments": null,
"loc": null,
"type": "CallExpression",
},
"loc": null,
"type": "VariableDeclarator",
},
],
"kind": "const",
"loc": null,
"type": "VariableDeclaration",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`outputPath transforms correctly using "outputPath-0" data 1`] = `
"const path = require('path');
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')
}
}
"
`;
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test where path is imported as a variable with a different name?

const p = require('path') should also work I guess.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, what is path is required in the file but output doesnnt use it? Need to add some more checks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this.

}
}
34 changes: 34 additions & 0 deletions lib/transformations/outputPath/outputPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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) {
literalOutputPath.find(j.Literal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the .find() be also a new line?

.replaceWith(p => replaceWithPath(j, p));

const isPathPresent = ast
.find(j.VariableDeclarator)
.filter(p => utils.safeTraverse(p, ['value', 'id', 'name']) === 'path').size();
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) {
const convertedPath = j.callExpression(
j.memberExpression(
j.identifier('path'),
j.identifier('join'),
false),
[j.identifier('__dirname'), p.value]);
return convertedPath;
}

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

defineTest(__dirname, 'outputPath', 'outputPath-0');
defineTest(__dirname, 'outputPath', 'outputPath-1');
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
};
9 changes: 8 additions & 1 deletion lib/transformations/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var a = { plugs: [] }

describe('createLiteral', () => {
it('should create basic literal', () => {
const literal = utils.createLiteral(j, 'strintLiteral');
const literal = utils.createLiteral(j, 'stringLiteral');
expect(literal).toMatchSnapshot();
});
it('should create boolean', () => {
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(require).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably create snapshot for the source code. Right now it's for AST.

expect(require.toSource()).toMatchSnapshot()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragments dont actually have .toSource() on them. :( Will try to figure out if this is possible some other way. I think I should be able to convert it to a node somehow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to wrap it in a j()

});
});
});