codemod-get-to-optional-member-expressions:将Lodash的get函数更改为ES7可选

  • l3_320969
    了解作者
  • 40.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-12 11:21
    上传日期
codemod获取到可选成员表达式 该存储库包含一个与一起使用的codemod脚本。 概述 codemod用对ECMAScript的本机替换对Lodash的方法的调用。 这是一种可靠访问深度嵌套对象属性的新颖方法,现在可以与。 重要的是要注意,这目前仅适用于Babel 7 。 如果使用的是Babel 6.x,则需要将项目更新为Babel 7才能使用此codemod。 const foo = get ( data , 'crate.box.present.wrapping.color' ) ; const bar = get ( data , 'crate.box.present.wrapping.color' , 'green' ) ; // :backhand_index_pointing_down: Becomes :backhand_index_pointing_down: const foo = data ?. crate ?. box ?. present ?. wrapping ?. co
codemod-get-to-optional-member-expressions-master.zip
  • codemod-get-to-optional-member-expressions-master
  • tests
  • lodash-get-to-optional-member-expressions.js
    1.5KB
  • yarn.lock
    150.2KB
  • README.md
    1.5KB
  • lodash-get-to-optional-member-expressions.js
    2.9KB
  • .gitignore
    12B
  • package.json
    766B
  • .babelrc
    134B
内容介绍
## codemod-get-to-optional-member-expressions This repository contains a codemod script for use with [JSCodeshift](https://github.com/facebook/jscodeshift). ### Overview The codemod replaces calls to Lodash's [get](https://lodash.com/docs#get) method with the native [optional chaining proposal](https://claudepache.github.io/es-optional-chaining/) for ECMAScript. This is a novel new way to reliably access deeply nested object properties, and can be used now with [@babel/plugin-proposal-optional-chaining](https://new.babeljs.io/docs/en/next/babel-plugin-proposal-optional-chaining.html). It is important to note that this will currently only work with **Babel 7**. If you are using Babel 6.x, then you will need to update your project to Babel 7 to use this codemod. ```javascript const foo = get(data, 'crate.box.present.wrapping.color'); const bar = get(data, 'crate.box.present.wrapping.color', 'green'); // 👇 Becomes 👇 const foo = data?.crate?.box?.present?.wrapping?.color; const bar = data?.crate?.box?.present?.wrapping?.color || 'green'; ``` ### Setup & Run ```sh npm install -g jscodeshift git clone https://github.com/eschaefer/codemod-get-to-optional-member-expressions.git cd codemod-get-to-optional-member-expressions/ jscodeshift -t ./lodash-get-to-optional-member-expressions.js <file(s)> ``` Use the `-d` option for a dry-run and use `-p` to print the output for comparison. Run tests for this codemod ```sh npm run test ``` ### TODO - Add support for `require`; - Add support for string literal member expressions - Add support for array paths
评论
    相关推荐