diff --git a/src/babel/helpers/parse.js b/src/babel/helpers/parse.js index 67984af0aa..89001810d0 100644 --- a/src/babel/helpers/parse.js +++ b/src/babel/helpers/parse.js @@ -13,6 +13,7 @@ export default function (opts, code, callback) { allowReturnOutsideFunction: opts.looseModules, ecmaVersion: 6, strictMode: opts.strictMode, + sourceType: opts.sourceType, onComment: comments, locations: true, features: opts.features || {}, @@ -21,8 +22,8 @@ export default function (opts, code, callback) { ranges: true }; - parseOpts.plugins.flow = true; parseOpts.plugins.jsx = true; + parseOpts.plugins.flow = true; var ast = acorn.parse(code, parseOpts); diff --git a/src/babel/transformation/templates/.babelrc b/src/babel/transformation/templates/.babelrc index c7e124c630..ad84f4d5b9 100644 --- a/src/babel/transformation/templates/.babelrc +++ b/src/babel/transformation/templates/.babelrc @@ -1,5 +1,4 @@ { "blacklist": ["useStrict", "es6.blockScoping", "regenerator"], - "loose": ["es6.modules"], - "breakConfig": true + "loose": ["es6.modules"] } diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index d322ed26e4..2895f86992 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -180,7 +180,7 @@ class ClassTransformer { for (var i = 0; i < classBody.length; i++) { var node = classBody[i]; if (t.isMethodDefinition(node)) { - var isConstructor = (!node.computed && t.isIdentifier(node.key, { name: "constructor" })) || t.isLiteral(node.key, { value: "constructor" }); + var isConstructor = node.kind === "constructor"; if (isConstructor) this.verifyConstructor(classBodyPaths[i]); var replaceSupers = new ReplaceSupers({ @@ -332,10 +332,6 @@ class ClassTransformer { */ pushConstructor(method: { type: "MethodDefinition" }) { - if (method.kind) { - throw this.file.errorWithNode(method, messages.get("classesIllegalConstructorKind")); - } - var construct = this.constructor; var fn = method.value; diff --git a/src/babel/traversal/index.js b/src/babel/traversal/index.js index c36162f7df..fa68811939 100644 --- a/src/babel/traversal/index.js +++ b/src/babel/traversal/index.js @@ -37,23 +37,15 @@ traverse.node = function (node, opts, scope, state, parentPath) { } }; +const CLEAR_KEYS = [ + "trailingComments", "leadingComments", "_declarations", "extendedRange", + "_paths", "tokens", "range", "start", "end", "loc", "raw" +]; + function clearNode(node) { - node._declarations = null; - node.extendedRange = null; - node._paths = null; - node.tokens = null; - node.range = null; - node.start = null; - node.end = null; - node.loc = null; - node.raw = null; - - if (Array.isArray(node.trailingComments)) { - clearComments(node.trailingComments); - } - - if (Array.isArray(node.leadingComments)) { - clearComments(node.leadingComments); + for (var i = 0; i < CLEAR_KEYS.length; i++) { + var key = CLEAR_KEYS[i]; + if (node[key] != null) node[key] = null; } for (var key in node) { diff --git a/vendor/acorn/acorn.js b/vendor/acorn/acorn.js index 4e0ceb83f0..9c5c8c6dcc 100644 --- a/vendor/acorn/acorn.js +++ b/vendor/acorn/acorn.js @@ -51,6 +51,8 @@ // mode, the set of reserved words, support for getters and // setters and other features. ecmaVersion: 5, + // Source type ("script" or "module") for different semantics + sourceType: "script", // `onInsertedSemicolon` can be a callback that will be called // when a semicolon is automatically inserted. It will be passed // th position of the comma as an offset, and if `locations` is @@ -347,6 +349,7 @@ kw("with"); kw("new", beforeExpr); kw("this"); + kw("super"); kw("class"); kw("extends", beforeExpr); kw("export"); @@ -421,6 +424,10 @@ var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield"); + // ECMAScript 6 reserved words. + + var isReservedWord6 = makePredicate("enum await"); + // The forbidden variable names in strict mode. var isStrictBadIdWord = makePredicate("eval arguments"); @@ -431,7 +438,7 @@ var isEcma5AndLessKeyword = makePredicate(ecma5AndLessKeywords); - var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield"); + var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield super"); // ## Character categories @@ -548,6 +555,7 @@ this.loadPlugins(this.options.plugins); this.sourceFile = this.options.sourceFile || null; this.isKeyword = this.options.ecmaVersion >= 6 ? isEcma6Keyword : isEcma5AndLessKeyword; + this.isReservedWord = this.options.ecmaVersion === 3 ? isReservedWord3 : this.options.ecmaVersion === 5 ? isReservedWord5 : isReservedWord6; this.input = String(input); // Set up token state @@ -583,9 +591,11 @@ this.context = [tc.b_stat]; this.exprAllowed = true; - // Flags to track whether we are in strict mode, a function, a - // generator. - this.strict = this.inFunction = this.inGenerator = false; + // Figure out if it's a module code. + this.inModule = this.options.sourceType === "module"; + this.strict = options.strictMode === false ? false : this.inModule; + // Flags to track whether we are in a function, a generator. + this.inFunction = this.inGenerator = this.inAsync = false; // Labels in scope. this.labels = []; @@ -623,6 +633,25 @@ this.nextToken(); }; + var STATE_KEYS = ["lastTokStartLoc", "lastTokEndLoc", "lastTokStart", "lastTokEnd", "startLoc", "endLoc", "start", "pos", "end", "type", "value"]; + + pp.getState = function () { + var state = {}; + for (var i = 0; i < STATE_KEYS.length; i++) { + var key = STATE_KEYS[i]; + state[key] = this[key]; + } + return state; + }; + + pp.lookahead = function() { + var old = this.getState(); + this.next(); + var curr = this.getState(); + for (var key in old) this[key] = old[key]; + return curr; + }; + pp.getToken = function() { this.next(); return new Token(this); @@ -964,6 +993,7 @@ } if (next == 33 && code == 60 && this.input.charCodeAt(this.pos + 2) == 45 && this.input.charCodeAt(this.pos + 3) == 45) { + if (this.inModule) unexpected(); // `