|
342 | 342 | // Can be either `"script"`, `"module"` or `"commonjs"`. This influences global |
343 | 343 | // strict mode and parsing of `import` and `export` declarations. |
344 | 344 | sourceType: "script", |
| 345 | +// When set to true, enable strict parsing mode even if `sourceType` |
| 346 | +// is `"script"`. |
| 347 | +strict: false, |
345 | 348 | // `onInsertedSemicolon` can be a callback that will be called when |
346 | 349 | // a semicolon is automatically inserted. It will be passed the |
347 | 350 | // position of the inserted semicolon as an offset, and if |
|
568 | 571 |
|
569 | 572 | // Figure out if it's a module code. |
570 | 573 | this.inModule=options.sourceType==="module"; |
571 | | -this.strict=this.inModule||this.strictDirective(this.pos); |
| 574 | +this.strict=this.inModule||options.strict===true||this.strictDirective(this.pos); |
572 | 575 |
|
573 | 576 | // Used to signify the start of a potential arrow function |
574 | 577 | this.potentialArrowAt=-1; |
|
606 | 609 | varprototypeAccessors={inFunction: {configurable: true},inGenerator: {configurable: true},inAsync: {configurable: true},canAwait: {configurable: true},allowReturn: {configurable: true},allowSuper: {configurable: true},allowDirectSuper: {configurable: true},treatFunctionsAsVar: {configurable: true},allowNewDotTarget: {configurable: true},allowUsing: {configurable: true},inClassStaticBlock: {configurable: true}}; |
607 | 610 |
|
608 | 611 | Parser.prototype.parse=functionparse(){ |
| 612 | +varthis$1$1=this; |
| 613 | + |
609 | 614 | varnode=this.options.program||this.startNode(); |
610 | 615 | this.nextToken(); |
611 | | -returnthis.parseTopLevel(node) |
| 616 | +returnthis.catchStackOverflow(function(){returnthis$1$1.parseTopLevel(node);}) |
612 | 617 | }; |
613 | 618 |
|
614 | 619 | prototypeAccessors.inFunction.get=function(){return(this.currentVarScope().flags&SCOPE_FUNCTION)>0}; |
|
747 | 752 | returntrue |
748 | 753 | }; |
749 | 754 |
|
| 755 | +pp$9.catchStackOverflow=function(f){ |
| 756 | +try{ |
| 757 | +returnf() |
| 758 | +}catch(e){ |
| 759 | +if(einstanceofError&&(/\bstack\b.*\b(exceeded|overflow)\b/i.test(e.message)||/\btoomuchrecursion\b/i.test(e.message))) |
| 760 | +{this.raise(this.start,"Not enough stack space to parse input");} |
| 761 | +else |
| 762 | +{throwe} |
| 763 | +} |
| 764 | +}; |
| 765 | + |
750 | 766 | // Asserts that following token is given contextual keyword. |
751 | 767 |
|
752 | 768 | pp$9.expectContextual=function(name){ |
|
850 | 866 | // to its body instead of creating a new node. |
851 | 867 |
|
852 | 868 | pp$8.parseTopLevel=function(node){ |
853 | | -varexports=Object.create(null); |
| 869 | +varexports$1=Object.create(null); |
854 | 870 | if(!node.body){node.body=[];} |
855 | 871 | while(this.type!==types$1.eof){ |
856 | | -varstmt=this.parseStatement(null,true,exports); |
| 872 | +varstmt=this.parseStatement(null,true,exports$1); |
857 | 873 | node.body.push(stmt); |
858 | 874 | } |
859 | 875 | if(this.inModule) |
|
942 | 958 | while(isIdentifierChar(ch=this.fullCharCodeAt(next))) |
943 | 959 | if(ch===92){returntrue} |
944 | 960 | varid=this.input.slice(idStart,next); |
945 | | -if(keywordRelationalOperator.test(id)||isFor&&id==="of"){returnfalse} |
| 961 | +if(keywordRelationalOperator.test(id)){returnfalse} |
| 962 | +if(isFor&&!isAwaitUsing&&id==="of"){ |
| 963 | +// Look ahead for using declaration with initializer, i.e., `for (using of = ...)` |
| 964 | +skipWhiteSpace.lastIndex=next; |
| 965 | +varskipAfterOf=skipWhiteSpace.exec(this.input); |
| 966 | +next=next+skipAfterOf[0].length; |
| 967 | +if(this.input.charCodeAt(next)!==61/* '=' */|| |
| 968 | +// Check for ==, === and => operators |
| 969 | +(ch=this.input.charCodeAt(next+1))===61/* '=' */||ch===62/* '>' */){ |
| 970 | +returnfalse |
| 971 | +} |
| 972 | +} |
946 | 973 | returntrue |
947 | 974 | }; |
948 | 975 |
|
|
961 | 988 | // `if (foo) /blah/.exec(foo)`, where looking at the previous token |
962 | 989 | // does not help. |
963 | 990 |
|
964 | | -pp$8.parseStatement=function(context,topLevel,exports){ |
| 991 | +pp$8.parseStatement=function(context,topLevel,exports$1){ |
965 | 992 | varstarttype=this.type,node=this.startNode(),kind; |
966 | 993 |
|
967 | 994 | if(this.isLet(context)){ |
|
1016 | 1043 | if(!this.inModule) |
1017 | 1044 | {this.raise(this.start,"'import' and 'export' may appear only with 'sourceType: module'");} |
1018 | 1045 | } |
1019 | | -returnstarttype===types$1._import ? this.parseImport(node) : this.parseExport(node,exports) |
| 1046 | +returnstarttype===types$1._import ? this.parseImport(node) : this.parseExport(node,exports$1) |
1020 | 1047 |
|
1021 | 1048 | // If the statement does not start with a statement keyword or a |
1022 | 1049 | // brace, it's an ExpressionStatement or LabeledStatement. We |
|
1035 | 1062 | if(!this.allowUsing){ |
1036 | 1063 | this.raise(this.start,"Using declaration cannot appear in the top level when source type is `script` or in the bare case statement"); |
1037 | 1064 | } |
| 1065 | +if(context){ |
| 1066 | +// Cases like `for (;;) using x = ...;`, `if (true) await using x = ...;`, etc. are not allowed. |
| 1067 | +this.raise(this.start,"Using declaration is not allowed in single-statement positions"); |
| 1068 | +} |
1038 | 1069 | if(usingKind==="await using"){ |
1039 | 1070 | if(!this.canAwait){ |
1040 | 1071 | this.raise(this.start,"Await using cannot appear outside of async function"); |
|
1168 | 1199 | // Helper method to parse for loop after variable initialization |
1169 | 1200 | pp$8.parseForAfterInit=function(node,init,awaitAt){ |
1170 | 1201 | if((this.type===types$1._in||(this.options.ecmaVersion>=6&&this.isContextual("of")))&&init.declarations.length===1){ |
1171 | | -if(this.options.ecmaVersion>=9){ |
1172 | | -if(this.type===types$1._in){ |
1173 | | -if(awaitAt>-1){this.unexpected(awaitAt);} |
1174 | | -}else{node.await=awaitAt>-1;} |
1175 | | -} |
| 1202 | +if(this.type===types$1._in){ |
| 1203 | +if((init.kind==="using"||init.kind==="await using")&&!init.declarations[0].init){ |
| 1204 | +this.raise(this.start,"Using declaration is not allowed in for-in loops"); |
| 1205 | +} |
| 1206 | +if(this.options.ecmaVersion>=9&&awaitAt>-1){this.unexpected(awaitAt);} |
| 1207 | +}elseif(this.options.ecmaVersion>=9){node.await=awaitAt>-1;} |
1176 | 1208 | returnthis.parseForIn(node,init) |
1177 | 1209 | } |
1178 | 1210 | if(awaitAt>-1){this.unexpected(awaitAt);} |
|
1777 | 1809 |
|
1778 | 1810 | // Parses module export declaration. |
1779 | 1811 |
|
1780 | | -pp$8.parseExportAllDeclaration=function(node,exports){ |
| 1812 | +pp$8.parseExportAllDeclaration=function(node,exports$1){ |
1781 | 1813 | if(this.options.ecmaVersion>=11){ |
1782 | 1814 | if(this.eatContextual("as")){ |
1783 | 1815 | node.exported=this.parseModuleExportName(); |
1784 | | -this.checkExport(exports,node.exported,this.lastTokStart); |
| 1816 | +this.checkExport(exports$1,node.exported,this.lastTokStart); |
1785 | 1817 | }else{ |
1786 | 1818 | node.exported=null; |
1787 | 1819 | } |
|
1795 | 1827 | returnthis.finishNode(node,"ExportAllDeclaration") |
1796 | 1828 | }; |
1797 | 1829 |
|
1798 | | -pp$8.parseExport=function(node,exports){ |
| 1830 | +pp$8.parseExport=function(node,exports$1){ |
1799 | 1831 | this.next(); |
1800 | 1832 | // export * from '...' |
1801 | 1833 | if(this.eat(types$1.star)){ |
1802 | | -returnthis.parseExportAllDeclaration(node,exports) |
| 1834 | +returnthis.parseExportAllDeclaration(node,exports$1) |
1803 | 1835 | } |
1804 | 1836 | if(this.eat(types$1._default)){// export default ... |
1805 | | -this.checkExport(exports,"default",this.lastTokStart); |
| 1837 | +this.checkExport(exports$1,"default",this.lastTokStart); |
1806 | 1838 | node.declaration=this.parseExportDefaultDeclaration(); |
1807 | 1839 | returnthis.finishNode(node,"ExportDefaultDeclaration") |
1808 | 1840 | } |
1809 | 1841 | // export var|const|let|function|class ... |
1810 | 1842 | if(this.shouldParseExportStatement()){ |
1811 | 1843 | node.declaration=this.parseExportDeclaration(node); |
1812 | 1844 | if(node.declaration.type==="VariableDeclaration") |
1813 | | -{this.checkVariableExport(exports,node.declaration.declarations);} |
| 1845 | +{this.checkVariableExport(exports$1,node.declaration.declarations);} |
1814 | 1846 | else |
1815 | | -{this.checkExport(exports,node.declaration.id,node.declaration.id.start);} |
| 1847 | +{this.checkExport(exports$1,node.declaration.id,node.declaration.id.start);} |
1816 | 1848 | node.specifiers=[]; |
1817 | 1849 | node.source=null; |
1818 | 1850 | if(this.options.ecmaVersion>=16) |
1819 | 1851 | {node.attributes=[];} |
1820 | 1852 | }else{// export { x, y as z } [from '...'] |
1821 | 1853 | node.declaration=null; |
1822 | | -node.specifiers=this.parseExportSpecifiers(exports); |
| 1854 | +node.specifiers=this.parseExportSpecifiers(exports$1); |
1823 | 1855 | if(this.eatContextual("from")){ |
1824 | 1856 | if(this.type!==types$1.string){this.unexpected();} |
1825 | 1857 | node.source=this.parseExprAtom(); |
|
1869 | 1901 | } |
1870 | 1902 | }; |
1871 | 1903 |
|
1872 | | -pp$8.checkExport=function(exports,name,pos){ |
1873 | | -if(!exports){return} |
| 1904 | +pp$8.checkExport=function(exports$1,name,pos){ |
| 1905 | +if(!exports$1){return} |
1874 | 1906 | if(typeofname!=="string") |
1875 | 1907 | {name=name.type==="Identifier" ? name.name : name.value;} |
1876 | | -if(hasOwn(exports,name)) |
| 1908 | +if(hasOwn(exports$1,name)) |
1877 | 1909 | {this.raiseRecoverable(pos,"Duplicate export '"+name+"'");} |
1878 | | -exports[name]=true; |
| 1910 | +exports$1[name]=true; |
1879 | 1911 | }; |
1880 | 1912 |
|
1881 | | -pp$8.checkPatternExport=function(exports,pat){ |
| 1913 | +pp$8.checkPatternExport=function(exports$1,pat){ |
1882 | 1914 | vartype=pat.type; |
1883 | 1915 | if(type==="Identifier") |
1884 | | -{this.checkExport(exports,pat,pat.start);} |
| 1916 | +{this.checkExport(exports$1,pat,pat.start);} |
1885 | 1917 | elseif(type==="ObjectPattern") |
1886 | 1918 | {for(vari=0,list=pat.properties;i<list.length;i+=1) |
1887 | 1919 | { |
1888 | 1920 | varprop=list[i]; |
1889 | 1921 |
|
1890 | | -this.checkPatternExport(exports,prop); |
| 1922 | +this.checkPatternExport(exports$1,prop); |
1891 | 1923 | }} |
1892 | 1924 | elseif(type==="ArrayPattern") |
1893 | 1925 | {for(vari$1=0,list$1=pat.elements;i$1<list$1.length;i$1+=1){ |
1894 | 1926 | varelt=list$1[i$1]; |
1895 | 1927 |
|
1896 | | -if(elt){this.checkPatternExport(exports,elt);} |
| 1928 | +if(elt){this.checkPatternExport(exports$1,elt);} |
1897 | 1929 | }} |
1898 | 1930 | elseif(type==="Property") |
1899 | | -{this.checkPatternExport(exports,pat.value);} |
| 1931 | +{this.checkPatternExport(exports$1,pat.value);} |
1900 | 1932 | elseif(type==="AssignmentPattern") |
1901 | | -{this.checkPatternExport(exports,pat.left);} |
| 1933 | +{this.checkPatternExport(exports$1,pat.left);} |
1902 | 1934 | elseif(type==="RestElement") |
1903 | | -{this.checkPatternExport(exports,pat.argument);} |
| 1935 | +{this.checkPatternExport(exports$1,pat.argument);} |
1904 | 1936 | }; |
1905 | 1937 |
|
1906 | | -pp$8.checkVariableExport=function(exports,decls){ |
1907 | | -if(!exports){return} |
| 1938 | +pp$8.checkVariableExport=function(exports$1,decls){ |
| 1939 | +if(!exports$1){return} |
1908 | 1940 | for(vari=0,list=decls;i<list.length;i+=1) |
1909 | 1941 | { |
1910 | 1942 | vardecl=list[i]; |
1911 | 1943 |
|
1912 | | -this.checkPatternExport(exports,decl.id); |
| 1944 | +this.checkPatternExport(exports$1,decl.id); |
1913 | 1945 | } |
1914 | 1946 | }; |
1915 | 1947 |
|
|
1924 | 1956 |
|
1925 | 1957 | // Parses a comma-separated list of module exports. |
1926 | 1958 |
|
1927 | | -pp$8.parseExportSpecifier=function(exports){ |
| 1959 | +pp$8.parseExportSpecifier=function(exports$1){ |
1928 | 1960 | varnode=this.startNode(); |
1929 | 1961 | node.local=this.parseModuleExportName(); |
1930 | 1962 |
|
1931 | 1963 | node.exported=this.eatContextual("as") ? this.parseModuleExportName() : node.local; |
1932 | 1964 | this.checkExport( |
1933 | | -exports, |
| 1965 | +exports$1, |
1934 | 1966 | node.exported, |
1935 | 1967 | node.exported.start |
1936 | 1968 | ); |
1937 | 1969 |
|
1938 | 1970 | returnthis.finishNode(node,"ExportSpecifier") |
1939 | 1971 | }; |
1940 | 1972 |
|
1941 | | -pp$8.parseExportSpecifiers=function(exports){ |
| 1973 | +pp$8.parseExportSpecifiers=function(exports$1){ |
1942 | 1974 | varnodes=[],first=true; |
1943 | 1975 | // export { x, y as z } [from '...'] |
1944 | 1976 | this.expect(types$1.braceL); |
|
1948 | 1980 | if(this.afterTrailingComma(types$1.braceR)){break} |
1949 | 1981 | }else{first=false;} |
1950 | 1982 |
|
1951 | | -nodes.push(this.parseExportSpecifier(exports)); |
| 1983 | +nodes.push(this.parseExportSpecifier(exports$1)); |
1952 | 1984 | } |
1953 | 1985 | returnnodes |
1954 | 1986 | }; |
|
2679 | 2711 | // delayed syntax error at correct position). |
2680 | 2712 |
|
2681 | 2713 | pp$5.parseExpression=function(forInit,refDestructuringErrors){ |
2682 | | -varstartPos=this.start,startLoc=this.startLoc; |
2683 | | -varexpr=this.parseMaybeAssign(forInit,refDestructuringErrors); |
2684 | | -if(this.type===types$1.comma){ |
2685 | | -varnode=this.startNodeAt(startPos,startLoc); |
2686 | | -node.expressions=[expr]; |
2687 | | -while(this.eat(types$1.comma)){node.expressions.push(this.parseMaybeAssign(forInit,refDestructuringErrors));} |
2688 | | -returnthis.finishNode(node,"SequenceExpression") |
2689 | | -} |
2690 | | -returnexpr |
| 2714 | +varthis$1$1=this; |
| 2715 | + |
| 2716 | +returnthis.catchStackOverflow(function(){ |
| 2717 | +varstartPos=this$1$1.start,startLoc=this$1$1.startLoc; |
| 2718 | +varexpr=this$1$1.parseMaybeAssign(forInit,refDestructuringErrors); |
| 2719 | +if(this$1$1.type===types$1.comma){ |
| 2720 | +varnode=this$1$1.startNodeAt(startPos,startLoc); |
| 2721 | +node.expressions=[expr]; |
| 2722 | +while(this$1$1.eat(types$1.comma)){node.expressions.push(this$1$1.parseMaybeAssign(forInit,refDestructuringErrors));} |
| 2723 | +returnthis$1$1.finishNode(node,"SequenceExpression") |
| 2724 | +} |
| 2725 | +returnexpr |
| 2726 | +}) |
2691 | 2727 | }; |
2692 | 2728 |
|
2693 | 2729 | // Parse an assignment expression. This includes applications of |
|
2752 | 2788 | varstartPos=this.start,startLoc=this.startLoc; |
2753 | 2789 | varexpr=this.parseExprOps(forInit,refDestructuringErrors); |
2754 | 2790 | if(this.checkExpressionErrors(refDestructuringErrors)){returnexpr} |
2755 | | -if(this.eat(types$1.question)){ |
| 2791 | +if(!(expr.type==="ArrowFunctionExpression"&&expr.start===startPos)&&this.eat(types$1.question)){ |
2756 | 2792 | varnode=this.startNodeAt(startPos,startLoc); |
2757 | 2793 | node.test=expr; |
2758 | 2794 | node.consequent=this.parseMaybeAssign(); |
|
3304 | 3340 | } |
3305 | 3341 | varstartPos=this.start,startLoc=this.startLoc; |
3306 | 3342 | node.callee=this.parseSubscripts(this.parseExprAtom(null,false,true),startPos,startLoc,true,false); |
| 3343 | +if(node.callee.type==="Super") |
| 3344 | +{this.raiseRecoverable(startPos,"Invalid use of 'super'");} |
3307 | 3345 | if(this.eat(types$1.parenL)){node.arguments=this.parseExprList(types$1.parenR,this.options.ecmaVersion>=8,false);} |
3308 | 3346 | else{node.arguments=empty;} |
3309 | 3347 | returnthis.finishNode(node,"NewExpression") |
|
6220 | 6258 | // [ghbt]: https://github.com/acornjs/acorn/issues |
6221 | 6259 |
|
6222 | 6260 |
|
6223 | | -varversion="8.16.0"; |
| 6261 | +varversion="8.17.0"; |
6224 | 6262 |
|
6225 | 6263 | Parser.acorn={ |
6226 | 6264 | Parser: Parser, |
|
0 commit comments