|
| 1 | +"use strict" |
| 2 | + |
| 3 | +constacorn=require('internal/deps/acorn/acorn/dist/acorn') |
| 4 | +consttt=acorn.tokTypes |
| 5 | +constprivateClassElements=require('internal/deps/acorn-plugins/acorn-private-class-elements/index') |
| 6 | + |
| 7 | +functionmaybeParseFieldValue(field){ |
| 8 | +if(this.eat(tt.eq)){ |
| 9 | +constoldInFieldValue=this._inFieldValue |
| 10 | +this._inFieldValue=true |
| 11 | +field.value=this.parseExpression() |
| 12 | +this._inFieldValue=oldInFieldValue |
| 13 | +}elsefield.value=null |
| 14 | +} |
| 15 | + |
| 16 | +module.exports=function(Parser){ |
| 17 | +Parser=privateClassElements(Parser) |
| 18 | +returnclassextendsParser{ |
| 19 | +// Parse fields |
| 20 | +parseClassElement(_constructorAllowsSuper){ |
| 21 | +if(this.options.ecmaVersion>=8&&(this.type==tt.name||this.type==this.privateNameToken||this.type==tt.bracketL||this.type==tt.string)){ |
| 22 | +constbranch=this._branch() |
| 23 | +if(branch.type==tt.bracketL){ |
| 24 | +letcount=0 |
| 25 | +do{ |
| 26 | +if(branch.eat(tt.bracketL))++count |
| 27 | +elseif(branch.eat(tt.bracketR))--count |
| 28 | +elsebranch.next() |
| 29 | +}while(count>0) |
| 30 | +}elsebranch.next() |
| 31 | +if(branch.type==tt.eq||branch.canInsertSemicolon()||branch.type==tt.semi){ |
| 32 | +constnode=this.startNode() |
| 33 | +if(this.type==this.privateNameToken){ |
| 34 | +this.parsePrivateClassElementName(node) |
| 35 | +}else{ |
| 36 | +this.parsePropertyName(node) |
| 37 | +} |
| 38 | +if((node.key.type==="Identifier"&&node.key.name==="constructor")|| |
| 39 | +(node.key.type==="Literal"&&node.key.value==="constructor")){ |
| 40 | +this.raise(node.key.start,"Classes may not have a field called constructor") |
| 41 | +} |
| 42 | +maybeParseFieldValue.call(this,node) |
| 43 | +this.finishNode(node,"FieldDefinition") |
| 44 | +this.semicolon() |
| 45 | +returnnode |
| 46 | +} |
| 47 | +} |
| 48 | + |
| 49 | +returnsuper.parseClassElement.apply(this,arguments) |
| 50 | +} |
| 51 | + |
| 52 | +// Prohibit arguments in class field initializers |
| 53 | +parseIdent(liberal,isBinding){ |
| 54 | +constident=super.parseIdent(liberal,isBinding) |
| 55 | +if(this._inFieldValue&&ident.name=="arguments")this.raise(ident.start,"A class field initializer may not contain arguments") |
| 56 | +returnident |
| 57 | +} |
| 58 | +} |
| 59 | +} |
0 commit comments