Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(putout) add support of jscodeshift codemods
- Loading branch information
1 parent
b16c31a
commit 1b9ef88
Showing
9 changed files
with
134 additions
and
3 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
File renamed without changes.
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,74 @@ | ||
'use strict'; | ||
|
||
const once = require('once'); | ||
const print = require('../print'); | ||
const getPositions = require('../get-positions-by-diff'); | ||
|
||
const {isArray} = Array; | ||
const {assign} = Object; | ||
|
||
const getMessage = (a) => a.replace('/', ': '); | ||
const parsePlugin = (a) => { | ||
if (!isArray(a)) | ||
return [ | ||
a, | ||
getMessage(a), | ||
]; | ||
|
||
const [name, message] = a; | ||
|
||
return [ | ||
name, | ||
getMessage(message), | ||
]; | ||
}; | ||
|
||
const getJScodeshift = once(() => require('jscodeshift')); | ||
|
||
const wrapCodeShift = () => { | ||
const j = getJScodeshift(); | ||
|
||
const fixedJscodeshift = (a) => { | ||
return assign(j(a), { | ||
toSource() { | ||
return print(this.__paths[0].value); | ||
}, | ||
}); | ||
}; | ||
|
||
return assign(fixedJscodeshift, j); | ||
}; | ||
|
||
module.exports = ({fix, ast, jscodeshiftPlugins}) => { | ||
const places = []; | ||
|
||
if (!jscodeshiftPlugins) | ||
return places; | ||
|
||
const jscodeshift = wrapCodeShift(ast); | ||
|
||
let oldCode = print(ast); | ||
for (const plugin of jscodeshiftPlugins) { | ||
const [pluginName, message] = parsePlugin(plugin); | ||
const fn = require(pluginName); | ||
|
||
const newCode = fn({source: ast}, {jscodeshift}); | ||
|
||
if (!fix && newCode !== oldCode) { | ||
const positions = getPositions(oldCode, newCode); | ||
const rule = `jscodeshift/${pluginName}`; | ||
|
||
oldCode = newCode; | ||
|
||
for (const position of positions) | ||
places.push({ | ||
rule, | ||
message, | ||
position, | ||
}); | ||
} | ||
} | ||
|
||
return places; | ||
}; | ||
|
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,4 @@ | ||
async function promise() { | ||
const helloResult = await hello(); | ||
return world(helloResult); | ||
} |
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,3 @@ | ||
function promise() { | ||
return hello().then(world); | ||
} |
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