V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
magicdawn
V2EX  ›  Node.js

分享一个代码格式化工具 esformatter 以及一个小插件

  •  
  •   magicdawn · 2016-05-20 21:55:07 +08:00 · 3356 次点击
    这是一个创建于 2891 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原帖发在 CNodejs.org https://cnodejs.org/topic/573f17b3fcf698421d20371c


    esformatter 使用了 babylon 作为 parser, 支持 async function / es6 module import 等等啊, 比较好使 这里写了一个小插件 esformatter-curly https://github.com/magicdawn/esformatter-curly 例如下面的代码

    import multiline

    import {
      x as X,
      y as Y
    } from 'utils'
    

    会被格式化成一行(默认)

    import { x as X, y as Y } from 'utils'
    

    使用这个 esformatter-curly 可以避免这个现象, 如果原来是多行的, 使用这个插件之后, 给你保留多行

    inline object

    像下面这个对象字面两, 会被格式化成多行(默认)

    var o = { x: 'hello', y: 'world' }
    

    格式化成

    var o = {
      x: 'hello',
      y: 'world'
    }
    

    有了这个插件之后可以避免这种状况, 如果你原来是单行的, 使用这个插件, 检测到是单行的, 就给你保留

    插件地址 https://github.com/magicdawn/esformatter-curly 求 star, 另这个 esformatter 真的比较强大, 自己写插件可以 limit linebreak indent whitespace 等等... 写点自定义的插件比较好玩...

    2 条回复    2016-05-20 22:52:40 +08:00
    BOYPT
        1
    BOYPT  
       2016-05-20 22:45:47 +08:00
    atom 里面早就用上了
    magicdawn
        2
    magicdawn  
    OP
       2016-05-20 22:52:40 +08:00
    @BOYPT 后续我会分享一下他的文档, 以及 plugin 的写法~如果有现有翻译的, 请告诉我一声, 免得重复劳动
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1788 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:30 · PVG 00:30 · LAX 09:30 · JFK 12:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.