// file
var a = "a" // what if this is import statement?
// jscodeshift
export default (file, api) => {
const j = api.jscodeshift;
const root = j(file.source);
root.find(j.VariableDeclaration)
.insertBefore("use strict");
return root.toSource();
}
Run Code Online (Sandbox Code Playgroud)
如果第一行代码在文件中不同,insertBefore如何工作.例如(变量声明,导入声明)
使用jscodeshift,我该如何转换
// Some code ...
const someObj = {
x: {
foo: 3
}
};
// Some more code ...
Run Code Online (Sandbox Code Playgroud)
到
// Some code ...
const someObj = {
x: {
foo: 4,
bar: '5'
}
};
// Some more code ...
Run Code Online (Sandbox Code Playgroud)
?
我努力了
module.exports = function(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
return root
.find(j.Identifier)
.filter(path => (
path.node.name === 'someObj'
))
.replaceWith(JSON.stringify({foo: 4, bar: '5'}))
.toSource();
}
Run Code Online (Sandbox Code Playgroud)
但我最终得到的是
// Some code ...
const …Run Code Online (Sandbox Code Playgroud) 我正在创建一个转换,它将替换以下所有实例:
templateUrl: 'some/url/to/some.html'
Run Code Online (Sandbox Code Playgroud)
和
template: require('some/url/to/some.html')
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我正在改变 AngularJS 代码引入模板的方式。我将使它们全部使用 webpack 字符串加载器。
我已经开始工作了。但现在我不知道如何针对我的项目中的所有文件运行它。我在文档中看不到如何针对.js我的项目中的所有文件运行它。要运行我的转换,我当前输入:
jscodeshift ./folder/myComponent.js -t ./tools/codemodes/template.js -d -p
Run Code Online (Sandbox Code Playgroud)
此命令将针对该myComponent.js文件运行我的转换,但我希望它针对我的项目中的所有文件运行.js。我需要对当前命令进行哪些更改才能使其选择所有.js文件并对所有文件运行转换?
我的用例:我正在构建一个Yeoman生成器,该生成器可以修改TypeScript文件。类似于:
import语句Yeoman建议针对此任务使用AST解析器:
最可靠的方法是解析文件AST(抽象语法树)并对其进行编辑。
诸如jscodeshift之类的工具对于JavaScript文件而言相当简单,但它似乎不支持TypeScript。是否有任何类似的工具来解析和修改TypeScript文件的AST?
我现在发现我正在尝试replace表达,我不在乎它是什么.
在这个例子中我发现了this.state.showMenu && this.handleMouseDown部分
<a
onMouseDown={this.state.showMenu && this.handleMouseDown}
>
Run Code Online (Sandbox Code Playgroud)
我需要转换为:
<a
onMouseDown={this.state.showMenu ? this.handleMouseDown : undefined}
>
Run Code Online (Sandbox Code Playgroud)
如何在不明确重建树的情况下这样做?我只是想做点什么
path.replaceText("this.state.showMenu ? this.handleMouseDown : undefined")
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行top-level-imports此处找到的codemod: https: //github.com/mui-org/material-ui/blob/master/packages/material-ui-codemod/README.md#top-level-imports。
所以我运行了npm install -D @material-ui/codemod脚本,然后
find src -name '*.tsx' -print | xargs npx jscodeshift -t node_modules/@material-ui/codemod/lib/v4.0.0/top-level-imports.js
得到以下错误输出:
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
ERR ./components/form/TextField.tsx Transformation error (Cannot find module '@material-ui/core/es')
Error: Cannot find module '@material-ui/core/es'
at Function.Module._resolveFilename (module.js:536:15)
at Function.resolve (internal/module.js:18:19)
at transformer (.../node_modules/@material-ui/codemod/lib/v4.0.0/top-level-imports.js:29:54)
All done.
Run Code Online (Sandbox Code Playgroud)
以前有人遇到过这种情况吗?
我看到一些开发人员想知道如何在没有 CLI 的情况下运行 jscodeshift,存在一些问题,但它们非常简单,所以如果有人想使用选项有效地运行它,这里有一个示例。
const path = require('path');
const Runner = require('jscodeshift/src/Runner');
// just the paths of the files to apply the transformation
const paths = ['path/to/example1.js', 'path/to/example2.js'];
/**
* taken from
* @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L18
*/
const options = {
transform: 'path/to/transformerFile.js',
verbose: 0,
dry: false,
print: true,
babel: true,
extensions: 'js',
ignorePattern: [],
ignoreConfig: [],
runInBand: false,
silent: false,
parser: 'babel',
stdin: false
}
/**
* taken from
* @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L135
*/
Runner.run(
/^https?/.test(options.transform) ? options.transform : path.resolve(options.transform),
paths, …Run Code Online (Sandbox Code Playgroud) This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom'
我收到上述错误,而试图将整个应用程序从反应v15.6到v16.2,通过使用迁移工具从Facebook等jscodeshift。
当我运行时出现以下错误jscodeshift -t ./react-codemod/transforms/React-PropTypes-to-prop-types.js ./src
Transformation error (This experimental syntax requires enabling one of the following parser plugin(s): 'decorators-legacy, decorators' (13:0))
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索了一下,发现我必须设置--parser-configjscodeshift。我需要有关可以解决上述错误的示例 json 文件的帮助。
我正在从 Material-UI 0.x 更新到 1.0。
迁移助手的文档说要运行:
jscodeshift -t <codemod-script> <path>.
Run Code Online (Sandbox Code Playgroud)
我以前从未使用过 jscodeshift,也从未见过这种表示法,所以我想就如何使用它获得一些建议。:) 谷歌搜索jscodeshift codemod-script没有发现任何相关内容。
codemod-script需要什么?
jscodeshift ×10
javascript ×4
material-ui ×2
reactjs ×2
codemod ×1
facebook ×1
migration ×1
node.js ×1
typescript ×1
yeoman ×1