Skip to content

Commit e211dc2

Browse files
Merge pull request #11 from browserify/acorn-7
Update acorn to v7
2 parents 7cd9cba + 953ef7a commit e211dc2

File tree

12 files changed

+200
-156
lines changed

12 files changed

+200
-156
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ var acorn = require('acorn-node')
2929

3030
The API is the same as [acorn](https://github.com/acornjs/acorn), but the following syntax features are enabled by default:
3131

32-
- Bigint syntax (via [acorn-bigint](https://github.com/acornjs/acorn-bigint]), MIT)
33-
- Public and private class instance fields (via [acorn-class-fields](https://github.com/acornjs/acorn-class-fields), MIT)
34-
- Public and private class static fields (via [acorn-static-class-features](https://github.com/acornjs/acorn-static-class-features), MIT)
35-
- Dynamic `import()` (via [acorn-dynamic-import](https://github.com/kesne/acorn-dynamic-import))
36-
- The `import.meta` property (via [acorn-import-meta](https://github.com/acornjs/acorn-import-meta), MIT)
37-
- `export * as ns from` syntax (via [acorn-export-ns-from](https://github.com/acornjs/acorn-export-ns-from), MIT)
32+
- Bigint syntax `10n`
33+
- Numeric separators syntax `10_000`
34+
- Public and private class instance fields
35+
- Public and private class static fields
36+
- Dynamic `import()`
37+
- The `import.meta` property
38+
- `export * as ns from` syntax
3839

3940
And the following options have different defaults from acorn, to match Node modules:
4041

@@ -51,4 +52,14 @@ See the [acorn documentation](https://github.com/acornjs/acorn#distwalkjs) for d
5152

5253
## License
5354

54-
[Apache-2.0](LICENSE.md)
55+
The files in the repo root and the ./test folder are licensed as [Apache-2.0](LICENSE.md).
56+
57+
The files in lib/ are generated from other packages:
58+
59+
- lib/bigint: [acorn-bigint](https://github.com/acornjs/acorn-bigint]), MIT
60+
- lib/class-private-elements: [acorn-class-private-elements](https://github.com/acornjs/acorn-class-private-elements), MIT
61+
- lib/dynamic-import: [acorn-dynamic-import](https://github.com/acornjs/acorn-dynamic-import), MIT
62+
- lib/export-ns-from: [acorn-export-ns-from](https://github.com/acornjs/acorn-export-ns-from), MIT
63+
- lib/import-meta: [acorn-import-meta](https://github.com/acornjs/acorn-import-meta), MIT
64+
- lib/numeric-separator: [acorn-numeric-separator](https://github.com/acornjs/acorn-numeric-separator]), MIT
65+
- lib/static-class-features: [acorn-static-class-features](https://github.com/acornjs/acorn-static-class-features), MIT

build.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function privateClassElements (str) {
2323
}
2424

2525
compile('acorn-bigint', './lib/bigint/index.js')
26+
compile('acorn-numeric-separator', './lib/numeric-separator/index.js')
27+
compile('acorn-dynamic-import', './lib/dynamic-import/index.js')
2628
compile('acorn-import-meta', './lib/import-meta/index.js')
2729
compile('acorn-export-ns-from', './lib/export-ns-from/index.js')
2830
compile('acorn-class-fields', './lib/class-fields/index.js', privateClassElements)

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ var CJSParser = acorn.Parser
55
.extend(require('./lib/bigint'))
66
.extend(require('./lib/class-fields'))
77
.extend(require('./lib/static-class-features'))
8-
.extend(require('acorn-dynamic-import').default)
8+
.extend(require('./lib/numeric-separator'))
9+
.extend(require('./lib/dynamic-import').default)
910
var ESModulesParser = CJSParser
1011
.extend(require('./lib/export-ns-from'))
1112
.extend(require('./lib/import-meta'))
1213

1314
function mapOptions (opts) {
1415
if (!opts) opts = {}
1516
return xtend({
16-
ecmaVersion: 2019,
17+
ecmaVersion: 2020,
1718
allowHashBang: true,
1819
allowReturnOutsideFunction: true
1920
}, opts)

lib/acorn-private-class-elements/index.js

Lines changed: 0 additions & 135 deletions
This file was deleted.

lib/bigint/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function(Parser) {
1818

1919
anonymous.prototype.parseLiteral = function parseLiteral (value) {
2020
var node = Parser.prototype.parseLiteral.call(this, value)
21-
if (node.raw.charCodeAt(node.raw.length - 1) == 110) { node.bigint = node.raw }
21+
if (node.raw.charCodeAt(node.raw.length - 1) == 110) { node.bigint = this.getNumberInput(node.start, node.end) }
2222
return node
2323
};
2424

@@ -28,7 +28,7 @@ module.exports = function(Parser) {
2828
var val = this.readInt(radix)
2929
if (val === null) { this.raise(this.start + 2, ("Expected number in radix " + radix)) }
3030
if (this.input.charCodeAt(this.pos) == 110) {
31-
var str = this.input.slice(start, this.pos)
31+
var str = this.getNumberInput(start, this.pos)
3232
val = typeof BigInt !== "undefined" ? BigInt(str) : null
3333
++this.pos
3434
} else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number") }
@@ -54,12 +54,18 @@ module.exports = function(Parser) {
5454
return Parser.prototype.readNumber.call(this, startsWithDot)
5555
}
5656

57-
var str = this.input.slice(start, this.pos)
57+
var str = this.getNumberInput(start, this.pos)
5858
var val = typeof BigInt !== "undefined" ? BigInt(str) : null
5959
++this.pos
6060
return this.finishToken(tt.num, val)
6161
};
6262

63+
// This is basically a hook for acorn-numeric-separator
64+
anonymous.prototype.getNumberInput = function getNumberInput (start, end) {
65+
if (Parser.prototype.getNumberInput) { return Parser.prototype.getNumberInput.call(this, start, end) }
66+
return this.input.slice(start, end)
67+
};
68+
6369
return anonymous;
6470
}(Parser))
6571
}

lib/dynamic-import/index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
'use strict';
4+
5+
Object.defineProperty(exports, "__esModule", {
6+
value: true
7+
});
8+
exports.DynamicImportKey = undefined;
9+
10+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) { descriptor.writable = true; } Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) { defineProperties(Constructor.prototype, protoProps); } if (staticProps) { defineProperties(Constructor, staticProps); } return Constructor; }; }();
11+
12+
var _get = function () {
13+
function get(object, property, receiver) { if (object === null) { object = Function.prototype; } var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }
14+
15+
return get;
16+
}();
17+
18+
exports['default'] = dynamicImport;
19+
20+
var _acorn = require('acorn');
21+
22+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23+
24+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
25+
26+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) { Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } } /* eslint-disable no-underscore-dangle */
27+
28+
29+
var DynamicImportKey = exports.DynamicImportKey = 'Import';
30+
31+
// NOTE: This allows `yield import()` to parse correctly.
32+
_acorn.tokTypes._import.startsExpr = true;
33+
34+
function parseDynamicImport() {
35+
var node = this.startNode();
36+
this.next();
37+
if (this.type !== _acorn.tokTypes.parenL) {
38+
this.unexpected();
39+
}
40+
return this.finishNode(node, DynamicImportKey);
41+
}
42+
43+
function parenAfter() {
44+
return (/^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos))
45+
);
46+
}
47+
48+
function dynamicImport(Parser) {
49+
return function (_Parser) {
50+
_inherits(_class, _Parser);
51+
52+
function _class() {
53+
_classCallCheck(this, _class);
54+
55+
return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
56+
}
57+
58+
_createClass(_class, [{
59+
key: 'parseStatement',
60+
value: function () {
61+
function parseStatement(context, topLevel, exports) {
62+
if (this.type === _acorn.tokTypes._import && parenAfter.call(this)) {
63+
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
64+
}
65+
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'parseStatement', this).call(this, context, topLevel, exports);
66+
}
67+
68+
return parseStatement;
69+
}()
70+
}, {
71+
key: 'parseExprAtom',
72+
value: function () {
73+
function parseExprAtom(refDestructuringErrors) {
74+
if (this.type === _acorn.tokTypes._import) {
75+
return parseDynamicImport.call(this);
76+
}
77+
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'parseExprAtom', this).call(this, refDestructuringErrors);
78+
}
79+
80+
return parseExprAtom;
81+
}()
82+
}]);
83+
84+
return _class;
85+
}(Parser);
86+
}

lib/import-meta/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ module.exports = function(Parser) {
3737
if (node.property.name !== "meta") {
3838
this.raiseRecoverable(node.property.start, "The only valid meta property for import is import.meta")
3939
}
40+
if (this.containsEsc) {
41+
this.raiseRecoverable(node.property.start, "\"meta\" in import.meta must not contain escape sequences")
42+
}
4043
return this.finishNode(node, "MetaProperty")
4144
};
4245

lib/numeric-separator/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
"use strict"
4+
5+
module.exports = function(Parser) {
6+
return /*@__PURE__*/(function (Parser) {
7+
function anonymous () {
8+
Parser.apply(this, arguments);
9+
}
10+
11+
if ( Parser ) anonymous.__proto__ = Parser;
12+
anonymous.prototype = Object.create( Parser && Parser.prototype );
13+
anonymous.prototype.constructor = anonymous;
14+
15+
anonymous.prototype.readInt = function readInt (radix, len) {
16+
// Hack: len is only != null for unicode escape sequences,
17+
// where numeric separators are not allowed
18+
if (len != null) { return Parser.prototype.readInt.call(this, radix, len) }
19+
20+
var start = this.pos, total = 0, acceptUnderscore = false
21+
for (;;) {
22+
var code = this.input.charCodeAt(this.pos), val = (void 0)
23+
if (code >= 97) { val = code - 97 + 10 } // a
24+
else if (code == 95) {
25+
if (!acceptUnderscore) { this.raise(this.pos, "Invalid numeric separator") }
26+
++this.pos
27+
acceptUnderscore = false
28+
continue
29+
} else if (code >= 65) { val = code - 65 + 10 } // A
30+
else if (code >= 48 && code <= 57) { val = code - 48 } // 0-9
31+
else { val = Infinity }
32+
if (val >= radix) { break }
33+
++this.pos
34+
total = total * radix + val
35+
acceptUnderscore = true
36+
}
37+
if (this.pos === start) { return null }
38+
if (!acceptUnderscore) { this.raise(this.pos - 1, "Invalid numeric separator") }
39+
40+
return total
41+
};
42+
43+
anonymous.prototype.readNumber = function readNumber (startsWithDot) {
44+
var token = Parser.prototype.readNumber.call(this, startsWithDot)
45+
var octal = this.end - this.start >= 2 && this.input.charCodeAt(this.start) === 48
46+
var stripped = this.getNumberInput(this.start, this.end)
47+
if (stripped.length < this.end - this.start) {
48+
if (octal) { this.raise(this.start, "Invalid number") }
49+
this.value = parseFloat(stripped)
50+
}
51+
return token
52+
};
53+
54+
// This is used by acorn-bigint
55+
anonymous.prototype.getNumberInput = function getNumberInput (start, end) {
56+
return this.input.slice(start, end).replace(/_/g, "")
57+
};
58+
59+
return anonymous;
60+
}(Parser))
61+
}

0 commit comments

Comments
 (0)