Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make
injectImports
& injectRootOptions
work for .vue
files (
- Loading branch information
Showing
7 changed files
with
115 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = function injectImports (fileInfo, api, { imports }) { | ||
const j = api.jscodeshift | ||
const root = j(fileInfo.source) | ||
|
||
const toImportAST = i => j(`${i}\n`).nodes()[0].program.body[0] | ||
const toImportHash = node => JSON.stringify({ | ||
specifiers: node.specifiers.map(s => s.local.name), | ||
source: node.source.raw | ||
}) | ||
|
||
const declarations = root.find(j.ImportDeclaration) | ||
const importSet = new Set(declarations.nodes().map(toImportHash)) | ||
const nonDuplicates = node => !importSet.has(toImportHash(node)) | ||
|
||
const importASTNodes = imports.map(toImportAST).filter(nonDuplicates) | ||
|
||
if (declarations.length) { | ||
declarations | ||
.at(-1) | ||
// a tricky way to avoid blank line after the previous import | ||
.forEach(({ node }) => delete node.loc) | ||
.insertAfter(importASTNodes) | ||
} else { | ||
// no pre-existing import declarations | ||
root.get().node.program.body.unshift(...importASTNodes) | ||
} | ||
|
||
return root.toSource() | ||
} |
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,27 @@ | ||
module.exports = function injectOptions (fileInfo, api, { injections }) { | ||
const j = api.jscodeshift | ||
const root = j(fileInfo.source) | ||
|
||
const toPropertyAST = i => { | ||
return j(`({${i}})`).nodes()[0].program.body[0].expression.properties[0] | ||
} | ||
|
||
const properties = root | ||
.find(j.NewExpression, { | ||
callee: { name: 'Vue' }, | ||
arguments: [{ type: 'ObjectExpression' }] | ||
}) | ||
.map(path => path.get('arguments', 0)) | ||
.get() | ||
.node | ||
.properties | ||
|
||
const toPropertyHash = p => `${p.key.name}: ${j(p.value).toSource()}` | ||
const propertySet = new Set(properties.map(toPropertyHash)) | ||
const nonDuplicates = p => !propertySet.has(toPropertyHash(p)) | ||
|
||
// inject at index length - 1 as it's usually the render fn | ||
properties.splice(-1, 0, ...injections.map(toPropertyAST).filter(nonDuplicates)) | ||
|
||
return root.toSource() | ||
} |
This file was deleted.
Oops, something went wrong.
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 jscodeshift = require('jscodeshift') | ||
const adapt = require('vue-jscodeshift-adapter') | ||
|
||
module.exports = function runCodemod (transform, fileInfo, options) { | ||
return adapt(transform)(fileInfo, { jscodeshift }, options || {}) | ||
} |
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