标签: jscodeshift

如何使用jscodeshift在文件的开头插入一行

https://astexplorer.net/#/gist/ad90272020dd0bfb15619d93cca81b66/28d3cf7178271f4f99b10bc9352daa873c2f2b20

// 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如何工作.例如(变量​​声明,导入声明)

javascript abstract-syntax-tree jscodeshift

8
推荐指数
1
解决办法
1575
查看次数

jscodeshift 更改对象文字值

使用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)

javascript jscodeshift

6
推荐指数
1
解决办法
2083
查看次数

如何对所有文件运行 jscodeshift 转换?

我正在创建一个转换,它将替换以下所有实例:

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文件并对所有文件运行转换?

jscodeshift

6
推荐指数
1
解决办法
5403
查看次数

如何解析,修改和重新生成TypeScript文件的AST(如jscodeshift)?

我的用例:我正在构建一个Yeoman生成器,该生成器可以修改TypeScript文件。类似于:

  • 添加import语句
  • 将组件导入AngularJS模块

Yeoman建议针对此任务使用AST解析器:

最可靠的方法是解析文件AST(抽象语法树)并对其进行编辑。

诸如jscodeshift之类的工具对于JavaScript文件而言相当简单,但它似乎不支持TypeScript。是否有任何类似的工具来解析和修改TypeScript文件的AST?

yeoman typescript jscodeshift

6
推荐指数
1
解决办法
825
查看次数

如何用刚刚解析的javascript(字符串)替换AST中的路径?

https://astexplorer.net/#/gist/70df1bc56b9ee73d19fc949d2ef829ed/7e14217fd8510f0bf83f3372bf08454b7617bce1

我现在发现我正在尝试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)

abstract-syntax-tree jscodeshift codemod

6
推荐指数
1
解决办法
508
查看次数

运行material-ui codemod时无法找到模块'@material-ui/core/es'错误

我正在尝试运行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)

以前有人遇到过这种情况吗?

material-ui jscodeshift

6
推荐指数
1
解决办法
519
查看次数

如何从节点 api 运行 jscodeshift?

我看到一些开发人员想知道如何在没有 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)

javascript jscodeshift

6
推荐指数
0
解决办法
594
查看次数

此实验性语法需要启用解析器插件:'exportDefaultFrom'

This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom'

我收到上述错误,而试图将整个应用程序从反应v15.6v16.2,通过使用迁移工具从Facebook等jscodeshift

javascript facebook reactjs jscodeshift

5
推荐指数
1
解决办法
6413
查看次数

转换错误...解析器插件:'decorators-legacy,decorators'

当我运行时出现以下错误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 文件的帮助。

jscodeshift

5
推荐指数
1
解决办法
9218
查看次数

Material-UI 迁移助手:codemod-script?

我正在从 Material-UI 0.x 更新到 1.0。

迁移助手的文档说要运行:

 jscodeshift -t <codemod-script> <path>. 
Run Code Online (Sandbox Code Playgroud)

我以前从未使用过 jscodeshift,也从未见过这种表示法,所以我想就如何使用它获得一些建议。:) 谷歌搜索jscodeshift codemod-script没有发现任何相关内容。

codemod-script需要什么?

migration node.js reactjs material-ui jscodeshift

4
推荐指数
1
解决办法
789
查看次数