-
-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Pavithra Kodmad
authored
Mar 6, 2017
1 parent
4020043
commit 37a594d
Showing
9 changed files
with
141 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
lib/transformations/outputPath/__snapshots__/outputPath.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
" | ||
`; |
5 changes: 5 additions & 0 deletions
5
lib/transformations/outputPath/__testfixtures__/outputPath-0.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
output: { | ||
path: 'dist' | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
lib/transformations/outputPath/__testfixtures__/outputPath-1.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
lib/transformations/outputPath/__testfixtures__/outputPath-2.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const p = require('path'); | ||
module.exports = { | ||
output: { | ||
path: 'dist' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters