-
-
Notifications
You must be signed in to change notification settings - Fork 597
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
} | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the |
||
.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; | ||
} | ||
|
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'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fragments dont actually have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to wrap it in a |
||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this.