acorn-class-fields
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -0,1 +1,5 @@ | ||
## 0.3.1 (2019-02-09) | ||
* Restore compatibility with acorn-private-methods | ||
## 0.3.0 (2019-02-09) | ||
@@ -2,0 +6,0 @@ |
63
index.js
"use strict" | ||
const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g | ||
const acorn = require("acorn") | ||
@@ -21,42 +19,33 @@ const tt = acorn.tokTypes | ||
return class extends Parser { | ||
// Parse private fields | ||
// Parse fields | ||
parseClassElement(_constructorAllowsSuper) { | ||
if (this.eat(tt.semi)) return null | ||
const node = this.startNode() | ||
if (!(this.options.ecmaVersion >= 8) || this.type != this.privateNameToken) { | ||
// Special-case for `async`, since `parseClassMember` currently looks | ||
// for `(` to determine whether `async` is a method name | ||
if (this.isContextual("async")) { | ||
skipWhiteSpace.lastIndex = this.pos | ||
let skip = skipWhiteSpace.exec(this.input) | ||
let next = this.input.charAt(this.pos + skip[0].length) | ||
if (next === ";" || next === "=") { | ||
node.key = this.parseIdent(true) | ||
node.computed = false | ||
maybeParseFieldValue.call(this, node) | ||
this.finishNode(node, "FieldDefinition") | ||
this.semicolon() | ||
return node | ||
if (this.options.ecmaVersion >= 8 && (this.type == tt.name || this.type == this.privateNameToken || this.type == tt.bracketL || this.type == tt.string)) { | ||
const branch = this._branch() | ||
if (branch.type == tt.bracketL) { | ||
let count = 0 | ||
do { | ||
if (branch.eat(tt.bracketL)) ++count | ||
else if (branch.eat(tt.bracketR)) --count | ||
else branch.next() | ||
} while (count > 0) | ||
} else branch.next() | ||
if (branch.type == tt.eq || branch.canInsertSemicolon() || branch.type == tt.semi) { | ||
const node = this.startNode() | ||
if (this.type == this.privateNameToken) { | ||
this.parsePrivateClassElementName(node) | ||
} else { | ||
this.parsePropertyName(node) | ||
} | ||
if ((node.key.type === "Identifier" && node.key.name === "constructor") || | ||
(node.key.type === "Literal" && node.key.value === "constructor")) { | ||
this.raise(node.key.start, "Classes may not have a field called constructor") | ||
} | ||
maybeParseFieldValue.call(this, node) | ||
this.finishNode(node, "FieldDefinition") | ||
this.semicolon() | ||
return node | ||
} | ||
return super.parseClassElement.apply(this, arguments) | ||
} | ||
this.parsePrivateClassElementName(node) | ||
maybeParseFieldValue.call(this, node) | ||
this.finishNode(node, "FieldDefinition") | ||
this.semicolon() | ||
return node | ||
} | ||
// Parse public fields | ||
parseClassMethod(method, isGenerator, isAsync, _allowsDirectSuper) { | ||
if (isGenerator || isAsync || method.kind != "method" || method.static || this.options.ecmaVersion < 8 || this.type == tt.parenL) { | ||
return super.parseClassMethod.apply(this, arguments) | ||
} | ||
maybeParseFieldValue.call(this, method) | ||
delete method.kind | ||
delete method.static | ||
method = this.finishNode(method, "FieldDefinition") | ||
this.semicolon() | ||
return method | ||
return super.parseClassElement.apply(this, arguments) | ||
} | ||
@@ -63,0 +52,0 @@ |
@@ -24,3 +24,3 @@ { | ||
}, | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"devDependencies": { | ||
@@ -35,4 +35,4 @@ "acorn": "^6.1.0", | ||
"dependencies": { | ||
"acorn-private-class-elements": "^0.1.0" | ||
"acorn-private-class-elements": "^0.1.1" | ||
} | ||
} |
@@ -17,10 +17,9 @@ "use strict" | ||
it(text, function () { | ||
let failed = false | ||
let msg = null | ||
try { | ||
Parser.parse(text, Object.assign({ ecmaVersion: 9 }, additionalOptions)) | ||
} catch (e) { | ||
assert.strictEqual(e.message, expectedResult) | ||
failed = true | ||
msg = e.message | ||
} | ||
assert(failed) | ||
assert.strictEqual(msg, expectedResult) | ||
}) | ||
@@ -60,7 +59,7 @@ } | ||
testFail("class A { a = this.#a; b = this.#b }", "Usage of undeclared private name (1:19)") | ||
testFail("class A { constructor = 4 }", "Unexpected token (1:22)") | ||
testFail("class A { constructor = 4 }", "Classes may not have a field called constructor (1:10)") | ||
testFail("class A { #constructor = 4 }", "Classes may not have a private element named constructor (1:10)") | ||
testFail("class A { a = () => arguments }", "A class field initializer may not contain arguments (1:20)") | ||
testFail("class A { a = () => super() }", "'super' keyword outside a method (1:20)") | ||
testFail("class A { # a }", "Unexpected token (1:12)") | ||
testFail("class A { # a }", "Unexpected token (1:10)") | ||
testFail("class A { #a; a() { this.# a } }", "Unexpected token (1:27)") | ||
@@ -67,0 +66,0 @@ |
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
24962
703