Skip to content

Commit 0c5f9dc

Browse files
Merge pull request #1 from browserify/acorn-6
Acorn 6
2 parents 712822f + d46cbe0 commit 0c5f9dc

File tree

11 files changed

+297
-68
lines changed

11 files changed

+297
-68
lines changed

LICENSE.md

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,82 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616

17-
## acorn5-object-spread
17+
## acorn-bigint
1818

19-
The code in the `lib/object-spread` folder is compiled from code licensed as MIT:
20-
21-
> Copyright (C) 2016 by UXtemple
19+
The code in the `lib/bigint` folder is compiled from code licensed as MIT:
2220

21+
> Copyright (C) 2017-2018 by Adrian Heine
22+
>
2323
> Permission is hereby granted, free of charge, to any person obtaining a copy
2424
> of this software and associated documentation files (the "Software"), to deal
2525
> in the Software without restriction, including without limitation the rights
2626
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2727
> copies of the Software, and to permit persons to whom the Software is
2828
> furnished to do so, subject to the following conditions:
29-
29+
>
3030
> The above copyright notice and this permission notice shall be included in
3131
> all copies or substantial portions of the Software.
32+
>
33+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39+
> THE SOFTWARE.
3240
41+
Find the source code at https://github.com/acornjs/acorn-bigint.
42+
43+
## acorn-import-meta
44+
45+
The code in the `lib/import-meta` folder is compiled from code licensed as MIT:
46+
47+
> Copyright (C) 2017-2018 by Adrian Heine
48+
>
49+
> Permission is hereby granted, free of charge, to any person obtaining a copy
50+
> of this software and associated documentation files (the "Software"), to deal
51+
> in the Software without restriction, including without limitation the rights
52+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
53+
> copies of the Software, and to permit persons to whom the Software is
54+
> furnished to do so, subject to the following conditions:
55+
>
56+
> The above copyright notice and this permission notice shall be included in
57+
> all copies or substantial portions of the Software.
58+
>
3359
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3460
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3561
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3662
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3763
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3864
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3965
> THE SOFTWARE.
66+
67+
Find the source code at https://github.com/acornjs/acorn-import-meta.
68+
69+
## acorn-dynamic-import
70+
71+
The code in the `lib/dynamic-import` folder is licensed as MIT:
72+
73+
> MIT License
74+
>
75+
> Copyright (c) 2016 Jordan Gensler
76+
>
77+
> Permission is hereby granted, free of charge, to any person obtaining a copy
78+
> of this software and associated documentation files (the "Software"), to deal
79+
> in the Software without restriction, including without limitation the rights
80+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
81+
> copies of the Software, and to permit persons to whom the Software is
82+
> furnished to do so, subject to the following conditions:
83+
>
84+
> The above copyright notice and this permission notice shall be included in all
85+
> copies or substantial portions of the Software.
86+
>
87+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
88+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
89+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
90+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
91+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
92+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
93+
> SOFTWARE.
94+
95+
Find the source code at https://github.com/kesne/acorn-dynamic-import.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ 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]))
3233
- Dynamic `import()` (via [acorn-dynamic-import](https://github.com/kesne/acorn-dynamic-import))
3334
- The `import.meta` property (via [acorn-import-meta](https://github.com/acornjs/acorn-import-meta))
3435

build.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ function compile (name, output) { // eslint-disable-line no-unused-vars
1717
fs.writeFileSync(path.join(__dirname, output), HEADER + result.code, 'utf8')
1818
}
1919

20-
compile('acorn-import-meta/inject', './lib/import-meta/index.js')
20+
compile('acorn-bigint', './lib/bigint/index.js')
21+
compile('acorn-import-meta', './lib/import-meta/index.js')
22+
compile('./lib/dynamic-import/source', './lib/dynamic-import/index.js')

index.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
var acorn = require('acorn')
22
var xtend = require('xtend')
33

4+
var CJSParser = acorn.Parser
5+
.extend(require('./lib/bigint'))
6+
var ESModulesParser = CJSParser
7+
.extend(require('./lib/dynamic-import'))
8+
.extend(require('./lib/import-meta'))
9+
410
function mapOptions (opts) {
511
if (!opts) opts = {}
6-
opts = xtend({
12+
return xtend({
713
ecmaVersion: 2019,
814
allowHashBang: true,
9-
allowReturnOutsideFunction: true,
10-
plugins: {
11-
dynamicImport: opts.sourceType === 'module',
12-
importMeta: opts.sourceType === 'module'
13-
}
15+
allowReturnOutsideFunction: true
1416
}, opts)
15-
opts.plugins = xtend(opts.plugins, {})
16-
return opts
17+
}
18+
19+
function getParser (opts) {
20+
if (!opts) opts = {}
21+
return opts.sourceType === 'module' ? ESModulesParser : CJSParser
1722
}
1823

1924
module.exports = exports = xtend(acorn, {
2025
parse: function parse (src, opts) {
21-
return acorn.parse(src, mapOptions(opts))
26+
return getParser(opts).parse(src, mapOptions(opts))
2227
},
2328
parseExpressionAt: function parseExpressionAt (src, offset, opts) {
24-
return acorn.parseExpressionAt(src, offset, mapOptions(opts))
29+
return getParser(opts).parseExpressionAt(src, offset, mapOptions(opts))
2530
},
2631
tokenizer: function tokenizer (src, opts) {
27-
return acorn.tokenizer(src, mapOptions(opts))
32+
return getParser(opts).tokenizer(src, mapOptions(opts))
2833
}
2934
})
30-
31-
require('acorn-dynamic-import/lib/inject').default(exports)
32-
require('./lib/import-meta')(exports)

lib/bigint/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
"use strict"
4+
5+
var acorn = require("acorn")
6+
var tt = acorn.tokTypes
7+
var isIdentifierStart = acorn.isIdentifierStart
8+
9+
module.exports = function(Parser) {
10+
return (function (Parser) {
11+
function anonymous () {
12+
Parser.apply(this, arguments);
13+
}
14+
15+
if ( Parser ) anonymous.__proto__ = Parser;
16+
anonymous.prototype = Object.create( Parser && Parser.prototype );
17+
anonymous.prototype.constructor = anonymous;
18+
19+
anonymous.prototype.parseLiteral = function parseLiteral (value) {
20+
var node = Parser.prototype.parseLiteral.call(this, value)
21+
if (node.raw.charCodeAt(node.raw.length - 1) == 110) { node.bigint = node.raw }
22+
return node
23+
};
24+
25+
anonymous.prototype.readRadixNumber = function readRadixNumber (radix) {
26+
var start = this.pos
27+
this.pos += 2 // 0x
28+
var val = this.readInt(radix)
29+
if (val === null) { this.raise(this.start + 2, ("Expected number in radix " + radix)) }
30+
if (this.input.charCodeAt(this.pos) == 110) {
31+
var str = this.input.slice(start, this.pos)
32+
val = typeof BigInt !== "undefined" ? BigInt(str) : null
33+
++this.pos
34+
} else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number") }
35+
return this.finishToken(tt.num, val)
36+
};
37+
38+
anonymous.prototype.readNumber = function readNumber (startsWithDot) {
39+
var start = this.pos
40+
41+
// Not an int
42+
if (startsWithDot) { return Parser.prototype.readNumber.call(this, startsWithDot) }
43+
44+
// Legacy octal
45+
if (this.input.charCodeAt(start) === 48 && this.input.charCodeAt(start + 1) !== 110) {
46+
return Parser.prototype.readNumber.call(this, startsWithDot)
47+
}
48+
49+
if (this.readInt(10) === null) { this.raise(start, "Invalid number") }
50+
51+
// Not a BigInt, reset and parse again
52+
if (this.input.charCodeAt(this.pos) != 110) {
53+
this.pos = start
54+
return Parser.prototype.readNumber.call(this, startsWithDot)
55+
}
56+
57+
var str = this.input.slice(start, this.pos)
58+
var val = typeof BigInt !== "undefined" && BigInt.parseInt ? BigInt.parseInt(str) : null
59+
++this.pos
60+
return this.finishToken(tt.num, val)
61+
};
62+
63+
return anonymous;
64+
}(Parser))
65+
}

lib/dynamic-import/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
/* eslint-disable no-underscore-dangle */
4+
5+
var DynamicImportKey = 'Import';
6+
7+
var ref = require('acorn');
8+
var tt = ref.tokTypes;
9+
10+
// NOTE: This allows `yield import()` to parse correctly.
11+
tt._import.startsExpr = true;
12+
13+
function parseDynamicImport() {
14+
var node = this.startNode();
15+
this.next();
16+
if (this.type !== tt.parenL) {
17+
this.unexpected();
18+
}
19+
return this.finishNode(node, DynamicImportKey);
20+
}
21+
22+
function parenAfter() {
23+
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
24+
}
25+
26+
function dynamicImport(Parser) {
27+
return (function (Parser) {
28+
function anonymous () {
29+
Parser.apply(this, arguments);
30+
}
31+
32+
if ( Parser ) anonymous.__proto__ = Parser;
33+
anonymous.prototype = Object.create( Parser && Parser.prototype );
34+
anonymous.prototype.constructor = anonymous;
35+
36+
anonymous.prototype.parseStatement = function parseStatement (context, topLevel, exports) {
37+
if (this.type === tt._import && parenAfter.call(this)) {
38+
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
39+
}
40+
return Parser.prototype.parseStatement.call(this, context, topLevel, exports)
41+
};
42+
43+
anonymous.prototype.parseExprAtom = function parseExprAtom (refDestructuringErrors) {
44+
if (this.type === tt._import) {
45+
return parseDynamicImport.call(this);
46+
}
47+
return Parser.prototype.parseExprAtom.call(this, refDestructuringErrors);
48+
};
49+
50+
return anonymous;
51+
}(Parser));
52+
};
53+
54+
module.exports = dynamicImport
55+
dynamicImport.DynamicImportKey = DynamicImportKey

lib/dynamic-import/source.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
const DynamicImportKey = 'Import';
4+
5+
const { tokTypes: tt } = require('acorn')
6+
7+
// NOTE: This allows `yield import()` to parse correctly.
8+
tt._import.startsExpr = true;
9+
10+
function parseDynamicImport() {
11+
const node = this.startNode();
12+
this.next();
13+
if (this.type !== tt.parenL) {
14+
this.unexpected();
15+
}
16+
return this.finishNode(node, DynamicImportKey);
17+
}
18+
19+
function parenAfter() {
20+
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
21+
}
22+
23+
function dynamicImport(Parser) {
24+
return class extends Parser {
25+
parseStatement(context, topLevel, exports) {
26+
if (this.type === tt._import && parenAfter.call(this)) {
27+
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
28+
}
29+
return super.parseStatement(context, topLevel, exports)
30+
}
31+
32+
parseExprAtom(refDestructuringErrors) {
33+
if (this.type === tt._import) {
34+
return parseDynamicImport.call(this);
35+
}
36+
return super.parseExprAtom(refDestructuringErrors);
37+
}
38+
};
39+
};
40+
41+
module.exports = dynamicImport
42+
dynamicImport.DynamicImportKey = DynamicImportKey

0 commit comments

Comments
 (0)