SyntaxError on spread operator, while using babel env preset

耗尽温柔 提交于 2020-02-03 11:02:12

问题


I am trying to "modernize" mern.io starter bolerplate by replacing babel es2015 and stage-0 presets with env. However, it seems that env preset does not recognize the following snippet in client/modules/Intl/IntlReducer.js:9:

import { enabledLanguages, localizationData } from '../../../Intl/setup';
import { SWITCH_LANGUAGE } from './IntlActions';

const initLocale = global.navigator && global.navigator.language || 'en';

const initialState = {
  locale: initLocale,
  enabledLanguages,
  ...(localizationData[initLocale] || {}),
// ^ 
// SyntaxError: client/modules/Intl/IntlReducer.js: Unexpected token
};


const IntlReducer = (state = initialState, action) => {
  switch (action.type) {
    case SWITCH_LANGUAGE: {
      const { type, ...actionWithoutType } = action; // eslint-disable-line
      return { ...state, ...actionWithoutType };
    }

    default:
      return state;
  }
};

export default IntlReducer;

localizationData object is populated in Intl/setup.js:56

This spread operator seems quite normal to me, I cannot spot the syntax error. Also it seems that other code preceding to this file, is being transpiled just fine, so I guess env preset is applied correctly.

What may be the reason of this failure? Was this particular flavor of spread operator not accepted from stage-0 into current version of ES? How can I investigate this issue further?

Full error message (project root path stripped):

node_modules/babel-core/lib/transformation/file/index.js:590
      throw err;
      ^

SyntaxError: client/modules/Intl/IntlReducer.js: Unexpected token (9:2)
   7 |   locale: initLocale,
   8 |   enabledLanguages,
>  9 |   ...localizationData[initLocale]
     |   ^
  10 | };
  11 | 
  12 | const IntlReducer = (state = initialState, action) => {
    at Parser.pp$5.raise (node_modules/babylon/lib/index.js:4454:13)
    at Parser.pp.unexpected (node_modules/babylon/lib/index.js:1761:8)
    at Parser.pp$3.parseIdentifier (node_modules/babylon/lib/index.js:4332:10)
    at Parser.pp$3.parsePropertyName (node_modules/babylon/lib/index.js:4156:96)
    at Parser.parsePropertyName (node_modules/babylon/lib/index.js:6229:23)
    at Parser.pp$3.parseObj (node_modules/babylon/lib/index.js:4045:12)
    at Parser.pp$3.parseExprAtom (node_modules/babylon/lib/index.js:3719:19)
    at Parser.parseExprAtom (node_modules/babylon/lib/index.js:7238:22)
    at Parser.pp$3.parseExprSubscripts (node_modules/babylon/lib/index.js:3494:19)
    at Parser.pp$3.parseMaybeUnary (node_modules/babylon/lib/index.js:3474:19)
    at Parser.pp$3.parseExprOps (node_modules/babylon/lib/index.js:3404:19)
    at Parser.pp$3.parseMaybeConditional (node_modules/babylon/lib/index.js:3381:19)
    at Parser.pp$3.parseMaybeAssign (node_modules/babylon/lib/index.js:3344:19)
    at Parser.parseMaybeAssign (node_modules/babylon/lib/index.js:6474:20)
    at Parser.pp$1.parseVar (node_modules/babylon/lib/index.js:2340:24)
    at Parser.pp$1.parseVarStatement (node_modules/babylon/lib/index.js:2169:8)

回答1:


Object spread is not in the specification yet, it is currently in stage 3, which means that the env preset won't provide this feature.

If you want to use this feature with Babel, you will need to add the babel-plugin-transform-object-rest-spread transform.

You can check the status of the ES proposals in this repo https://github.com/tc39/proposals



来源:https://stackoverflow.com/questions/48066025/syntaxerror-on-spread-operator-while-using-babel-env-preset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!