V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
hubert3
V2EX  ›  JavaScript

TC39 委员会正式写入 ES6 规格:请不要省略分号!

  •  2
     
  •   hubert3 · 2018-01-12 09:36:02 +08:00 · 16619 次点击
    这是一个创建于 2267 天前的主题,其中的信息可能已经有所发展或是发生改变。
    许多 JS 开发者喜欢省略行尾的分号,让引擎自动添加。

    现在,TC39 委员会正式写入 ES6 规格:请不要省略分号!一个主要原因是,即将进入规格的 class field 是以分号结尾,省略会有危险。另外,自动添加分号的机制是靠猜的,但软件运行不应该靠猜。
    https://github.com/tc39/ecma262/pull/1062/files
    https://weibo.com/1400854834/FDYzQvUlL
    98 条回复    2019-05-05 17:01:10 +08:00
    Sin
        1
    Sin  
       2018-01-12 09:41:33 +08:00
    分号党头顶青天
    HuHui
        2
    HuHui  
       2018-01-12 09:44:58 +08:00
    突然想起男厕尿兜上方贴的一句话:前进一小步,文明一大步。
    k9982874
        3
    k9982874  
       2018-01-12 09:45:03 +08:00 via iPhone   ❤️ 5
    vue 表示 wtf !
    joshua7v
        4
    joshua7v  
       2018-01-12 09:45:24 +08:00 via Android
    格式问题已经交给 prettier
    ctsed
        5
    ctsed  
       2018-01-12 09:45:28 +08:00 via Android
    binux
        6
    binux  
       2018-01-12 09:46:16 +08:00 via Android
    \分号党大胜利 /
    xiadd
        7
    xiadd  
       2018-01-12 09:46:28 +08:00
    我们现在写的 js 不是直接运行在浏览器里的 基本都需要编译一遍 所以没啥问题
    dong3580
        8
    dong3580  
       2018-01-12 09:47:37 +08:00
    不知道我设置了什么,一写分号就检查不通过。。。
    sobigfish
        9
    sobigfish  
       2018-01-12 09:48:24 +08:00
    你们真能折腾,偶尔写写 JavaScript 表示被 eslint 去掉;的时候很绝望(直接用项目自带的设置)
    所以到底要折腾几种写法出来?
    jlkm2010
        10
    jlkm2010  
       2018-01-12 09:50:34 +08:00
    终于要带分号了,eslint 不给写分号恶心死了
    kosilence
        11
    kosilence  
       2018-01-12 09:51:09 +08:00 via iPhone
    胜利!
    est
        12
    est  
       2018-01-12 09:51:59 +08:00   ❤️ 14



    居然没人贴这个。
    zhujinliang
        13
    zhujinliang  
       2018-01-12 09:52:45 +08:00 via iPhone
    耶✌️~
    swulling
        14
    swulling  
       2018-01-12 09:54:34 +08:00 via iPhone
    我司的代码规范就已经强制分号了
    shaonian
        15
    shaonian  
       2018-01-12 09:56:32 +08:00   ❤️ 2
    ES6 已经定稿...怎么可能写入 ES6...
    sobigfish
        16
    sobigfish  
       2018-01-12 09:56:49 +08:00
    @est #11 第一眼觉得很奇葩,多看 2 眼觉得可读性还蛮高的
    oott123
        17
    oott123  
       2018-01-12 09:57:45 +08:00 via Android
    正式?
    es6 ?
    tabris17
        18
    tabris17  
       2018-01-12 09:59:23 +08:00
    @est 异端!
    hxsf
        19
    hxsf  
       2018-01-12 09:59:54 +08:00
    只是 Open 吧,还没 Merge 呢。。。
    realwangyibo
        20
    realwangyibo  
       2018-01-12 10:00:13 +08:00
    standardjs 或成最大输家
    Vindroid
        21
    Vindroid  
       2018-01-12 10:06:26 +08:00
    @est 这写法第一次见,不没插件自动排版会很麻烦吧
    kingcc
        22
    kingcc  
       2018-01-12 10:07:54 +08:00   ❤️ 1
    正式 Are you sure ?
    ES6 Are you sure ?
    ```
    <em>This section is non-normative.</em>
    ```
    isCyan
        23
    isCyan  
       2018-01-12 10:08:52 +08:00 via Android
    最喜欢分号了,分号能让思路清晰,分号党万岁
    kingcc
        24
    kingcc  
       2018-01-12 10:11:05 +08:00   ❤️ 1
    ruanyifeng 那种标题党估计也只有楼主这种智商才会想也不想的转过来水贴
    adeweb
        25
    adeweb  
       2018-01-12 10:11:51 +08:00
    分号看着难受,上下不对称。
    CosimoZi
        26
    CosimoZi  
       2018-01-12 10:14:05 +08:00
    @est 特别想有个支持这种排版的 jetbrain 系列 ide 插件.
    或者是不是已经有了?以什么关键词搜索?
    Creabine
        27
    Creabine  
       2018-01-12 10:14:20 +08:00
    不要啊。。。雅蠛蝶。。。
    autoxbc
        28
    autoxbc  
       2018-01-12 10:17:09 +08:00
    分号党坐看好戏
    LWXYFER
        29
    LWXYFER  
       2018-01-12 10:17:14 +08:00
    @kingcc 哈哈,遇到个明白人了。大家只看标题不去 GitHub 看下真实情况。23333
    lovedebug
        30
    lovedebug  
       2018-01-12 10:36:56 +08:00
    还没有实锤,看双方博弈了
    flashback313
        31
    flashback313  
       2018-01-12 10:39:14 +08:00
    妈蛋,不写分号才好看
    sudoz
        32
    sudoz  
       2018-01-12 10:45:20 +08:00
    @est 法科,这代码是人看的?
    DOLLOR
        33
    DOLLOR  
       2018-01-12 10:50:27 +08:00
    连分号都懒得写的干嘛不用 coffeescript ?
    whypool
        34
    whypool  
       2018-01-12 10:55:50 +08:00
    分号已经成了习惯,比如这样;
    sparkle2015
        35
    sparkle2015  
       2018-01-12 11:01:31 +08:00
    ... 现在看见分号就想删掉,不能省略分号的语言不是好语言!
    zjqzxc
        36
    zjqzxc  
       2018-01-12 11:23:26 +08:00
    有段时间写 js 多,也习惯了不加分号;
    后来再写 C/PHP 的时候老是忘记分号;

    于是就成了坚定的分号党,能加分号绝不省略;
    maplerecall
        37
    maplerecall  
       2018-01-12 11:30:39 +08:00
    虽然还不是正式的,不过做为分号党心理还是暗爽的_(:3 」∠)_
    MushishiXian
        38
    MushishiXian  
       2018-01-12 11:30:46 +08:00
    分号党路过...
    tonymitcher
        39
    tonymitcher  
       2018-01-12 11:32:01 +08:00
    哈哈,分号党的胜利
    southsala
        40
    southsala  
       2018-01-12 12:04:39 +08:00
    不加分好的语言都不是好语言
    mooncakejs
        41
    mooncakejs  
       2018-01-12 12:33:47 +08:00
    @est 竟然还有人喜欢 python 的缩进。上班尺子带了没。
    Phariel
        42
    Phariel  
       2018-01-12 12:44:14 +08:00 via Android
    强迫症表示不加分号我浑身难受。。。
    ziki
        43
    ziki  
       2018-01-12 12:47:32 +08:00
    四个空格啥时候写进标准 /滑稽
    xiaojie668329
        44
    xiaojie668329  
       2018-01-12 12:49:17 +08:00 via iPhone
    我不喜欢加分号,但是 prettier 会给我加上去。。。
    lyhiving
        45
    lyhiving  
       2018-01-12 12:50:35 +08:00 via Android
    习惯加分号
    blless
        46
    blless  
       2018-01-12 12:53:22 +08:00
    @mooncakejs 缩进多了拆分函数啊 看见一个几百行的函数我就头痛
    Mitt
        47
    Mitt  
       2018-01-12 13:14:07 +08:00
    妈的 还以为不写分号是大趋势 好不容易接受了设定现在又强制写分号了
    otakustay
        48
    otakustay  
       2018-01-12 13:17:12 +08:00   ❤️ 6
    这楼里已经没一个人在认真看问题了,我贴一下关键的内容:

    > What we state here is that using ASI style is likely to lead to an inconsistent programming style in the future and in result code that requires higher cognitive load to work with.

    确实分号自动插入(可省分号)在当前是工作得 OK 的,但是这导致在新语法进来后,需要手动写分号的地方越来越多,那么你写代码的时候就需要越来越多的思考“这里能不能省分号”,变得不友好

    > The crux of the issue is that ASI makes it harder for the language to evolve. This is not an opinion but a fact. The committee has tried hard to support a semicolon-free coding style so far, but there are cases where it ’ s not feasible to do so (@bmeck repeatedly pointed to one), and there will be more in the future.

    省分号已经严重影响了新的语法的设计,当前的 Class Property 已经遇到这个问题,后面更多语法总会有各种各样的边界情况

    ---

    这里一个本质的问题其实不在于分号,而在于:ES 不把换行符当作语法的分隔符

    这一特性导致一个 ES 片段是可以写在多行里的,比如这么写:

    const a = 0
    [x].map...

    这时候没有明确的分号就会完蛋,那么在更多新语法进来的时候,就会死得更惨

    与其这样,还不如推荐大家所有该加分号的地方都加上,保持一种一致的编码形式
    dtysky
        49
    dtysky  
       2018-01-12 13:20:47 +08:00
    Error Correction

    One of the most hotly contested religious wars in the JS community (besides tabs vs. spaces) is whether to rely heavily/exclusively on ASI or not.

    Most, but not all, semicolons are optional, but the two ;s in the for ( .. ) .. loop header are required.

    On the pro side of this debate, many developers believe that ASI is a useful mechanism that allows them to write more terse (and more "beautiful") code by omitting all but the strictly required ;s (which are very few). It is often asserted that ASI makes many ;s optional, so a correctly written program without them is no different than a correctly written program with them.

    On the con side of the debate, many other developers will assert that there are too many places that can be accidental gotchas, especially for newer, less experienced developers, where unintended ;s being magically inserted change the meaning. Similarly, some developers will argue that if they omit a semicolon, it's a flat-out mistake, and they want their tools (linters, etc.) to catch it before the JS engine corrects the mistake under the covers.

    Let me just share my perspective. A strict reading of the spec implies that ASI is an "error correction" routine. What kind of error, you may ask? Specifically, a parser error. In other words, in an attempt to have the parser fail less, ASI lets it be more tolerant.

    But tolerant of what? In my view, the only way a parser error occurs is if it's given an incorrect/errored program to parse. So, while ASI is strictly correcting parser errors, the only way it can get such errors is if there were first program authoring errors -- omitting semicolons where the grammar rules require them.

    So, to put it more bluntly, when I hear someone claim that they want to omit "optional semicolons," my brain translates that claim to "I want to write the most parser-broken program I can that will still work."

    I find that to be a ludicrous position to take and the arguments of saving keystrokes and having more "beautiful code" to be weak at best.

    Furthermore, I don't agree that this is the same thing as the spaces vs tabs debate -- that it's purely cosmetic -- but rather I believe it's a fundamental question of writing code that adheres to grammar requirements vs. code that relies on grammar exceptions to just barely skate through.

    Another way of looking at it is that relying on ASI is essentially considering newlines to be significant "whitespace." Other languages like Python have true significant whitespace. But is it really appropriate to think of JavaScript as having significant newlines as it stands today?

    My take: use semicolons wherever you know they are "required," and limit your assumptions about ASI to a minimum.
    seki
        50
    seki  
       2018-01-12 13:21:07 +08:00   ❤️ 1
    分号?不接受,不参与,不承认,不执行,废纸一张
    hyyou2010
        51
    hyyou2010  
       2018-01-12 13:25:57 +08:00
    一直觉得不加很别扭
    tanranran
        52
    tanranran  
       2018-01-12 13:32:19 +08:00
    @est 哔了我的眼👁
    superchijinpeng
        53
    superchijinpeng  
       2018-01-12 13:33:01 +08:00 via iPhone
    上面不是写了 This section is non-normative ?
    ES 6 不是已经定稿了?
    用 ESLint 规范代码貌似可以在新一行的开始加分号解决这个问题,所以说每一个语句都加分号没必要吧。
    ob
        54
    ob  
       2018-01-12 13:35:35 +08:00 via Android
    必须分号;
    xAx
        55
    xAx  
       2018-01-12 13:51:47 +08:00
    爽,对 vuejs 那套烦的不得了,烧死不加分号的异教徒
    TimRChen
        56
    TimRChen  
       2018-01-12 14:14:25 +08:00
    @dong3580 eslint ?
    dong3580
        57
    dong3580  
       2018-01-12 14:16:35 +08:00
    @TimRChen
    是的,编译也会报,所以这该如何解决呢。。。
    TimRChen
        58
    TimRChen  
       2018-01-12 14:20:24 +08:00
    @dong3580 https://eslint.org/docs/user-guide/configuring#using-eslintrecommended 按配置把"semi": ["error", "always"],去掉,然后再看看?
    banricho
        59
    banricho  
       2018-01-12 14:28:46 +08:00
    认真看内容的没几个,标题党大行其道
    写不写分号是编码习惯,ESLint 配置也可以自己改
    怪 ESLint 和 Vue 的不是很懂
    rocksolid
        60
    rocksolid  
       2018-01-12 15:00:50 +08:00
    @est 这是写 python 出身的么....
    Martox
        61
    Martox  
       2018-01-12 15:03:40 +08:00
    我不服
    an168bang521
        62
    an168bang521  
       2018-01-12 15:06:57 +08:00
    js 代码不加分号,感觉就像拉屎不擦屁股;
    2333,这个习惯,我应该是很难改了,看到没有分号的代码,总想给补全;
    libook
        63
    libook  
       2018-01-12 15:07:28 +08:00
    这个帖子有些标题党。

    ES6 是 ECMA-262 6th Edition,如果后续有修改的话不知道是直接给 ES6 打补丁还是在 ES2018 做修订案。
    不过这一部分是非规范性的,只是一些建议,最终什么趋势还说不准。

    个人喜欢加分号,觉得在 ES 的语言特性方面严格使用分号是一种高可靠性方案;
    不过确实此前 ES 的语法情况,末尾分号对语言、引擎和开发程序的设计和实现都没什么实质影响,末尾加不加分号全凭习惯;一些 JS 大牛也认为分号的意义太弱而推崇末尾无分号,综合来看,这确实是合理的。

    可能在 ES 的未来发展方面,末尾分号对于语言、引擎的设计和实现造成了影响,未来 ES 对末尾分号的依赖有可能会逐渐增强。以前只需要注意几个特殊情况下特殊处理(如 standardjs 的方案),以保证末尾无分号的风格具备高可靠性;但未来随着新特性的增加,有可能这类特殊情况会越来越多,多到末尾无分号的方案失去其便利优势,到那个时候综合对比可能末尾写分号的代码风格会处于便利性和可靠性的优势地位。

    当然都是后话了。
    alian
        64
    alian  
       2018-01-12 15:23:11 +08:00
    表示最近几个月都不加分号开发真的很爽。。。现在这就有点尴尬了
    dong3580
        65
    dong3580  
       2018-01-12 15:49:33 +08:00
    @TimRChen
    看了看,我没那个配置嘛,是不是 babel-eslint 的原因呢?


    root: true,
    parser: 'babel-eslint',
    parserOptions: {
    sourceType: 'module'
    },
    env: {
    browser: true,
    commonjs: true,
    es6: true,
    jquery: true
    },
    extends: 'standard',
    plugins: ['html'],
    rules: {
    'arrow-parens': 0,
    'generator-star-spacing': 0,
    quotes: ['error', 'single']
    }
    dong3580
        66
    dong3580  
       2018-01-12 15:51:10 +08:00
    @TimRChen
    我知道了 应该加上'semi':['error','always']
    deadEgg
        67
    deadEgg  
       2018-01-12 15:59:58 +08:00
    “靠猜”这个说法不准确。

    不加分号是存在二义性,二义性是 coder 角度的,而不是 parser 角度的。

    对于 parser 来说,在你不加分号情况下得到的 AST 结构是稳定正确的。
    lzszone
        68
    lzszone  
       2018-01-12 15:59:58 +08:00   ❤️ 1
    分号党昂首挺胸,为所欲为,胡作非为,肆意妄为
    非分号党垂头丧气,无地自容,畏首畏尾,自暴自弃!!!
    xuhai951753
        69
    xuhai951753  
       2018-01-12 16:35:49 +08:00
    另外我还强烈建议把两格空格写进 ES6 标准(我不是在引战)
    kingwl
        70
    kingwl  
       2018-01-12 17:24:31 +08:00
    @deadEgg 未必 某些情况下正是因为 parser 区分不开才需要加分号
    coolcoffee
        71
    coolcoffee  
       2018-01-12 17:26:19 +08:00
    @xuhai951753 空格几个或者 tab 都没什么关系,大不了每个人用不同的缩进,提交代码的时候自动格式化就好了。
    leemove
        72
    leemove  
       2018-01-12 17:29:38 +08:00
    习惯不写分号,至少少按一次最无力的小拇指 哈哈
    oswuhan
        73
    oswuhan  
       2018-01-12 17:32:53 +08:00
    说没写分号习惯的“前端”们,你们不写 CSS 么?号称熟练安装各种 node_module 的全栈前端开发,怎么会不写 CSS ?
    vinsony
        74
    vinsony  
       2018-01-12 17:58:29 +08:00
    好不容易习惯了不加~~
    skyadmin
        75
    skyadmin  
       2018-01-12 18:33:23 +08:00
    @oswuhan 你知道 stylus 吗……
    madNeal
        76
    madNeal  
       2018-01-12 18:38:17 +08:00
    太赞了
    hr6r
        77
    hr6r  
       2018-01-12 18:40:17 +08:00
    i dissent
    nosay
        78
    nosay  
       2018-01-12 18:48:06 +08:00
    不加分号浑身难受
    oswuhan
        79
    oswuhan  
       2018-01-12 19:03:23 +08:00
    @skyadmin 今天撸了一天代码有点累,咱们立场不对立就歇歇。我只是想嘲讽哪些拿熟练安装第三方库以替代学习前端基础内容的人。
    XueSeason
        80
    XueSeason  
       2018-01-12 19:32:52 +08:00   ❤️ 1
    marcong95
        81
    marcong95  
       2018-01-12 20:46:49 +08:00 via Android
    @oswuhan 所以我也想嘲讽一下你,css、sass、stylus 在「前端的基础内容」这一层面上并没有什么区别。难道 npm 能帮我把样式写好?你这嘲讽的点不太好理解。

    我倒是真心不懂回车跟分号作为 EOS 在本质上到底有什么不同。
    oswuhan
        82
    oswuhan  
       2018-01-12 21:11:12 +08:00
    @marcong95 没有不同
    oswuhan
        83
    oswuhan  
       2018-01-12 21:14:04 +08:00
    @marcong95 怼不起,怼不起
    think2011
        84
    think2011  
       2018-01-12 21:27:57 +08:00
    prettier +1
    kingcos
        85
    kingcos  
       2018-01-12 21:38:29 +08:00
    还好 Swift 没有分号😂
    xy90321
        86
    xy90321  
       2018-01-12 21:46:11 +08:00
    @kingcos
    所以我在 swift 和其他之间切换的时候经常就打错了
    当然,主要都是错在了顺手给 swift 加了分号
    虽然语法上加了也不算错
    huclengyue
        87
    huclengyue  
       2018-01-12 22:06:48 +08:00 via Android
    @est 为什么分号不跟在每句结尾
    huclengyue
        88
    huclengyue  
       2018-01-12 22:09:45 +08:00 via Android
    @mooncakejs 大部分语言都是要求缩进的只是不像 python 那么严格。。个人感觉有缩进的看着清晰。
    wspsxing
        89
    wspsxing  
       2018-01-12 22:30:23 +08:00
    胜利
    DaCong
        90
    DaCong  
       2018-01-12 22:37:28 +08:00
    @est #12 怎么感觉有点像 C++ 了……
    fuxkcsdn
        91
    fuxkcsdn  
       2018-01-12 22:45:59 +08:00
    分号满塞!
    修改起公司那些前端的代码就恨不得统一格式化
    conn4575
        92
    conn4575  
       2018-01-13 03:22:15 +08:00 via Android
    后端表示看了没分号 js 浑身难受!!
    PythonAnswer
        93
    PythonAnswer  
       2018-01-13 04:40:02 +08:00 via Android
    nnd 生效后我就不写 js 了。抗议。
    jtsai
        94
    jtsai  
       2018-01-13 08:24:22 +08:00 via Android   ❤️ 1
    加分号太丑了,受不了
    jtsai
        95
    jtsai  
       2018-01-13 08:46:36 +08:00 via Android
    坚决不加分号到代码奔溃
    doubleflower
        96
    doubleflower  
       2018-01-13 09:36:33 +08:00
    看了下和现在没什么区别,都是在 linter 可以处理的范围内
    awing
        97
    awing  
       2018-01-13 16:29:49 +08:00   ❤️ 1
    话说你们说完一句话要加句号吗?
    基本上没人加
    写完一句代码也不应该加;
    robinlovemaggie
        98
    robinlovemaggie  
       2019-05-05 17:01:10 +08:00
    python 党表示键盘上已经没有分号(;)这个键~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4268 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 347ms · UTC 01:02 · PVG 09:02 · LAX 18:02 · JFK 21:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.