Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jan 9, 2018
0 parents commit b5ef15c
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
package-lock.json
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
node_js:
- 9
- 8
- 6
- 4
- 0.12
- 0.10
- 0.8
- 0.6
cache:
directories:
- ~/.npm
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @browserify/acorn change log

All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).
15 changes: 15 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# [Apache License 2.0](https://spdx.org/licenses/Apache-2.0)

Copyright 2018 Renée Kooi <renee@kooi.me>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

> http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# @browserify/acorn

Acorn preloaded with plugins for syntax parity with recent Node versions.

[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url]

[npm-image]: https://img.shields.io/npm/v/@browserify/acorn.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/@browserify/acorn
[travis-image]: https://img.shields.io/travis/browserify/acorn.svg?style=flat-square
[travis-url]: https://travis-ci.org/browserify/acorn
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard

## Install

```
npm install @browserify/acorn
```

## Usage

```js
var acorn = require('@browserify/acorn')
```

## License

[Apache-2.0](LICENSE.md)
18 changes: 18 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var buble = require('buble')

function compile (name, output) {
console.log(name, '→', output)
mkdirp.sync(path.dirname(path.join(__dirname, output)))
var source = fs.readFileSync(require.resolve(name), 'utf8')
var result = buble.transform(source, {
transforms: {
dangerousForOf: true
}
})
fs.writeFileSync(path.join(__dirname, output), result.code, 'utf8')
}

compile('acorn5-object-spread/inject', 'lib/object-spread/index.js')
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var acorn = require('acorn')
var xtend = require('xtend')

function mapOptions (opts) {
if (!opts) opts = {}
opts = xtend({ ecmaVersion: 9 }, opts)
opts.plugins = xtend(opts.plugins, {
objectSpread: true
})
return opts
}

module.exports = exports = xtend(acorn, {
parse: function parse (src, opts) {
return acorn.parse(src, mapOptions(opts))
},
parseExpressionAt: function parseExpressionAt (src, offset, opts) {
return acorn.parseExpressionAt(src, offset, mapOptions(opts))
},
tokenizer: function tokenizer (src, opts) {
return acorn.tokenizer(src, mapOptions(opts))
},
})

require('./lib/object-spread')(exports)
102 changes: 102 additions & 0 deletions lib/object-spread/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

module.exports = function(acorn) {
var 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 getCheckLVal = function (origCheckLVal) { return function (expr, bindingType, checkClashes) {
var this$1 = this;

if (expr.type == "ObjectPattern") {
for (var i = 0, list = expr.properties; i < list.length; i += 1)
{
var prop = list[i];

this$1.checkLVal(prop, bindingType, checkClashes)
}
return
} else if (expr.type === "Property") {
// AssignmentProperty has type == "Property"
return this.checkLVal(expr.value, bindingType, checkClashes)
}
return origCheckLVal.apply(this, arguments)
}; }

acorn.plugins.objectSpread = function objectSpreadPlugin(instance) {
instance.extend("parseProperty", function (nextMethod) { return function (isPattern, refDestructuringErrors) {
if (this.options.ecmaVersion >= 6 && this.type === tt.ellipsis) {
var 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", function (nextMethod) { return function(prop, propHash) {
if (prop.type == "SpreadElement" || prop.type == "RestElement") { return }
return nextMethod.apply(this, arguments)
}; })
instance.extend("checkLVal", getCheckLVal)
instance.extend("toAssignable", function (nextMethod) { return function(node, isBinding) {
var this$1 = this;

if (this.options.ecmaVersion >= 6 && node) {
if (node.type == "ObjectExpression") {
node.type = "ObjectPattern"
for (var i = 0, list = node.properties; i < list.length; i += 1)
{
var prop = list[i];

this$1.toAssignable(prop, isBinding)
}
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)
} else if (node.type === "SpreadElement") {
node.type = "RestElement"
return this.toAssignable(node.argument, isBinding)
}
}
return nextMethod.apply(this, arguments)
}; })
instance.extend("checkPatternExport", function (nextMethod) { return function(exports, pat) {
var this$1 = this;

if (pat.type == "ObjectPattern") {
for (var i = 0, list = pat.properties; i < list.length; i += 1)
{
var prop = list[i];

this$1.checkPatternExport(exports, prop)
}
return
} else if (pat.type === "Property") {
return this.checkPatternExport(exports, pat.value)
} else if (pat.type === "RestElement") {
return this.checkPatternExport(exports, pat.argument)
}
nextMethod.apply(this, arguments)
}; })
};

return acorn;
};
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@browserify/acorn",
"description": "the acorn javascript parser, preloaded with plugins for syntax parity with recent node versions",
"version": "0.0.0",
"author": "Renée Kooi <renee@kooi.me>",
"bugs": {
"url": "https://github.com/browserify/acorn/issues"
},
"dependencies": {
"acorn": "^5.3.0",
"xtend": "^4.0.1"
},
"devDependencies": {
"acorn5-object-spread": "^5.0.0",
"buble": "^0.18.0",
"mkdirp": "^0.5.1",
"standard": "^10.0.3",
"tape": "^4.8.0"
},
"homepage": "https://github.com/browserify/acorn",
"keywords": [
"acorn",
"browserify",
"javascript",
"parser"
],
"license": "Apache-2.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/browserify/acorn.git"
},
"scripts": {
"test": "standard && node test/index.js",
"prepublish": "npm run build",
"build": "node build.js"
}
}
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var test = require('tape')
var acorn = require('../')
var baseAcorn = require('acorn')

test('parses object spread syntax', function (t) {
var ast = acorn.parse('var a = { ...b }')
t.equal(ast.body[0].declarations[0].init.type, 'ObjectExpression')
t.equal(ast.body[0].declarations[0].init.properties[0].type, 'SpreadElement')

ast = acorn.parse('function a ({ ...b }) {}')
t.equal(ast.body[0].params[0].type, 'ObjectPattern')
t.equal(ast.body[0].params[0].properties[0].type, 'RestElement')

t.end()
})

test('does not change main acorn module', function (t) {
t.throws(function () {
baseAcorn.parse('var a = { ...b }')
})
t.end()
})

0 comments on commit b5ef15c

Please sign in to comment.