推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
hubert3

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

  •  2
     
  •   hubert3 · Jan 12, 2018 · 18593 views
    This topic created in 3220 days ago, the information mentioned may be changed or developed.
    许多 JS 开发者喜欢省略行尾的分号,让引擎自动添加。

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



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

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

    > 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
    dtysky
    Jan 12, 2018
    49
    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
    seki
    Jan 12, 2018❤️1
    50
    分号?不接受,不参与,不承认,不执行,废纸一张
    hyyou2010
    hyyou2010
    Jan 12, 2018
    51
    一直觉得不加很别扭
    ByteRan
    ByteRan
    Jan 12, 2018
    52
    @est 哔了我的眼👁
    cutiechi
    cutiechi
    Jan 12, 2018 via iPhone
    53
    上面不是写了 This section is non-normative ?
    ES 6 不是已经定稿了?
    用 ESLint 规范代码貌似可以在新一行的开始加分号解决这个问题,所以说每一个语句都加分号没必要吧。
    ob
    ob
    Jan 12, 2018 via Android
    54
    必须分号;
    xAx
    xAx
    Jan 12, 2018
    55
    爽,对 vuejs 那套烦的不得了,烧死不加分号的异教徒
    TimRChen
    TimRChen
    Jan 12, 2018
    56
    @dong3580 eslint ?
    dong3580
    dong3580
    Jan 12, 2018
    57
    @TimRChen
    是的,编译也会报,所以这该如何解决呢。。。
    TimRChen
    TimRChen
    Jan 12, 2018
    58
    @dong3580 https://eslint.org/docs/user-guide/configuring#using-eslintrecommended 按配置把"semi": ["error", "always"],去掉,然后再看看?
    banricho
    banricho
    Jan 12, 2018
    59
    认真看内容的没几个,标题党大行其道
    写不写分号是编码习惯,ESLint 配置也可以自己改
    怪 ESLint 和 Vue 的不是很懂
    rocksolid
    rocksolid
    Jan 12, 2018
    60
    @est 这是写 python 出身的么....
    Martox
    Martox
    Jan 12, 2018
    61
    我不服
    an168bang521
    an168bang521
    Jan 12, 2018
    62
    js 代码不加分号,感觉就像拉屎不擦屁股;
    2333,这个习惯,我应该是很难改了,看到没有分号的代码,总想给补全;
    libook
    libook
    Jan 12, 2018
    63
    这个帖子有些标题党。

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

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

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

    当然都是后话了。
    alian
    alian
    Jan 12, 2018
    64
    表示最近几个月都不加分号开发真的很爽。。。现在这就有点尴尬了
    dong3580
    dong3580
    Jan 12, 2018
    65
    @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
    dong3580
    Jan 12, 2018
    66
    @TimRChen
    我知道了 应该加上'semi':['error','always']
    deadEgg
    deadEgg
    Jan 12, 2018
    67
    “靠猜”这个说法不准确。

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

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

    我倒是真心不懂回车跟分号作为 EOS 在本质上到底有什么不同。
    oswuhan
    oswuhan
    Jan 12, 2018
    82
    @marcong95 没有不同
    oswuhan
    oswuhan
    Jan 12, 2018
    83
    @marcong95 怼不起,怼不起
    think2011
    think2011
    Jan 12, 2018
    84
    prettier +1
    kingcos
    kingcos
    Jan 12, 2018
    85
    还好 Swift 没有分号😂
    xy90321
    xy90321
    Jan 12, 2018
    86
    @kingcos
    所以我在 swift 和其他之间切换的时候经常就打错了
    当然,主要都是错在了顺手给 swift 加了分号
    虽然语法上加了也不算错
    huclengyue
    huclengyue
    Jan 12, 2018 via Android
    87
    @est 为什么分号不跟在每句结尾
    huclengyue
    huclengyue
    Jan 12, 2018 via Android
    88
    @mooncakejs 大部分语言都是要求缩进的只是不像 python 那么严格。。个人感觉有缩进的看着清晰。
    wspsxing
    wspsxing
    Jan 12, 2018
    89
    胜利
    DaCong
    DaCong
    Jan 12, 2018
    90
    @est #12 怎么感觉有点像 C++ 了……
    fuxkcsdn
    fuxkcsdn
    Jan 12, 2018
    91
    分号满塞!
    修改起公司那些前端的代码就恨不得统一格式化
    conn4575
    conn4575
    Jan 13, 2018 via Android
    92
    后端表示看了没分号 js 浑身难受!!
    PythonAnswer
    PythonAnswer
    Jan 13, 2018 via Android
    93
    nnd 生效后我就不写 js 了。抗议。
    jtsai
    jtsai
    Jan 13, 2018 via Android❤️1
    94
    加分号太丑了,受不了
    jtsai
    jtsai
    Jan 13, 2018 via Android
    95
    坚决不加分号到代码奔溃
    doubleflower
    doubleflower
    Jan 13, 2018
    96
    看了下和现在没什么区别,都是在 linter 可以处理的范围内
    awing
    awing
    Jan 13, 2018❤️1
    97
    话说你们说完一句话要加句号吗?
    基本上没人加
    写完一句代码也不应该加;
    robinlovemaggie
    robinlovemaggie
    May 5, 2019
    98
    python 党表示键盘上已经没有分号(;)这个键~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3558 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 206ms · UTC 10:08 · PVG 18:08 · LAX 02:08 · JFK 05:08
    ♥ Do have faith in what you're doing.