acorn-bigint
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -126,3 +126,3 @@ { | ||
], | ||
"new-cap": "error", | ||
"new-cap": ["error", {"capIsNewExceptions": ["BigInt"]}], | ||
"new-parens": "error", | ||
@@ -129,0 +129,0 @@ "newline-after-var": "off", |
52
index.js
"use strict" | ||
module.exports = require("./inject")(require("acorn")) | ||
const acorn = require("acorn") | ||
const tt = acorn.tokTypes | ||
const isIdentifierStart = acorn.isIdentifierStart | ||
module.exports = function(Parser) { | ||
return class extends Parser { | ||
parseLiteral(value) { | ||
const node = super.parseLiteral(value) | ||
if (node.raw.charCodeAt(node.raw.length - 1) == 110) node.bigint = node.raw | ||
return node | ||
} | ||
readRadixNumber(radix) { | ||
let start = this.pos | ||
this.pos += 2 // 0x | ||
let val = this.readInt(radix) | ||
if (val === null) this.raise(this.start + 2, `Expected number in radix ${radix}`) | ||
if (this.input.charCodeAt(this.pos) == 110) { | ||
let str = this.input.slice(start, this.pos) | ||
val = typeof BigInt !== "undefined" ? BigInt(str) : null | ||
++this.pos | ||
} else if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number") | ||
return this.finishToken(tt.num, val) | ||
} | ||
readNumber(startsWithDot) { | ||
let start = this.pos | ||
// Not an int | ||
if (startsWithDot) return super.readNumber(startsWithDot) | ||
// Legacy octal | ||
if (this.input.charCodeAt(start) === 48 && this.input.charCodeAt(start + 1) !== 110) { | ||
return super.readNumber(startsWithDot) | ||
} | ||
if (this.readInt(10) === null) this.raise(start, "Invalid number") | ||
// Not a BigInt, reset and parse again | ||
if (this.input.charCodeAt(this.pos) != 110) { | ||
this.pos = start | ||
return super.readNumber(startsWithDot) | ||
} | ||
let str = this.input.slice(start, this.pos) | ||
let val = typeof BigInt !== "undefined" && BigInt.parseInt ? BigInt.parseInt(str) : null | ||
++this.pos | ||
return this.finishToken(tt.num, val) | ||
} | ||
} | ||
} |
{ | ||
"name": "acorn-bigint", | ||
"description": "Support for BigInt in acorn", | ||
"homepage": "https://github.com/adrianheine/acorn-bigint", | ||
"homepage": "https://github.com/acornjs/acorn-bigint", | ||
"contributors": [ | ||
@@ -13,5 +13,5 @@ "Adrian Heine <mail@adrianheine.de>" | ||
"type": "git", | ||
"url": "https://github.com/adrianheine/acorn-bigint" | ||
"url": "https://github.com/acornjs/acorn-bigint" | ||
}, | ||
"license": "AGPL-3.0", | ||
"license": "MIT", | ||
"scripts": { | ||
@@ -22,13 +22,14 @@ "test": "mocha", | ||
}, | ||
"dependencies": { | ||
"acorn": "^5.2.1" | ||
"peerDependencies": { | ||
"acorn": "^6.0.0" | ||
}, | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"devDependencies": { | ||
"eslint": "^4.13.1", | ||
"eslint-plugin-node": "^5.2.1", | ||
"mocha": "^4.0.1", | ||
"test262": "git+https://github.com/tc39/test262.git#4fdbb34914eec8b004343fd34f3955b45ee83bc6", | ||
"test262-parser-runner": "^0.2.0" | ||
"acorn": "^6.0.0", | ||
"eslint": "^5.5.0", | ||
"eslint-plugin-node": "^7.0.1", | ||
"mocha": "^5.2.0", | ||
"test262": "git+https://github.com/tc39/test262.git#e286bfa00086226f781a3ed4e0a6295634b8ed11", | ||
"test262-parser-runner": "^0.4.0" | ||
} | ||
} |
@@ -11,24 +11,12 @@ # BigInt support for Acorn | ||
You can use this module directly in order to get an Acorn instance with the plugin installed: | ||
This module provides a plugin that can be used to extend the Acorn `Parser` class: | ||
```javascript | ||
var acorn = require('acorn-bigint'); | ||
const {Parser} = require('acorn'); | ||
const bigInt = require('acorn-bigint'); | ||
Parser.extend(bigInt).parse('100n'); | ||
``` | ||
Or you can use `inject.js` for injecting the plugin into your own version of Acorn like this: | ||
```javascript | ||
var acorn = require('acorn-bigint/inject')(require('./custom-acorn')); | ||
``` | ||
Then, use the `plugins` option to enable the plugiin: | ||
```javascript | ||
var ast = acorn.parse(code, { | ||
plugins: { bigInt: true } | ||
}); | ||
``` | ||
## License | ||
This plugin is released under the [GNU Affero General Public License](./LICENSE). | ||
This plugin is released under an [MIT License](./LICENSE). |
@@ -5,12 +5,14 @@ "use strict" | ||
const run = require("test262-parser-runner") | ||
const parse = require(".").parse | ||
const acorn = require("acorn") | ||
const bigInt = require(".") | ||
const Parser = acorn.Parser.extend(bigInt) | ||
const unsupportedFeatures = ["object-rest", "object-spread", "regexp-named-groups", | ||
"async-iteration", "class-fields", "class-fields-public", | ||
"computed-property-names", // Only used for class fields | ||
"regexp-unicode-property-escapes", | ||
"regexp-lookbehind", "regexp-dotall", "optional-catch-binding"] | ||
const unsupportedFeatures = [ | ||
"async-iteration", | ||
"class-fields-private", | ||
"class-fields-public" | ||
] | ||
run( | ||
(content, options) => parse(content, {sourceType: options.sourceType, ecmaVersion: 9, plugins: { bigInt: true }}), | ||
(content, options) => Parser.parse(content, {sourceType: options.sourceType, ecmaVersion: 9}), | ||
{ | ||
@@ -17,0 +19,0 @@ testsDirectory: path.dirname(require.resolve("test262/package.json")), |
193
test/test.js
"use strict" | ||
const assert = require("assert") | ||
const acorn = require("..") | ||
const acorn = require("acorn") | ||
const Parser = acorn.Parser.extend(require("..")) | ||
function test(text, expectedResult, additionalOptions) { | ||
it(text, function () { | ||
const result = acorn.parse(text, Object.assign({ ecmaVersion: 9, plugins: { bigInt: true } }, additionalOptions)) | ||
assert.deepEqual(result.body[0], expectedResult) | ||
const result = Parser.parse(text, Object.assign({ ecmaVersion: 9 }, additionalOptions)) | ||
assert.deepStrictEqual(result.body[0], expectedResult) | ||
}) | ||
@@ -16,5 +17,5 @@ } | ||
try { | ||
acorn.parse(text, Object.assign({ ecmaVersion: 9, plugins: { bigInt: true } }, additionalOptions)) | ||
Parser.parse(text, Object.assign({ ecmaVersion: 9 }, additionalOptions)) | ||
} catch (e) { | ||
assert.equal(e.message, expectedError) | ||
assert.strictEqual(e.message, expectedError) | ||
failed = true | ||
@@ -26,64 +27,24 @@ } | ||
const maybeBigInt = str => typeof BigInt !== "undefined" && BigInt.parseInt ? BigInt.parseInt(str) : null | ||
const newNode = (start, props) => Object.assign(new acorn.Node({options: {}}, start), props) | ||
const newBigIntLiteral = (start, stringValue) => newNode(start, { | ||
type: "Literal", | ||
end: start + stringValue.length + 1, | ||
value: typeof BigInt !== "undefined" ? BigInt(stringValue) : null, | ||
raw: `${stringValue}n`, | ||
bigint: `${stringValue}n` | ||
}) | ||
describe("acorn-bigint", function () { | ||
const digits = [ | ||
{d: "0", ast: start => ({ | ||
type: "Literal", | ||
start: start, | ||
end: start + 2, | ||
value: maybeBigInt("0"), | ||
raw: "0n", | ||
bigint: "0n" | ||
})}, | ||
{d: "2", ast: start => ({ | ||
type: "Literal", | ||
start: start, | ||
end: start + 2, | ||
value: maybeBigInt("2"), | ||
raw: "2n", | ||
bigint: "2n" | ||
})}, | ||
{d: "0x2", ast: start => ({ | ||
type: "Literal", | ||
start: start, | ||
end: start + 4, | ||
value: maybeBigInt("2"), | ||
raw: "0x2n", | ||
bigint: "0x2n" | ||
})}, | ||
{d: "0o2", ast: start => ({ | ||
type: "Literal", | ||
start: start, | ||
end: start + 4, | ||
value: maybeBigInt("2"), | ||
raw: "0o2n", | ||
bigint: "0o2n" | ||
})}, | ||
{d: "0b10", ast: start => ({ | ||
type: "Literal", | ||
start: start, | ||
end: start + 5, | ||
value: maybeBigInt("2"), | ||
raw: "0b10n", | ||
bigint: "0b10n" | ||
})}, | ||
{d: "-0xbf2ed51ff75d380fd3be813ec6185780", ast: start => ({ | ||
{d: "0", ast: start => newBigIntLiteral(start, "0")}, | ||
{d: "2", ast: start => newBigIntLiteral(start, "2")}, | ||
{d: "0x2", ast: start => newBigIntLiteral(start, "0x2")}, | ||
{d: "0o2", ast: start => newBigIntLiteral(start, "0o2")}, | ||
{d: "0b10", ast: start => newBigIntLiteral(start, "0b10")}, | ||
{d: "-0xbf2ed51ff75d380fd3be813ec6185780", ast: start => newNode(start, { | ||
type: "UnaryExpression", | ||
start: start, | ||
end: start + 36, | ||
operator: "-", | ||
prefix: true, | ||
argument: { | ||
type: "Literal", | ||
start: start + 1, | ||
end: start + 36, | ||
value: maybeBigInt("0xbf2ed51ff75d380fd3be813ec6185780"), | ||
raw: "0xbf2ed51ff75d380fd3be813ec6185780n", | ||
bigint: "0xbf2ed51ff75d380fd3be813ec6185780n" | ||
} | ||
argument: newBigIntLiteral(start + 1, "0xbf2ed51ff75d380fd3be813ec6185780") | ||
})}, | ||
@@ -96,47 +57,39 @@ {d: "02", error: start => `Identifier directly after number (1:${start + 2})`}, | ||
const statements = [ | ||
{s: "let i = %s", ast: content => ({ | ||
{s: "let i = %s", ast: content => newNode(0, { | ||
type: "VariableDeclaration", | ||
start: 0, | ||
end: content.end, | ||
kind: "let", | ||
declarations: [{ | ||
declarations: [newNode(4, { | ||
type: "VariableDeclarator", | ||
start: 4, | ||
end: content.end, | ||
id: { | ||
id: newNode(4, { | ||
type: "Identifier", | ||
start: 4, | ||
end: 5, | ||
name: "i" | ||
}, | ||
}), | ||
init: content | ||
}] | ||
})] | ||
})}, | ||
{s: "i = %s", ast: content => ({ | ||
{s: "i = %s", ast: content => newNode(0, { | ||
type: "ExpressionStatement", | ||
start: 0, | ||
end: content.end, | ||
expression: { | ||
expression: newNode(0, { | ||
type: "AssignmentExpression", | ||
start: 0, | ||
end: content.end, | ||
operator: "=", | ||
left: { | ||
left: newNode(0, { | ||
type: "Identifier", | ||
start: 0, | ||
end: 1, | ||
name: "i" | ||
}, | ||
}), | ||
right: content | ||
} | ||
}) | ||
})}, | ||
{s: "((i = %s) => {})", ast: content => ({ | ||
{s: "((i = %s) => {})", ast: content => newNode(0, { | ||
type: "ExpressionStatement", | ||
start: 0, | ||
end: content.end + 8, | ||
expression: { | ||
expression: newNode(1, { | ||
type: "ArrowFunctionExpression", | ||
start: 1, | ||
end: content.end + 7, | ||
@@ -148,60 +101,45 @@ id: null, | ||
params: [ | ||
{ | ||
newNode(2, { | ||
type: "AssignmentPattern", | ||
start: 2, | ||
end: content.end, | ||
left: { | ||
left: newNode(2, { | ||
type: "Identifier", | ||
start: 2, | ||
end: 3, | ||
name: "i" | ||
}, | ||
}), | ||
right: content | ||
} | ||
}) | ||
], | ||
body: { | ||
body: newNode(content.end + 5, { | ||
type: "BlockStatement", | ||
start: content.end + 5, | ||
end: content.end + 7, | ||
body: [] | ||
} | ||
} | ||
}) | ||
}) | ||
})}, | ||
{s: "for (let i = 0n; i < %s;++i) {}", ast: content => ({ | ||
{s: "for (let i = 0n; i < %s;++i) {}", ast: content => newNode(0, { | ||
type: "ForStatement", | ||
start: 0, | ||
end: content.end + 8, | ||
init: { | ||
init: newNode(5, { | ||
type: "VariableDeclaration", | ||
start: 5, | ||
end: 15, | ||
declarations: [ | ||
{ | ||
newNode(9, { | ||
type: "VariableDeclarator", | ||
start: 9, | ||
end: 15, | ||
id: { | ||
id: newNode(9, { | ||
type: "Identifier", | ||
start: 9, | ||
end: 10, | ||
name: "i" | ||
}, | ||
init: { | ||
type: "Literal", | ||
start: 13, | ||
end: 15, | ||
value: maybeBigInt("0"), | ||
raw: "0n", | ||
bigint: "0n" | ||
} | ||
} | ||
}), | ||
init: newBigIntLiteral(13, "0") | ||
}) | ||
], | ||
kind: "let" | ||
}, | ||
test: { | ||
}), | ||
test: newNode(17, { | ||
type: "BinaryExpression", | ||
start: 17, | ||
end: content.end, | ||
left: { | ||
left: newNode(17, { | ||
type: "Identifier", | ||
@@ -211,36 +149,31 @@ start: 17, | ||
name: "i" | ||
}, | ||
}), | ||
operator: "<", | ||
right: content | ||
}, | ||
update: { | ||
}), | ||
update: newNode(content.end + 1, { | ||
type: "UpdateExpression", | ||
start: content.end + 1, | ||
end: content.end + 4, | ||
operator: "++", | ||
prefix: true, | ||
argument: { | ||
argument: newNode(content.end + 3, { | ||
type: "Identifier", | ||
start: content.end + 3, | ||
end: content.end + 4, | ||
name: "i" | ||
} | ||
}, | ||
body: { | ||
}) | ||
}), | ||
body: newNode(content.end + 6, { | ||
type: "BlockStatement", | ||
start: content.end + 6, | ||
end: content.end + 8, | ||
body: [] | ||
} | ||
}) | ||
})}, | ||
{s: "i + %s", ast: content => ({ | ||
{s: "i + %s", ast: content => newNode(0, { | ||
type: "ExpressionStatement", | ||
start: 0, | ||
end: content.end, | ||
expression: { | ||
expression: newNode(0, { | ||
type: "BinaryExpression", | ||
start: 0, | ||
end: content.end, | ||
left: { | ||
left: newNode(0, { | ||
type: "Identifier", | ||
@@ -250,6 +183,6 @@ start: 0, | ||
name: "i" | ||
}, | ||
}), | ||
operator: "+", | ||
right: content | ||
} | ||
}) | ||
})} | ||
@@ -256,0 +189,0 @@ ] |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
0
100
20101
6
9
547
22
+ Addedacorn@6.4.2(transitive)
- Removedacorn@^5.2.1
- Removedacorn@5.7.4(transitive)