[Pkg-javascript-commits] [node-acorn-object-spread] 01/04: New upstream version 5.1.2

Julien Puydt julien.puydt at laposte.net
Sat Feb 17 23:06:51 UTC 2018


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-acorn-object-spread.

commit 6ae923e2303544a3a51b8f8e23caf5106c230839
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun Feb 18 00:04:59 2018 +0100

    New upstream version 5.1.2
---
 CHANGELOG.md                |  17 +++
 README.md                   |  46 +------
 inject.js                   | 108 +++++++--------
 package.json                |   4 +-
 test/tests-object-spread.js | 319 +-------------------------------------------
 5 files changed, 76 insertions(+), 418 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e14cba..39004fe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,22 @@
 # acorn5-object-spread changelog
 
+## 5.1.1
+
+* Backport check for default values from acorn 5.3.0
+
+## 5.1.0
+
+* Make plugin compatible with acorn 5.3.x
+
+## 5.0.0
+
+* Require acorn 5.2.x
+
+## 4.0.0
+
+* Remove support for complex rest properties since they are forbidded by the
+  spec
+
 ## 3.1.0
 
 * Support complex rest properties like `{...{a = 5, ...as}}`
diff --git a/README.md b/README.md
index f8e9777..35f8e68 100644
--- a/README.md
+++ b/README.md
@@ -2,48 +2,4 @@
 
 [![NPM version](https://img.shields.io/npm/v/acorn5-object-spread.svg)](https://www.npmjs.org/package/acorn5-object-spread)
 
-This is plugin for [Acorn](http://marijnhaverbeke.nl/acorn/) - a tiny, fast JavaScript parser, written completely in JavaScript.
-
-It implements support for spread and rest properties as defined in the stage 3 proposal [Object Rest/Spread Properties for ECMAScript](https://github.com/tc39/proposal-object-rest-spread).
-
-## Usage
-
-You can use this module directly in order to get an Acorn instance with the plugin installed:
-
-```javascript
-var acorn = require('acorn5-object-spread');
-```
-
-Or you can use `inject.js` for injecting the plugin into your own version of Acorn like this:
-
-```javascript
-var acorn = require('acorn5-object-spread/inject')(require('./custom-acorn'));
-```
-
-Then, use the `plugins` option whenever you need to support object spread or rest while parsing:
-
-```javascript
-var ast = acorn.parse(code, {
-  plugins: { objectSpread: true }
-});
-```
-
-## Differences to acorn-object-rest-spread
-
-[acorn-object-rest-spread](https://github.com/victor-homyakov/acorn-object-rest-spread)
-is another acorn plugin implementing the same spec. There are some differences, though:
-
-* acorn-object-rest-spread overwrites acorn`s `parseObj` with a modified copy from acorn 4,
-  so that an acorn instance with that plugin cannot for example parse `({async, foo})`
-* acorn-object-rest-spread emits `SpreadElement`s with a
-  [non-standard](https://github.com/estree/estree/blob/master/es2015.md#expressions)
-  `value` property
-* acorn-object-rest-spread emits `SpreadElement`s in arrow function argument patterns
-  and nested object patterns were it should emit `RestElement`s
-* acorn-object-rest-spread doesn't check for invalid trailing commas in rest properties
-
-## License
-
-This plugin is issued under the [MIT license](./LICENSE).
-
-With <3 by UXtemple.
+Since spread and rest properties are part of ECMAScript 2018, acorn now supports them out of the box. Just make sure that you use acorn >= 5.4.1 and set `ecmaVersion` >= 9. This plugin is deprecated.
diff --git a/inject.js b/inject.js
index 5b6abcc..2f95306 100644
--- a/inject.js
+++ b/inject.js
@@ -1,61 +1,11 @@
 'use strict';
 
 module.exports = function(acorn) {
-  if (acorn.version.substr(0, 1) !== "5") {
-    throw new Error("Unsupported acorn version " + acorn.version + ", please use acorn 5");
+  let acornVersion = acorn.version.match(/^5\.(\d+)\./)
+  if (!acornVersion || Number(acornVersion[1]) < 2) {
+    throw new Error("Unsupported acorn version " + acorn.version + ", please use acorn 5 >= 5.2");
   }
   var tt = acorn.tokTypes;
-  var pp = acorn.Parser.prototype;
-
-  // this is the same parseObj that acorn has with...
-  function parseObj(isPattern, refDestructuringErrors) {
-    let node = this.startNode(), first = true, propHash = {}
-    node.properties = []
-    this.next()
-    while (!this.eat(tt.braceR)) {
-      if (!first) {
-        this.expect(tt.comma)
-        if (this.afterTrailingComma(tt.braceR)) break
-      } else first = false
-
-      let prop = this.startNode(), isGenerator, isAsync, startPos, startLoc
-      if (this.options.ecmaVersion >= 6) {
-        // ...the spread logic borrowed from babylon :)
-        if (this.type === tt.ellipsis) {
-          prop = isPattern ? this.parseRestBinding() : this.parseSpread(refDestructuringErrors)
-          node.properties.push(prop)
-          if (this.type === tt.comma) {
-            if (isPattern) {
-              this.raise(this.start, "Comma is not permitted after the rest element")
-            } else if (refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
-              refDestructuringErrors.trailingComma = this.start
-            }
-          }
-          continue
-        }
-
-        prop.method = false
-        prop.shorthand = false
-        if (isPattern || refDestructuringErrors) {
-          startPos = this.start
-          startLoc = this.startLoc
-        }
-        if (!isPattern)
-          isGenerator = this.eat(tt.star)
-      }
-      this.parsePropertyName(prop)
-      if (!isPattern && this.options.ecmaVersion >= 8 && !isGenerator && this.isAsyncProp(prop)) {
-        isAsync = true
-        this.parsePropertyName(prop, refDestructuringErrors)
-      } else {
-        isAsync = false
-      }
-      this.parsePropertyValue(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors)
-      if (!isPattern) this.checkPropClash(prop, propHash)
-      node.properties.push(this.finishNode(prop, "Property"))
-    }
-    return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
-  }
 
   const getCheckLVal = origCheckLVal => function (expr, bindingType, checkClashes) {
     if (expr.type == "ObjectPattern") {
@@ -70,26 +20,68 @@ module.exports = function(acorn) {
   }
 
   acorn.plugins.objectSpread = function objectSpreadPlugin(instance) {
-    pp.parseObj = parseObj;
+    instance.extend("parseProperty", nextMethod => function (isPattern, refDestructuringErrors) {
+      if (this.options.ecmaVersion >= 6 && this.type === tt.ellipsis) {
+        let prop
+        if (isPattern) {
+          prop = this.startNode()
+          this.next()
+          prop.argument = this.parseIdent()
+          this.finishNode(prop, "RestElement")
+        } else {
+          prop = this.parseSpread(refDestructuringErrors)
+        }
+        if (this.type === tt.comma) {
+          if (isPattern) {
+            this.raise(this.start, "Comma is not permitted after the rest element")
+          } else if (refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
+            refDestructuringErrors.trailingComma = this.start
+          }
+        }
+        return prop
+      }
+
+      return nextMethod.apply(this, arguments)
+    })
+    instance.extend("checkPropClash", nextMethod => function(prop, propHash) {
+      if (prop.type == "SpreadElement" || prop.type == "RestElement") return
+      return nextMethod.apply(this, arguments)
+    })
     instance.extend("checkLVal", getCheckLVal)
-    instance.extend("toAssignable", nextMethod => function(node, isBinding) {
+
+    // This backports toAssignable from 5.3.0 to 5.2.x
+    instance.extend("toAssignable", nextMethod => function(node, isBinding, refDestructuringErrors) {
       if (this.options.ecmaVersion >= 6 && node) {
         if (node.type == "ObjectExpression") {
           node.type = "ObjectPattern"
+          if (refDestructuringErrors) this.checkPatternErrors(refDestructuringErrors, true)
           for (let prop of node.properties)
-            this.toAssignable(prop, isBinding)
+            this.toAssignable(prop, isBinding, refDestructuringErrors)
           return node
         } else if (node.type === "Property") {
           // AssignmentProperty has type == "Property"
           if (node.kind !== "init") this.raise(node.key.start, "Object pattern can't contain getter or setter")
-          return this.toAssignable(node.value, isBinding)
+          return this.toAssignable(node.value, isBinding, refDestructuringErrors)
         } else if (node.type === "SpreadElement") {
           node.type = "RestElement"
-          return this.toAssignable(node.argument, isBinding)
+          this.toAssignable(node.argument, isBinding, refDestructuringErrors)
+          if (node.argument.type === "AssignmentPattern")
+            this.raise(node.argument.start, "Rest elements cannot have a default value")
+          return
         }
       }
       return nextMethod.apply(this, arguments)
     })
+    instance.extend("toAssignableList", nextMethod => function (exprList, isBinding) {
+      const result = nextMethod.call(this, exprList, isBinding)
+      if (exprList.length && exprList[exprList.length - 1] && exprList[exprList.length - 1].type === "RestElement") {
+        // Backport check from 5.3.0
+        if (exprList[exprList.length - 1].argument.type === "AssignmentPattern")
+          this.raise(exprList[exprList.length - 1].argument.start, "Rest elements cannot have a default value")
+      }
+      return result
+    })
+
     instance.extend("checkPatternExport", nextMethod => function(exports, pat) {
       if (pat.type == "ObjectPattern") {
         for (let prop of pat.properties)
diff --git a/package.json b/package.json
index ec81139..1b53eed 100644
--- a/package.json
+++ b/package.json
@@ -15,7 +15,7 @@
     "test": "node test/run.js"
   },
   "dependencies": {
-    "acorn": "^5.1.2"
+    "acorn": "^5.2.1"
   },
-  "version": "3.1.0"
+  "version": "5.1.2"
 }
diff --git a/test/tests-object-spread.js b/test/tests-object-spread.js
index 8096156..d570e47 100644
--- a/test/tests-object-spread.js
+++ b/test/tests-object-spread.js
@@ -2,146 +2,6 @@
 
 var fbTestFixture = {
   // Taken and adapted from babylon's tests.
-  parseObj: {
-    "function fn({__proto__: a, __proto__: b}) {}": {
-      "type": "FunctionDeclaration",
-      "start": 0,
-      "end": 44,
-      "id": {
-        "type": "Identifier",
-        "start": 9,
-        "end": 11,
-        "name": "fn"
-      },
-      "generator": false,
-      "expression": false,
-      "params": [
-        {
-          "type": "ObjectPattern",
-          "start": 12,
-          "end": 40,
-          "properties": [
-            {
-              "type": "Property",
-              "start": 13,
-              "end": 25,
-              "method": false,
-              "shorthand": false,
-              "computed": false,
-              "key": {
-                "type": "Identifier",
-                "start": 13,
-                "end": 22,
-                "name": "__proto__"
-              },
-              "value": {
-                "type": "Identifier",
-                "start": 24,
-                "end": 25,
-                "name": "a"
-              },
-              "kind": "init"
-            },
-            {
-              "type": "Property",
-              "start": 27,
-              "end": 39,
-              "method": false,
-              "shorthand": false,
-              "computed": false,
-              "key": {
-                "type": "Identifier",
-                "start": 27,
-                "end": 36,
-                "name": "__proto__"
-              },
-              "value": {
-                "type": "Identifier",
-                "start": 38,
-                "end": 39,
-                "name": "b"
-              },
-              "kind": "init"
-            }
-          ]
-        }
-      ],
-      "body": {
-        "type": "BlockStatement",
-        "start": 42,
-        "end": 44,
-        "body": []
-      }
-    },
-    'obj = { then: 1, catch: 2 }': {
-      type: "ExpressionStatement",
-      start: 0,
-      end: 27,
-      expression: {
-        "type": "AssignmentExpression",
-        "start": 0,
-        "end": 27,
-        "operator": "=",
-        "left": {
-          "type": "Identifier",
-          "start": 0,
-          "end": 3,
-          "name": "obj"
-        },
-        "right": {
-          "type": "ObjectExpression",
-          "start": 6,
-          "end": 27,
-          "properties": [
-            {
-              "type": "Property",
-              "start": 8,
-              "end": 15,
-              "method": false,
-              "shorthand": false,
-              "computed": false,
-              "key": {
-                "type": "Identifier",
-                "start": 8,
-                "end": 12,
-                "name": "then"
-              },
-              "value": {
-                "type": "Literal",
-                "start": 14,
-                "end": 15,
-                "value": 1,
-                "raw": "1"
-              },
-              "kind": "init"
-            },
-            {
-              "type": "Property",
-              "start": 17,
-              "end": 25,
-              "method": false,
-              "shorthand": false,
-              "computed": false,
-              "key": {
-                "type": "Identifier",
-                "start": 17,
-                "end": 22,
-                "name": "catch"
-              },
-              "value": {
-                "type": "Literal",
-                "start": 24,
-                "end": 25,
-                "value": 2,
-                "raw": "2"
-              },
-              "kind": "init"
-            }
-          ]
-        }
-      }
-    }
-  },
   'ObjectSpread': {
     'let z = {...x}': {
       "type": "VariableDeclaration",
@@ -540,178 +400,6 @@ var fbTestFixture = {
         }
       }
     },
-    'let {...{x, y}} = {}': {
-      "type": "VariableDeclaration",
-      "start": 0,
-      "end": 20,
-      "declarations": [
-        {
-          "type": "VariableDeclarator",
-          "start": 4,
-          "end": 20,
-          "id": {
-            "type": "ObjectPattern",
-            "start": 4,
-            "end": 15,
-            "properties": [
-              {
-                "type": "RestElement",
-                "start": 5,
-                "end": 14,
-                "argument": {
-                  "type": "ObjectPattern",
-                  "start": 8,
-                  "end": 14,
-                  "properties": [
-                    {
-                      "type": "Property",
-                      "start": 9,
-                      "end": 10,
-                      "method": false,
-                      "shorthand": true,
-                      "computed": false,
-                      "key": {
-                        "type": "Identifier",
-                        "start": 9,
-                        "end": 10,
-                        "name": "x"
-                      },
-                      "kind": "init",
-                      "value": {
-                        "type": "Identifier",
-                        "start": 9,
-                        "end": 10,
-                        "name": "x"
-                      }
-                    },
-                    {
-                      "type": "Property",
-                      "start": 12,
-                      "end": 13,
-                      "method": false,
-                      "shorthand": true,
-                      "computed": false,
-                      "key": {
-                        "type": "Identifier",
-                        "start": 12,
-                        "end": 13,
-                        "name": "y"
-                      },
-                      "kind": "init",
-                      "value": {
-                        "type": "Identifier",
-                        "start": 12,
-                        "end": 13,
-                        "name": "y"
-                      }
-                    }
-                  ]
-                }
-              }
-            ]
-          },
-          "init": {
-            "type": "ObjectExpression",
-            "start": 18,
-            "end": 20,
-            "properties": []
-          }
-        }
-      ],
-      "kind": "let"
-    },
-    'let {...{...{x, y}}} = {}': {
-      "type": "VariableDeclaration",
-      "start": 0,
-      "end": 25,
-      "declarations": [
-        {
-          "type": "VariableDeclarator",
-          "start": 4,
-          "end": 25,
-          "id": {
-            "type": "ObjectPattern",
-            "start": 4,
-            "end": 20,
-            "properties": [
-              {
-                "type": "RestElement",
-                "start": 5,
-                "end": 19,
-                "argument": {
-                  "type": "ObjectPattern",
-                  "start": 8,
-                  "end": 19,
-                  "properties": [
-                    {
-                      "type": "RestElement",
-                      "start": 9,
-                      "end": 18,
-                      "argument": {
-                        "type": "ObjectPattern",
-                        "start": 12,
-                        "end": 18,
-                        "properties": [
-                          {
-                            "type": "Property",
-                            "start": 13,
-                            "end": 14,
-                            "method": false,
-                            "shorthand": true,
-                            "computed": false,
-                            "key": {
-                              "type": "Identifier",
-                              "start": 13,
-                              "end": 14,
-                              "name": "x"
-                            },
-                            "kind": "init",
-                            "value": {
-                              "type": "Identifier",
-                              "start": 13,
-                              "end": 14,
-                              "name": "x"
-                            }
-                          },
-                          {
-                            "type": "Property",
-                            "start": 16,
-                            "end": 17,
-                            "method": false,
-                            "shorthand": true,
-                            "computed": false,
-                            "key": {
-                              "type": "Identifier",
-                              "start": 16,
-                              "end": 17,
-                              "name": "y"
-                            },
-                            "kind": "init",
-                            "value": {
-                              "type": "Identifier",
-                              "start": 16,
-                              "end": 17,
-                              "name": "y"
-                            }
-                          }
-                        ]
-                      }
-                    }
-                  ]
-                }
-              }
-            ]
-          },
-          "init": {
-            "type": "ObjectExpression",
-            "start": 23,
-            "end": 25,
-            "properties": []
-          }
-        }
-      ],
-      "kind": "let"
-    },
     'const fn = ({text = "default", ...props}) => text + props.children': {
       "type": "VariableDeclaration",
       "start": 0,
@@ -834,10 +522,15 @@ if (typeof exports !== "undefined") {
 testFail("({get x() {}}) => {}", "Object pattern can't contain getter or setter (1:6)")
 testFail("let {...x, ...y} = {}", "Comma is not permitted after the rest element (1:9)")
 testFail("({...x,}) => z", "Comma is not permitted after the rest element (1:6)")
-testFail("({...{...x,}}) => z", "Comma is not permitted after the rest element (1:10)")
 testFail("export const { foo, ...bar } = baz;\nexport const bar = 1;\n", "Identifier 'bar' has already been declared (2:13)", {
   sourceType: "module"
 })
+testFail("function ({...x,}) { z }", "Unexpected token (1:9)")
+testFail("let {...{x, y}} = {}", "Unexpected token (1:8)")
+testFail("let {...{...{x, y}}} = {}", "Unexpected token (1:8)")
+testFail("0, {...rest, b} = {}", "Comma is not permitted after the rest element (1:11)")
+testFail("(([a, ...b = 0]) => {})", "Rest elements cannot have a default value (1:9)")
+testFail("(({a, ...b = 0}) => {})", "Rest elements cannot have a default value (1:9)")
 
 for (var ns in fbTestFixture) {
   ns = fbTestFixture[ns];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-object-spread.git



More information about the Pkg-javascript-commits mailing list