You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

es6.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use strict";;
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var core_1 = __importDefault(require("./core"));
  7. var types_1 = __importDefault(require("../lib/types"));
  8. var shared_1 = __importDefault(require("../lib/shared"));
  9. function default_1(fork) {
  10. fork.use(core_1.default);
  11. var types = fork.use(types_1.default);
  12. var def = types.Type.def;
  13. var or = types.Type.or;
  14. var defaults = fork.use(shared_1.default).defaults;
  15. def("Function")
  16. .field("generator", Boolean, defaults["false"])
  17. .field("expression", Boolean, defaults["false"])
  18. .field("defaults", [or(def("Expression"), null)], defaults.emptyArray)
  19. // TODO This could be represented as a RestElement in .params.
  20. .field("rest", or(def("Identifier"), null), defaults["null"]);
  21. // The ESTree way of representing a ...rest parameter.
  22. def("RestElement")
  23. .bases("Pattern")
  24. .build("argument")
  25. .field("argument", def("Pattern"))
  26. .field("typeAnnotation", // for Babylon. Flow parser puts it on the identifier
  27. or(def("TypeAnnotation"), def("TSTypeAnnotation"), null), defaults["null"]);
  28. def("SpreadElementPattern")
  29. .bases("Pattern")
  30. .build("argument")
  31. .field("argument", def("Pattern"));
  32. def("FunctionDeclaration")
  33. .build("id", "params", "body", "generator", "expression");
  34. def("FunctionExpression")
  35. .build("id", "params", "body", "generator", "expression");
  36. // The Parser API calls this ArrowExpression, but Esprima and all other
  37. // actual parsers use ArrowFunctionExpression.
  38. def("ArrowFunctionExpression")
  39. .bases("Function", "Expression")
  40. .build("params", "body", "expression")
  41. // The forced null value here is compatible with the overridden
  42. // definition of the "id" field in the Function interface.
  43. .field("id", null, defaults["null"])
  44. // Arrow function bodies are allowed to be expressions.
  45. .field("body", or(def("BlockStatement"), def("Expression")))
  46. // The current spec forbids arrow generators, so I have taken the
  47. // liberty of enforcing that. TODO Report this.
  48. .field("generator", false, defaults["false"]);
  49. def("ForOfStatement")
  50. .bases("Statement")
  51. .build("left", "right", "body")
  52. .field("left", or(def("VariableDeclaration"), def("Pattern")))
  53. .field("right", def("Expression"))
  54. .field("body", def("Statement"));
  55. def("YieldExpression")
  56. .bases("Expression")
  57. .build("argument", "delegate")
  58. .field("argument", or(def("Expression"), null))
  59. .field("delegate", Boolean, defaults["false"]);
  60. def("GeneratorExpression")
  61. .bases("Expression")
  62. .build("body", "blocks", "filter")
  63. .field("body", def("Expression"))
  64. .field("blocks", [def("ComprehensionBlock")])
  65. .field("filter", or(def("Expression"), null));
  66. def("ComprehensionExpression")
  67. .bases("Expression")
  68. .build("body", "blocks", "filter")
  69. .field("body", def("Expression"))
  70. .field("blocks", [def("ComprehensionBlock")])
  71. .field("filter", or(def("Expression"), null));
  72. def("ComprehensionBlock")
  73. .bases("Node")
  74. .build("left", "right", "each")
  75. .field("left", def("Pattern"))
  76. .field("right", def("Expression"))
  77. .field("each", Boolean);
  78. def("Property")
  79. .field("key", or(def("Literal"), def("Identifier"), def("Expression")))
  80. .field("value", or(def("Expression"), def("Pattern")))
  81. .field("method", Boolean, defaults["false"])
  82. .field("shorthand", Boolean, defaults["false"])
  83. .field("computed", Boolean, defaults["false"]);
  84. def("ObjectProperty")
  85. .field("shorthand", Boolean, defaults["false"]);
  86. def("PropertyPattern")
  87. .bases("Pattern")
  88. .build("key", "pattern")
  89. .field("key", or(def("Literal"), def("Identifier"), def("Expression")))
  90. .field("pattern", def("Pattern"))
  91. .field("computed", Boolean, defaults["false"]);
  92. def("ObjectPattern")
  93. .bases("Pattern")
  94. .build("properties")
  95. .field("properties", [or(def("PropertyPattern"), def("Property"))]);
  96. def("ArrayPattern")
  97. .bases("Pattern")
  98. .build("elements")
  99. .field("elements", [or(def("Pattern"), null)]);
  100. def("MethodDefinition")
  101. .bases("Declaration")
  102. .build("kind", "key", "value", "static")
  103. .field("kind", or("constructor", "method", "get", "set"))
  104. .field("key", def("Expression"))
  105. .field("value", def("Function"))
  106. .field("computed", Boolean, defaults["false"])
  107. .field("static", Boolean, defaults["false"]);
  108. def("SpreadElement")
  109. .bases("Node")
  110. .build("argument")
  111. .field("argument", def("Expression"));
  112. def("ArrayExpression")
  113. .field("elements", [or(def("Expression"), def("SpreadElement"), def("RestElement"), null)]);
  114. def("NewExpression")
  115. .field("arguments", [or(def("Expression"), def("SpreadElement"))]);
  116. def("CallExpression")
  117. .field("arguments", [or(def("Expression"), def("SpreadElement"))]);
  118. // Note: this node type is *not* an AssignmentExpression with a Pattern on
  119. // the left-hand side! The existing AssignmentExpression type already
  120. // supports destructuring assignments. AssignmentPattern nodes may appear
  121. // wherever a Pattern is allowed, and the right-hand side represents a
  122. // default value to be destructured against the left-hand side, if no
  123. // value is otherwise provided. For example: default parameter values.
  124. def("AssignmentPattern")
  125. .bases("Pattern")
  126. .build("left", "right")
  127. .field("left", def("Pattern"))
  128. .field("right", def("Expression"));
  129. var ClassBodyElement = or(def("MethodDefinition"), def("VariableDeclarator"), def("ClassPropertyDefinition"), def("ClassProperty"));
  130. def("ClassProperty")
  131. .bases("Declaration")
  132. .build("key")
  133. .field("key", or(def("Literal"), def("Identifier"), def("Expression")))
  134. .field("computed", Boolean, defaults["false"]);
  135. def("ClassPropertyDefinition") // static property
  136. .bases("Declaration")
  137. .build("definition")
  138. // Yes, Virginia, circular definitions are permitted.
  139. .field("definition", ClassBodyElement);
  140. def("ClassBody")
  141. .bases("Declaration")
  142. .build("body")
  143. .field("body", [ClassBodyElement]);
  144. def("ClassDeclaration")
  145. .bases("Declaration")
  146. .build("id", "body", "superClass")
  147. .field("id", or(def("Identifier"), null))
  148. .field("body", def("ClassBody"))
  149. .field("superClass", or(def("Expression"), null), defaults["null"]);
  150. def("ClassExpression")
  151. .bases("Expression")
  152. .build("id", "body", "superClass")
  153. .field("id", or(def("Identifier"), null), defaults["null"])
  154. .field("body", def("ClassBody"))
  155. .field("superClass", or(def("Expression"), null), defaults["null"]);
  156. // Specifier and ModuleSpecifier are abstract non-standard types
  157. // introduced for definitional convenience.
  158. def("Specifier").bases("Node");
  159. // This supertype is shared/abused by both def/babel.js and
  160. // def/esprima.js. In the future, it will be possible to load only one set
  161. // of definitions appropriate for a given parser, but until then we must
  162. // rely on default functions to reconcile the conflicting AST formats.
  163. def("ModuleSpecifier")
  164. .bases("Specifier")
  165. // This local field is used by Babel/Acorn. It should not technically
  166. // be optional in the Babel/Acorn AST format, but it must be optional
  167. // in the Esprima AST format.
  168. .field("local", or(def("Identifier"), null), defaults["null"])
  169. // The id and name fields are used by Esprima. The id field should not
  170. // technically be optional in the Esprima AST format, but it must be
  171. // optional in the Babel/Acorn AST format.
  172. .field("id", or(def("Identifier"), null), defaults["null"])
  173. .field("name", or(def("Identifier"), null), defaults["null"]);
  174. // Like ModuleSpecifier, except type:"ImportSpecifier" and buildable.
  175. // import {<id [as name]>} from ...;
  176. def("ImportSpecifier")
  177. .bases("ModuleSpecifier")
  178. .build("id", "name");
  179. // import <* as id> from ...;
  180. def("ImportNamespaceSpecifier")
  181. .bases("ModuleSpecifier")
  182. .build("id");
  183. // import <id> from ...;
  184. def("ImportDefaultSpecifier")
  185. .bases("ModuleSpecifier")
  186. .build("id");
  187. def("ImportDeclaration")
  188. .bases("Declaration")
  189. .build("specifiers", "source", "importKind")
  190. .field("specifiers", [or(def("ImportSpecifier"), def("ImportNamespaceSpecifier"), def("ImportDefaultSpecifier"))], defaults.emptyArray)
  191. .field("source", def("Literal"))
  192. .field("importKind", or("value", "type"), function () {
  193. return "value";
  194. });
  195. def("TaggedTemplateExpression")
  196. .bases("Expression")
  197. .build("tag", "quasi")
  198. .field("tag", def("Expression"))
  199. .field("quasi", def("TemplateLiteral"));
  200. def("TemplateLiteral")
  201. .bases("Expression")
  202. .build("quasis", "expressions")
  203. .field("quasis", [def("TemplateElement")])
  204. .field("expressions", [def("Expression")]);
  205. def("TemplateElement")
  206. .bases("Node")
  207. .build("value", "tail")
  208. .field("value", { "cooked": String, "raw": String })
  209. .field("tail", Boolean);
  210. }
  211. exports.default = default_1;
  212. module.exports = exports["default"];