ast-types
Advanced tools
Comparing version 0.11.7 to 0.12.0
@@ -1,298 +0,258 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./es7")); | ||
var types = fork.use(require("../lib/types")); | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
def("Noop") | ||
.bases("Statement") | ||
.build(); | ||
def("DoExpression") | ||
.bases("Expression") | ||
.build("body") | ||
.field("body", [def("Statement")]); | ||
def("Super") | ||
.bases("Expression") | ||
.build(); | ||
def("BindExpression") | ||
.bases("Expression") | ||
.build("object", "callee") | ||
.field("object", or(def("Expression"), null)) | ||
.field("callee", def("Expression")); | ||
def("Decorator") | ||
.bases("Node") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("Property") | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]); | ||
def("MethodDefinition") | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]); | ||
def("MetaProperty") | ||
.bases("Expression") | ||
.build("meta", "property") | ||
.field("meta", def("Identifier")) | ||
.field("property", def("Identifier")); | ||
def("ParenthesizedExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("ImportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("imported", "local") | ||
.field("imported", def("Identifier")); | ||
def("ImportDefaultSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local"); | ||
def("ImportNamespaceSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local"); | ||
def("ExportDefaultDeclaration") | ||
.bases("Declaration") | ||
.build("declaration") | ||
.field("declaration", or(def("Declaration"), def("Expression"))); | ||
def("ExportNamedDeclaration") | ||
.bases("Declaration") | ||
.build("declaration", "specifiers", "source") | ||
.field("declaration", or(def("Declaration"), null)) | ||
.field("specifiers", [def("ExportSpecifier")], defaults.emptyArray) | ||
.field("source", or(def("Literal"), null), defaults["null"]); | ||
def("ExportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local", "exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportNamespaceSpecifier") | ||
.bases("Specifier") | ||
.build("exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportDefaultSpecifier") | ||
.bases("Specifier") | ||
.build("exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportAllDeclaration") | ||
.bases("Declaration") | ||
.build("exported", "source") | ||
.field("exported", or(def("Identifier"), null)) | ||
.field("source", def("Literal")); | ||
def("CommentBlock") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("CommentLine") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("Directive") | ||
.bases("Node") | ||
.build("value") | ||
.field("value", def("DirectiveLiteral")); | ||
def("DirectiveLiteral") | ||
.bases("Node", "Expression") | ||
.build("value") | ||
.field("value", String, defaults["use strict"]); | ||
def("BlockStatement") | ||
.bases("Statement") | ||
.build("body") | ||
.field("body", [def("Statement")]) | ||
.field("directives", [def("Directive")], defaults.emptyArray); | ||
def("Program") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [def("Statement")]) | ||
.field("directives", [def("Directive")], defaults.emptyArray); | ||
// Split Literal | ||
def("StringLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", String); | ||
def("NumericLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", Number) | ||
.field("raw", or(String, null), defaults["null"]) | ||
.field("extra", { | ||
rawValue: Number, | ||
raw: String | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
var es7_1 = __importDefault(require("./es7")); | ||
function default_1(fork) { | ||
fork.use(es7_1.default); | ||
var types = fork.use(types_1.default); | ||
var defaults = fork.use(shared_1.default).defaults; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
def("Noop") | ||
.bases("Statement") | ||
.build(); | ||
def("DoExpression") | ||
.bases("Expression") | ||
.build("body") | ||
.field("body", [def("Statement")]); | ||
def("Super") | ||
.bases("Expression") | ||
.build(); | ||
def("BindExpression") | ||
.bases("Expression") | ||
.build("object", "callee") | ||
.field("object", or(def("Expression"), null)) | ||
.field("callee", def("Expression")); | ||
def("Decorator") | ||
.bases("Node") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("Property") | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]); | ||
def("MethodDefinition") | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]); | ||
def("MetaProperty") | ||
.bases("Expression") | ||
.build("meta", "property") | ||
.field("meta", def("Identifier")) | ||
.field("property", def("Identifier")); | ||
def("ParenthesizedExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("ImportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("imported", "local") | ||
.field("imported", def("Identifier")); | ||
def("ImportDefaultSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local"); | ||
def("ImportNamespaceSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local"); | ||
def("ExportDefaultDeclaration") | ||
.bases("Declaration") | ||
.build("declaration") | ||
.field("declaration", or(def("Declaration"), def("Expression"))); | ||
def("ExportNamedDeclaration") | ||
.bases("Declaration") | ||
.build("declaration", "specifiers", "source") | ||
.field("declaration", or(def("Declaration"), null)) | ||
.field("specifiers", [def("ExportSpecifier")], defaults.emptyArray) | ||
.field("source", or(def("Literal"), null), defaults["null"]); | ||
def("ExportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("local", "exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportNamespaceSpecifier") | ||
.bases("Specifier") | ||
.build("exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportDefaultSpecifier") | ||
.bases("Specifier") | ||
.build("exported") | ||
.field("exported", def("Identifier")); | ||
def("ExportAllDeclaration") | ||
.bases("Declaration") | ||
.build("exported", "source") | ||
.field("exported", or(def("Identifier"), null)) | ||
.field("source", def("Literal")); | ||
def("CommentBlock") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("CommentLine") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("Directive") | ||
.bases("Node") | ||
.build("value") | ||
.field("value", def("DirectiveLiteral")); | ||
def("DirectiveLiteral") | ||
.bases("Node", "Expression") | ||
.build("value") | ||
.field("value", String, defaults["use strict"]); | ||
def("InterpreterDirective") | ||
.bases("Node") | ||
.build("value") | ||
.field("value", String); | ||
def("BlockStatement") | ||
.bases("Statement") | ||
.build("body") | ||
.field("body", [def("Statement")]) | ||
.field("directives", [def("Directive")], defaults.emptyArray); | ||
def("Program") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [def("Statement")]) | ||
.field("directives", [def("Directive")], defaults.emptyArray) | ||
.field("interpreter", or(def("InterpreterDirective"), null), defaults["null"]); | ||
// Split Literal | ||
def("StringLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", String); | ||
def("NumericLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", Number) | ||
.field("raw", or(String, null), defaults["null"]) | ||
.field("extra", { | ||
rawValue: Number, | ||
raw: String | ||
}, function getDefault() { | ||
return { | ||
rawValue: this.value, | ||
raw: this.value + "" | ||
} | ||
return { | ||
rawValue: this.value, | ||
raw: this.value + "" | ||
}; | ||
}); | ||
def("BigIntLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
// Only String really seems appropriate here, since BigInt values | ||
// often exceed the limits of JS numbers. | ||
.field("value", or(String, Number)) | ||
.field("extra", { | ||
rawValue: String, | ||
raw: String | ||
def("BigIntLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
// Only String really seems appropriate here, since BigInt values | ||
// often exceed the limits of JS numbers. | ||
.field("value", or(String, Number)) | ||
.field("extra", { | ||
rawValue: String, | ||
raw: String | ||
}, function getDefault() { | ||
return { | ||
rawValue: String(this.value), | ||
raw: this.value + "n" | ||
}; | ||
return { | ||
rawValue: String(this.value), | ||
raw: this.value + "n" | ||
}; | ||
}); | ||
def("NullLiteral") | ||
.bases("Literal") | ||
.build() | ||
.field("value", null, defaults["null"]); | ||
def("BooleanLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", Boolean); | ||
def("RegExpLiteral") | ||
.bases("Literal") | ||
.build("pattern", "flags") | ||
.field("pattern", String) | ||
.field("flags", String) | ||
.field("value", RegExp, function () { | ||
return new RegExp(this.pattern, this.flags); | ||
def("NullLiteral") | ||
.bases("Literal") | ||
.build() | ||
.field("value", null, defaults["null"]); | ||
def("BooleanLiteral") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", Boolean); | ||
def("RegExpLiteral") | ||
.bases("Literal") | ||
.build("pattern", "flags") | ||
.field("pattern", String) | ||
.field("flags", String) | ||
.field("value", RegExp, function () { | ||
return new RegExp(this.pattern, this.flags); | ||
}); | ||
var ObjectExpressionProperty = or( | ||
def("Property"), | ||
def("ObjectMethod"), | ||
def("ObjectProperty"), | ||
def("SpreadProperty"), | ||
def("SpreadElement") | ||
); | ||
// Split Property -> ObjectProperty and ObjectMethod | ||
def("ObjectExpression") | ||
.bases("Expression") | ||
.build("properties") | ||
.field("properties", [ObjectExpressionProperty]); | ||
// ObjectMethod hoist .value properties to own properties | ||
def("ObjectMethod") | ||
.bases("Node", "Function") | ||
.build("kind", "key", "params", "body", "computed") | ||
.field("kind", or("method", "get", "set")) | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("params", [def("Pattern")]) | ||
.field("body", def("BlockStatement")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]) | ||
.field("accessibility", // TypeScript | ||
or(def("Literal"), null), | ||
defaults["null"]) | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]); | ||
def("ObjectProperty") | ||
.bases("Node") | ||
.build("key", "value") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("value", or(def("Expression"), def("Pattern"))) | ||
.field("accessibility", // TypeScript | ||
or(def("Literal"), null), | ||
defaults["null"]) | ||
.field("computed", Boolean, defaults["false"]); | ||
var ClassBodyElement = or( | ||
def("MethodDefinition"), | ||
def("VariableDeclarator"), | ||
def("ClassPropertyDefinition"), | ||
def("ClassProperty"), | ||
var ObjectExpressionProperty = or(def("Property"), def("ObjectMethod"), def("ObjectProperty"), def("SpreadProperty"), def("SpreadElement")); | ||
// Split Property -> ObjectProperty and ObjectMethod | ||
def("ObjectExpression") | ||
.bases("Expression") | ||
.build("properties") | ||
.field("properties", [ObjectExpressionProperty]); | ||
// ObjectMethod hoist .value properties to own properties | ||
def("ObjectMethod") | ||
.bases("Node", "Function") | ||
.build("kind", "key", "params", "body", "computed") | ||
.field("kind", or("method", "get", "set")) | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("params", [def("Pattern")]) | ||
.field("body", def("BlockStatement")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]) | ||
.field("accessibility", // TypeScript | ||
or(def("Literal"), null), defaults["null"]) | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]); | ||
def("ObjectProperty") | ||
.bases("Node") | ||
.build("key", "value") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("value", or(def("Expression"), def("Pattern"))) | ||
.field("accessibility", // TypeScript | ||
or(def("Literal"), null), defaults["null"]) | ||
.field("computed", Boolean, defaults["false"]); | ||
var ClassBodyElement = or(def("MethodDefinition"), def("VariableDeclarator"), def("ClassPropertyDefinition"), def("ClassProperty"), def("ClassPrivateProperty"), def("ClassMethod"), def("ClassPrivateMethod")); | ||
// MethodDefinition -> ClassMethod | ||
def("ClassBody") | ||
.bases("Declaration") | ||
.build("body") | ||
.field("body", [ClassBodyElement]); | ||
def("ClassMethod") | ||
); | ||
// MethodDefinition -> ClassMethod | ||
def("ClassBody") | ||
.bases("Declaration") | ||
.build("body") | ||
.field("body", [ClassBodyElement]); | ||
def("ClassMethod") | ||
.bases("Declaration", "Function") | ||
.build("kind", "key", "params", "body", "computed", "static") | ||
.field("kind", or("get", "set", "method", "constructor")) | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("params", [def("Pattern")]) | ||
.field("body", def("BlockStatement")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("static", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]) | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]); | ||
var ObjectPatternProperty = or( | ||
def("Property"), | ||
def("PropertyPattern"), | ||
def("SpreadPropertyPattern"), | ||
def("SpreadProperty"), // Used by Esprima | ||
.bases("Declaration", "Function") | ||
.build("kind", "key", "params", "body", "computed", "static") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))); | ||
def("ClassPrivateMethod") | ||
.bases("Declaration", "Function") | ||
.build("key", "params", "body", "kind", "computed", "static") | ||
.field("key", def("PrivateName")); | ||
["ClassMethod", | ||
"ClassPrivateMethod", | ||
].forEach(function (typeName) { | ||
def(typeName) | ||
.field("kind", or("get", "set", "method", "constructor"), function () { return "method"; }) | ||
.field("body", def("BlockStatement")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("static", or(Boolean, null), defaults["null"]) | ||
.field("abstract", or(Boolean, null), defaults["null"]) | ||
.field("access", or("public", "private", "protected", null), defaults["null"]) | ||
.field("accessibility", or("public", "private", "protected", null), defaults["null"]) | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]) | ||
.field("optional", or(Boolean, null), defaults["null"]); | ||
}); | ||
def("ClassPrivateProperty") | ||
.bases("ClassProperty") | ||
.build("key", "value") | ||
.field("key", def("PrivateName")) | ||
.field("value", or(def("Expression"), null), defaults["null"]); | ||
def("PrivateName") | ||
.bases("Expression", "Pattern") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
var ObjectPatternProperty = or(def("Property"), def("PropertyPattern"), def("SpreadPropertyPattern"), def("SpreadProperty"), // Used by Esprima | ||
def("ObjectProperty"), // Babel 6 | ||
def("RestProperty") // Babel 6 | ||
); | ||
// Split into RestProperty and SpreadProperty | ||
def("ObjectPattern") | ||
.bases("Pattern") | ||
.build("properties") | ||
.field("properties", [ObjectPatternProperty]) | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]); | ||
def("SpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("RestProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ForAwaitStatement") | ||
.bases("Statement") | ||
.build("left", "right", "body") | ||
.field("left", or( | ||
def("VariableDeclaration"), | ||
def("Expression"))) | ||
.field("right", def("Expression")) | ||
.field("body", def("Statement")); | ||
// The callee node of a dynamic import(...) expression. | ||
def("Import") | ||
.bases("Expression") | ||
.build(); | ||
}; | ||
); | ||
// Split into RestProperty and SpreadProperty | ||
def("ObjectPattern") | ||
.bases("Pattern") | ||
.build("properties") | ||
.field("properties", [ObjectPatternProperty]) | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]); | ||
def("SpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("RestProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ForAwaitStatement") | ||
.bases("Statement") | ||
.build("left", "right", "body") | ||
.field("left", or(def("VariableDeclaration"), def("Expression"))) | ||
.field("right", def("Expression")) | ||
.field("body", def("Statement")); | ||
// The callee node of a dynamic import(...) expression. | ||
def("Import") | ||
.bases("Expression") | ||
.build(); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,4 +0,13 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./babel-core")); | ||
fork.use(require("./flow")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var babel_core_1 = __importDefault(require("./babel-core")); | ||
var flow_1 = __importDefault(require("./flow")); | ||
function default_1(fork) { | ||
fork.use(babel_core_1.default); | ||
fork.use(flow_1.default); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
183
def/core.js
@@ -1,37 +0,31 @@ | ||
module.exports = function (fork) { | ||
var types = fork.use(require("../lib/types")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
var types = fork.use(types_1.default); | ||
var Type = types.Type; | ||
var def = Type.def; | ||
var or = Type.or; | ||
var shared = fork.use(require("../lib/shared")); | ||
var shared = fork.use(shared_1.default); | ||
var defaults = shared.defaults; | ||
var geq = shared.geq; | ||
// Abstract supertype of all syntactic entities that are allowed to have a | ||
// .loc field. | ||
def("Printable") | ||
.field("loc", or( | ||
def("SourceLocation"), | ||
null | ||
), defaults["null"], true); | ||
.field("loc", or(def("SourceLocation"), null), defaults["null"], true); | ||
def("Node") | ||
.bases("Printable") | ||
.field("type", String) | ||
.field("comments", or( | ||
[def("Comment")], | ||
null | ||
), defaults["null"], true); | ||
.field("comments", or([def("Comment")], null), defaults["null"], true); | ||
def("SourceLocation") | ||
.build("start", "end", "source") | ||
.field("start", def("Position")) | ||
.field("end", def("Position")) | ||
.field("source", or(String, null), defaults["null"]); | ||
def("Position") | ||
.build("line", "column") | ||
.field("line", geq(1)) | ||
.field("column", geq(0)); | ||
def("File") | ||
@@ -42,3 +36,2 @@ .bases("Node") | ||
.field("name", or(String, null), defaults["null"]); | ||
def("Program") | ||
@@ -48,3 +41,2 @@ .bases("Node") | ||
.field("body", [def("Statement")]); | ||
def("Function") | ||
@@ -54,10 +46,9 @@ .bases("Node") | ||
.field("params", [def("Pattern")]) | ||
.field("body", def("BlockStatement")); | ||
.field("body", def("BlockStatement")) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]); | ||
def("Statement").bases("Node"); | ||
// The empty .build() here means that an EmptyStatement can be constructed | ||
// (i.e. it's not abstract) but that it needs no arguments. | ||
// The empty .build() here means that an EmptyStatement can be constructed | ||
// (i.e. it's not abstract) but that it needs no arguments. | ||
def("EmptyStatement").bases("Statement").build(); | ||
def("BlockStatement") | ||
@@ -67,3 +58,2 @@ .bases("Statement") | ||
.field("body", [def("Statement")]); | ||
// TODO Figure out how to silently coerce Expressions to | ||
@@ -75,3 +65,2 @@ // ExpressionStatements where a Statement was expected. | ||
.field("expression", def("Expression")); | ||
def("IfStatement") | ||
@@ -83,3 +72,2 @@ .bases("Statement") | ||
.field("alternate", or(def("Statement"), null), defaults["null"]); | ||
def("LabeledStatement") | ||
@@ -90,3 +78,2 @@ .bases("Statement") | ||
.field("body", def("Statement")); | ||
def("BreakStatement") | ||
@@ -96,3 +83,2 @@ .bases("Statement") | ||
.field("label", or(def("Identifier"), null), defaults["null"]); | ||
def("ContinueStatement") | ||
@@ -102,3 +88,2 @@ .bases("Statement") | ||
.field("label", or(def("Identifier"), null), defaults["null"]); | ||
def("WithStatement") | ||
@@ -109,3 +94,2 @@ .bases("Statement") | ||
.field("body", def("Statement")); | ||
def("SwitchStatement") | ||
@@ -117,3 +101,2 @@ .bases("Statement") | ||
.field("lexical", Boolean, defaults["false"]); | ||
def("ReturnStatement") | ||
@@ -123,3 +106,2 @@ .bases("Statement") | ||
.field("argument", or(def("Expression"), null)); | ||
def("ThrowStatement") | ||
@@ -129,3 +111,2 @@ .bases("Statement") | ||
.field("argument", def("Expression")); | ||
def("TryStatement") | ||
@@ -136,10 +117,9 @@ .bases("Statement") | ||
.field("handler", or(def("CatchClause"), null), function () { | ||
return this.handlers && this.handlers[0] || null; | ||
}) | ||
return this.handlers && this.handlers[0] || null; | ||
}) | ||
.field("handlers", [def("CatchClause")], function () { | ||
return this.handler ? [this.handler] : []; | ||
}, true) // Indicates this field is hidden from eachField iteration. | ||
return this.handler ? [this.handler] : []; | ||
}, true) // Indicates this field is hidden from eachField iteration. | ||
.field("guardedHandlers", [def("CatchClause")], defaults.emptyArray) | ||
.field("finalizer", or(def("BlockStatement"), null), defaults["null"]); | ||
def("CatchClause") | ||
@@ -152,3 +132,2 @@ .bases("Node") | ||
.field("body", def("BlockStatement")); | ||
def("WhileStatement") | ||
@@ -159,3 +138,2 @@ .bases("Statement") | ||
.field("body", def("Statement")); | ||
def("DoWhileStatement") | ||
@@ -166,27 +144,17 @@ .bases("Statement") | ||
.field("test", def("Expression")); | ||
def("ForStatement") | ||
.bases("Statement") | ||
.build("init", "test", "update", "body") | ||
.field("init", or( | ||
def("VariableDeclaration"), | ||
def("Expression"), | ||
null)) | ||
.field("init", or(def("VariableDeclaration"), def("Expression"), null)) | ||
.field("test", or(def("Expression"), null)) | ||
.field("update", or(def("Expression"), null)) | ||
.field("body", def("Statement")); | ||
def("ForInStatement") | ||
.bases("Statement") | ||
.build("left", "right", "body") | ||
.field("left", or( | ||
def("VariableDeclaration"), | ||
def("Expression"))) | ||
.field("left", or(def("VariableDeclaration"), def("Expression"))) | ||
.field("right", def("Expression")) | ||
.field("body", def("Statement")); | ||
def("DebuggerStatement").bases("Statement").build(); | ||
def("Declaration").bases("Statement"); | ||
def("FunctionDeclaration") | ||
@@ -196,7 +164,5 @@ .bases("Function", "Declaration") | ||
.field("id", def("Identifier")); | ||
def("FunctionExpression") | ||
.bases("Function", "Expression") | ||
.build("id", "params", "body"); | ||
def("VariableDeclaration") | ||
@@ -207,3 +173,2 @@ .bases("Declaration") | ||
.field("declarations", [def("VariableDeclarator")]); | ||
def("VariableDeclarator") | ||
@@ -213,9 +178,6 @@ .bases("Node") | ||
.field("id", def("Pattern")) | ||
.field("init", or(def("Expression"), null)); | ||
.field("init", or(def("Expression"), null), defaults["null"]); | ||
// TODO Are all Expressions really Patterns? | ||
def("Expression").bases("Node", "Pattern"); | ||
def("ThisExpression").bases("Expression").build(); | ||
def("ArrayExpression") | ||
@@ -225,3 +187,2 @@ .bases("Expression") | ||
.field("elements", [or(def("Expression"), null)]); | ||
def("ObjectExpression") | ||
@@ -231,3 +192,2 @@ .bases("Expression") | ||
.field("properties", [def("Property")]); | ||
// TODO Not in the Mozilla Parser API, but used by Esprima. | ||
@@ -240,3 +200,2 @@ def("Property") | ||
.field("value", def("Expression")); | ||
def("SequenceExpression") | ||
@@ -246,7 +205,3 @@ .bases("Expression") | ||
.field("expressions", [def("Expression")]); | ||
var UnaryOperator = or( | ||
"-", "+", "!", "~", | ||
"typeof", "void", "delete"); | ||
var UnaryOperator = or("-", "+", "!", "~", "typeof", "void", "delete"); | ||
def("UnaryExpression") | ||
@@ -260,12 +215,4 @@ .bases("Expression") | ||
.field("prefix", Boolean, defaults["true"]); | ||
var BinaryOperator = or( | ||
"==", "!=", "===", "!==", | ||
"<", "<=", ">", ">=", | ||
"<<", ">>", ">>>", | ||
"+", "-", "*", "/", "%", "**", | ||
"&", // TODO Missing from the Parser API. | ||
"|", "^", "in", | ||
"instanceof", ".."); | ||
var BinaryOperator = or("==", "!=", "===", "!==", "<", "<=", ">", ">=", "<<", ">>", ">>>", "+", "-", "*", "/", "%", "**", "&", // TODO Missing from the Parser API. | ||
"|", "^", "in", "instanceof", ".."); | ||
def("BinaryExpression") | ||
@@ -277,8 +224,3 @@ .bases("Expression") | ||
.field("right", def("Expression")); | ||
var AssignmentOperator = or( | ||
"=", "+=", "-=", "*=", "/=", "%=", | ||
"<<=", ">>=", ">>>=", | ||
"|=", "^=", "&="); | ||
var AssignmentOperator = or("=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&="); | ||
def("AssignmentExpression") | ||
@@ -290,5 +232,3 @@ .bases("Expression") | ||
.field("right", def("Expression")); | ||
var UpdateOperator = or("++", "--"); | ||
def("UpdateExpression") | ||
@@ -300,5 +240,3 @@ .bases("Expression") | ||
.field("prefix", Boolean); | ||
var LogicalOperator = or("||", "&&"); | ||
def("LogicalExpression") | ||
@@ -310,3 +248,2 @@ .bases("Expression") | ||
.field("right", def("Expression")); | ||
def("ConditionalExpression") | ||
@@ -318,3 +255,2 @@ .bases("Expression") | ||
.field("alternate", def("Expression")); | ||
def("NewExpression") | ||
@@ -328,3 +264,2 @@ .bases("Expression") | ||
.field("arguments", [def("Expression")]); | ||
def("CallExpression") | ||
@@ -336,3 +271,2 @@ .bases("Expression") | ||
.field("arguments", [def("Expression")]); | ||
def("MemberExpression") | ||
@@ -344,13 +278,11 @@ .bases("Expression") | ||
.field("computed", Boolean, function () { | ||
var type = this.property.type; | ||
if (type === 'Literal' || | ||
type === 'MemberExpression' || | ||
type === 'BinaryExpression') { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
var type = this.property.type; | ||
if (type === 'Literal' || | ||
type === 'MemberExpression' || | ||
type === 'BinaryExpression') { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
def("Pattern").bases("Node"); | ||
def("SwitchCase") | ||
@@ -361,3 +293,2 @@ .bases("Node") | ||
.field("consequent", [def("Statement")]); | ||
def("Identifier") | ||
@@ -369,3 +300,2 @@ // But aren't Expressions and Patterns already Nodes? TODO Report this. | ||
.field("optional", Boolean, defaults["false"]); | ||
def("Literal") | ||
@@ -377,21 +307,20 @@ // But aren't Expressions already Nodes? TODO Report this. | ||
.field("regex", or({ | ||
pattern: String, | ||
flags: String | ||
}, null), function () { | ||
if (this.value instanceof RegExp) { | ||
var flags = ""; | ||
if (this.value.ignoreCase) flags += "i"; | ||
if (this.value.multiline) flags += "m"; | ||
if (this.value.global) flags += "g"; | ||
return { | ||
pattern: this.value.source, | ||
flags: flags | ||
}; | ||
} | ||
return null; | ||
}); | ||
pattern: String, | ||
flags: String | ||
}, null), function () { | ||
if (this.value instanceof RegExp) { | ||
var flags = ""; | ||
if (this.value.ignoreCase) | ||
flags += "i"; | ||
if (this.value.multiline) | ||
flags += "m"; | ||
if (this.value.global) | ||
flags += "g"; | ||
return { | ||
pattern: this.value.source, | ||
flags: flags | ||
}; | ||
} | ||
return null; | ||
}); | ||
// Abstract (non-buildable) comment supertype. Not a Node. | ||
@@ -408,2 +337,4 @@ def("Comment") | ||
.field("trailing", Boolean, defaults["false"]); | ||
}; | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,16 +0,19 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./core")); | ||
var types = fork.use(require("../lib/types")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var core_1 = __importDefault(require("./core")); | ||
function default_1(fork) { | ||
fork.use(core_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
// Note that none of these types are buildable because the Mozilla Parser | ||
// API doesn't specify any builder functions, and nobody uses E4X anymore. | ||
def("XMLDefaultDeclaration") | ||
.bases("Declaration") | ||
.field("namespace", def("Expression")); | ||
def("XMLAnyName").bases("Expression"); | ||
def("XMLQualifiedIdentifier") | ||
@@ -21,3 +24,2 @@ .bases("Expression") | ||
.field("computed", Boolean); | ||
def("XMLFunctionQualifiedIdentifier") | ||
@@ -27,7 +29,5 @@ .bases("Expression") | ||
.field("computed", Boolean); | ||
def("XMLAttributeSelector") | ||
.bases("Expression") | ||
.field("attribute", def("Expression")); | ||
def("XMLFilterExpression") | ||
@@ -37,49 +37,36 @@ .bases("Expression") | ||
.field("right", def("Expression")); | ||
def("XMLElement") | ||
.bases("XML", "Expression") | ||
.field("contents", [def("XML")]); | ||
def("XMLList") | ||
.bases("XML", "Expression") | ||
.field("contents", [def("XML")]); | ||
def("XML").bases("Node"); | ||
def("XMLEscape") | ||
.bases("XML") | ||
.field("expression", def("Expression")); | ||
def("XMLText") | ||
.bases("XML") | ||
.field("text", String); | ||
def("XMLStartTag") | ||
.bases("XML") | ||
.field("contents", [def("XML")]); | ||
def("XMLEndTag") | ||
.bases("XML") | ||
.field("contents", [def("XML")]); | ||
def("XMLPointTag") | ||
.bases("XML") | ||
.field("contents", [def("XML")]); | ||
def("XMLName") | ||
.bases("XML") | ||
.field("contents", or(String, [def("XML")])); | ||
def("XMLAttribute") | ||
.bases("XML") | ||
.field("value", String); | ||
def("XMLCdata") | ||
.bases("XML") | ||
.field("contents", String); | ||
def("XMLComment") | ||
.bases("XML") | ||
.field("contents", String); | ||
def("XMLProcessingInstruction") | ||
@@ -89,2 +76,4 @@ .bases("XML") | ||
.field("contents", or(String, null)); | ||
}; | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,33 +0,35 @@ | ||
module.exports = function (fork) { | ||
fork.use(require('./core')); | ||
var types = fork.use(require("../lib/types")); | ||
var Type = types.Type; | ||
var def = types.Type.def; | ||
var or = Type.or; | ||
var shared = fork.use(require("../lib/shared")); | ||
var defaults = shared.defaults; | ||
// https://github.com/tc39/proposal-optional-chaining | ||
// `a?.b` as per https://github.com/estree/estree/issues/146 | ||
def("OptionalMemberExpression") | ||
.bases("MemberExpression") | ||
.build("object", "property", "computed", "optional") | ||
.field("optional", Boolean, defaults["true"]) | ||
// a?.b() | ||
def("OptionalCallExpression") | ||
.bases("CallExpression") | ||
.build("callee", "arguments", "optional") | ||
.field("optional", Boolean, defaults["true"]) | ||
// https://github.com/tc39/proposal-nullish-coalescing | ||
// `a ?? b` as per https://github.com/babel/babylon/pull/761/files | ||
var LogicalOperator = or("||", "&&", "??"); | ||
def("LogicalExpression") | ||
.field("operator", LogicalOperator) | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
var core_1 = __importDefault(require("./core")); | ||
function default_1(fork) { | ||
fork.use(core_1.default); | ||
var types = fork.use(types_1.default); | ||
var Type = types.Type; | ||
var def = types.Type.def; | ||
var or = Type.or; | ||
var shared = fork.use(shared_1.default); | ||
var defaults = shared.defaults; | ||
// https://github.com/tc39/proposal-optional-chaining | ||
// `a?.b` as per https://github.com/estree/estree/issues/146 | ||
def("OptionalMemberExpression") | ||
.bases("MemberExpression") | ||
.build("object", "property", "computed", "optional") | ||
.field("optional", Boolean, defaults["true"]); | ||
// a?.b() | ||
def("OptionalCallExpression") | ||
.bases("CallExpression") | ||
.build("callee", "arguments", "optional") | ||
.field("optional", Boolean, defaults["true"]); | ||
// https://github.com/tc39/proposal-nullish-coalescing | ||
// `a ?? b` as per https://github.com/babel/babylon/pull/761/files | ||
var LogicalOperator = or("||", "&&", "??"); | ||
def("LogicalExpression") | ||
.field("operator", LogicalOperator); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
466
def/es6.js
@@ -1,258 +0,212 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./core")); | ||
var types = fork.use(require("../lib/types")); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
def("Function") | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("expression", Boolean, defaults["false"]) | ||
.field("defaults", [or(def("Expression"), null)], defaults.emptyArray) | ||
// TODO This could be represented as a RestElement in .params. | ||
.field("rest", or(def("Identifier"), null), defaults["null"]); | ||
// The ESTree way of representing a ...rest parameter. | ||
def("RestElement") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")) | ||
.field("typeAnnotation", // for Babylon. Flow parser puts it on the identifier | ||
or(def("TypeAnnotation"), def("TSTypeAnnotation"), null), defaults["null"]); | ||
def("SpreadElementPattern") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")); | ||
def("FunctionDeclaration") | ||
.build("id", "params", "body", "generator", "expression"); | ||
def("FunctionExpression") | ||
.build("id", "params", "body", "generator", "expression"); | ||
// The Parser API calls this ArrowExpression, but Esprima and all other | ||
// actual parsers use ArrowFunctionExpression. | ||
def("ArrowFunctionExpression") | ||
.bases("Function", "Expression") | ||
.build("params", "body", "expression") | ||
// The forced null value here is compatible with the overridden | ||
// definition of the "id" field in the Function interface. | ||
.field("id", null, defaults["null"]) | ||
// Arrow function bodies are allowed to be expressions. | ||
.field("body", or(def("BlockStatement"), def("Expression"))) | ||
// The current spec forbids arrow generators, so I have taken the | ||
// liberty of enforcing that. TODO Report this. | ||
.field("generator", false, defaults["false"]); | ||
def("ForOfStatement") | ||
.bases("Statement") | ||
.build("left", "right", "body") | ||
.field("left", or( | ||
def("VariableDeclaration"), | ||
def("Pattern"))) | ||
.field("right", def("Expression")) | ||
.field("body", def("Statement")); | ||
def("YieldExpression") | ||
.bases("Expression") | ||
.build("argument", "delegate") | ||
.field("argument", or(def("Expression"), null)) | ||
.field("delegate", Boolean, defaults["false"]); | ||
def("GeneratorExpression") | ||
.bases("Expression") | ||
.build("body", "blocks", "filter") | ||
.field("body", def("Expression")) | ||
.field("blocks", [def("ComprehensionBlock")]) | ||
.field("filter", or(def("Expression"), null)); | ||
def("ComprehensionExpression") | ||
.bases("Expression") | ||
.build("body", "blocks", "filter") | ||
.field("body", def("Expression")) | ||
.field("blocks", [def("ComprehensionBlock")]) | ||
.field("filter", or(def("Expression"), null)); | ||
def("ComprehensionBlock") | ||
.bases("Node") | ||
.build("left", "right", "each") | ||
.field("left", def("Pattern")) | ||
.field("right", def("Expression")) | ||
.field("each", Boolean); | ||
def("Property") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("value", or(def("Expression"), def("Pattern"))) | ||
.field("method", Boolean, defaults["false"]) | ||
.field("shorthand", Boolean, defaults["false"]) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ObjectProperty") | ||
.field("shorthand", Boolean, defaults["false"]); | ||
def("PropertyPattern") | ||
.bases("Pattern") | ||
.build("key", "pattern") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("pattern", def("Pattern")) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ObjectPattern") | ||
.bases("Pattern") | ||
.build("properties") | ||
.field("properties", [or(def("PropertyPattern"), def("Property"))]); | ||
def("ArrayPattern") | ||
.bases("Pattern") | ||
.build("elements") | ||
.field("elements", [or(def("Pattern"), null)]); | ||
def("MethodDefinition") | ||
.bases("Declaration") | ||
.build("kind", "key", "value", "static") | ||
.field("kind", or("constructor", "method", "get", "set")) | ||
.field("key", def("Expression")) | ||
.field("value", def("Function")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("static", Boolean, defaults["false"]); | ||
def("SpreadElement") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ArrayExpression") | ||
.field("elements", [or( | ||
def("Expression"), | ||
def("SpreadElement"), | ||
def("RestElement"), | ||
null | ||
)]); | ||
def("NewExpression") | ||
.field("arguments", [or(def("Expression"), def("SpreadElement"))]); | ||
def("CallExpression") | ||
.field("arguments", [or(def("Expression"), def("SpreadElement"))]); | ||
// Note: this node type is *not* an AssignmentExpression with a Pattern on | ||
// the left-hand side! The existing AssignmentExpression type already | ||
// supports destructuring assignments. AssignmentPattern nodes may appear | ||
// wherever a Pattern is allowed, and the right-hand side represents a | ||
// default value to be destructured against the left-hand side, if no | ||
// value is otherwise provided. For example: default parameter values. | ||
def("AssignmentPattern") | ||
.bases("Pattern") | ||
.build("left", "right") | ||
.field("left", def("Pattern")) | ||
.field("right", def("Expression")); | ||
var ClassBodyElement = or( | ||
def("MethodDefinition"), | ||
def("VariableDeclarator"), | ||
def("ClassPropertyDefinition"), | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var core_1 = __importDefault(require("./core")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(core_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
def("Function") | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("expression", Boolean, defaults["false"]) | ||
.field("defaults", [or(def("Expression"), null)], defaults.emptyArray) | ||
// TODO This could be represented as a RestElement in .params. | ||
.field("rest", or(def("Identifier"), null), defaults["null"]); | ||
// The ESTree way of representing a ...rest parameter. | ||
def("RestElement") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")) | ||
.field("typeAnnotation", // for Babylon. Flow parser puts it on the identifier | ||
or(def("TypeAnnotation"), def("TSTypeAnnotation"), null), defaults["null"]); | ||
def("SpreadElementPattern") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")); | ||
def("FunctionDeclaration") | ||
.build("id", "params", "body", "generator", "expression"); | ||
def("FunctionExpression") | ||
.build("id", "params", "body", "generator", "expression"); | ||
// The Parser API calls this ArrowExpression, but Esprima and all other | ||
// actual parsers use ArrowFunctionExpression. | ||
def("ArrowFunctionExpression") | ||
.bases("Function", "Expression") | ||
.build("params", "body", "expression") | ||
// The forced null value here is compatible with the overridden | ||
// definition of the "id" field in the Function interface. | ||
.field("id", null, defaults["null"]) | ||
// Arrow function bodies are allowed to be expressions. | ||
.field("body", or(def("BlockStatement"), def("Expression"))) | ||
// The current spec forbids arrow generators, so I have taken the | ||
// liberty of enforcing that. TODO Report this. | ||
.field("generator", false, defaults["false"]); | ||
def("ForOfStatement") | ||
.bases("Statement") | ||
.build("left", "right", "body") | ||
.field("left", or(def("VariableDeclaration"), def("Pattern"))) | ||
.field("right", def("Expression")) | ||
.field("body", def("Statement")); | ||
def("YieldExpression") | ||
.bases("Expression") | ||
.build("argument", "delegate") | ||
.field("argument", or(def("Expression"), null)) | ||
.field("delegate", Boolean, defaults["false"]); | ||
def("GeneratorExpression") | ||
.bases("Expression") | ||
.build("body", "blocks", "filter") | ||
.field("body", def("Expression")) | ||
.field("blocks", [def("ComprehensionBlock")]) | ||
.field("filter", or(def("Expression"), null)); | ||
def("ComprehensionExpression") | ||
.bases("Expression") | ||
.build("body", "blocks", "filter") | ||
.field("body", def("Expression")) | ||
.field("blocks", [def("ComprehensionBlock")]) | ||
.field("filter", or(def("Expression"), null)); | ||
def("ComprehensionBlock") | ||
.bases("Node") | ||
.build("left", "right", "each") | ||
.field("left", def("Pattern")) | ||
.field("right", def("Expression")) | ||
.field("each", Boolean); | ||
def("Property") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("value", or(def("Expression"), def("Pattern"))) | ||
.field("method", Boolean, defaults["false"]) | ||
.field("shorthand", Boolean, defaults["false"]) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ObjectProperty") | ||
.field("shorthand", Boolean, defaults["false"]); | ||
def("PropertyPattern") | ||
.bases("Pattern") | ||
.build("key", "pattern") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("pattern", def("Pattern")) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ObjectPattern") | ||
.bases("Pattern") | ||
.build("properties") | ||
.field("properties", [or(def("PropertyPattern"), def("Property"))]); | ||
def("ArrayPattern") | ||
.bases("Pattern") | ||
.build("elements") | ||
.field("elements", [or(def("Pattern"), null)]); | ||
def("MethodDefinition") | ||
.bases("Declaration") | ||
.build("kind", "key", "value", "static") | ||
.field("kind", or("constructor", "method", "get", "set")) | ||
.field("key", def("Expression")) | ||
.field("value", def("Function")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("static", Boolean, defaults["false"]); | ||
def("SpreadElement") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ArrayExpression") | ||
.field("elements", [or(def("Expression"), def("SpreadElement"), def("RestElement"), null)]); | ||
def("NewExpression") | ||
.field("arguments", [or(def("Expression"), def("SpreadElement"))]); | ||
def("CallExpression") | ||
.field("arguments", [or(def("Expression"), def("SpreadElement"))]); | ||
// Note: this node type is *not* an AssignmentExpression with a Pattern on | ||
// the left-hand side! The existing AssignmentExpression type already | ||
// supports destructuring assignments. AssignmentPattern nodes may appear | ||
// wherever a Pattern is allowed, and the right-hand side represents a | ||
// default value to be destructured against the left-hand side, if no | ||
// value is otherwise provided. For example: default parameter values. | ||
def("AssignmentPattern") | ||
.bases("Pattern") | ||
.build("left", "right") | ||
.field("left", def("Pattern")) | ||
.field("right", def("Expression")); | ||
var ClassBodyElement = or(def("MethodDefinition"), def("VariableDeclarator"), def("ClassPropertyDefinition"), def("ClassProperty")); | ||
def("ClassProperty") | ||
); | ||
def("ClassProperty") | ||
.bases("Declaration") | ||
.build("key") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ClassPropertyDefinition") // static property | ||
.bases("Declaration") | ||
.build("definition") | ||
// Yes, Virginia, circular definitions are permitted. | ||
.field("definition", ClassBodyElement); | ||
def("ClassBody") | ||
.bases("Declaration") | ||
.build("body") | ||
.field("body", [ClassBodyElement]); | ||
def("ClassDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body", "superClass") | ||
.field("id", or(def("Identifier"), null)) | ||
.field("body", def("ClassBody")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]); | ||
def("ClassExpression") | ||
.bases("Expression") | ||
.build("id", "body", "superClass") | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("body", def("ClassBody")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]); | ||
// Specifier and ModuleSpecifier are abstract non-standard types | ||
// introduced for definitional convenience. | ||
def("Specifier").bases("Node"); | ||
// This supertype is shared/abused by both def/babel.js and | ||
// def/esprima.js. In the future, it will be possible to load only one set | ||
// of definitions appropriate for a given parser, but until then we must | ||
// rely on default functions to reconcile the conflicting AST formats. | ||
def("ModuleSpecifier") | ||
.bases("Specifier") | ||
// This local field is used by Babel/Acorn. It should not technically | ||
// be optional in the Babel/Acorn AST format, but it must be optional | ||
// in the Esprima AST format. | ||
.field("local", or(def("Identifier"), null), defaults["null"]) | ||
// The id and name fields are used by Esprima. The id field should not | ||
// technically be optional in the Esprima AST format, but it must be | ||
// optional in the Babel/Acorn AST format. | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("name", or(def("Identifier"), null), defaults["null"]); | ||
// Like ModuleSpecifier, except type:"ImportSpecifier" and buildable. | ||
// import {<id [as name]>} from ...; | ||
def("ImportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id", "name"); | ||
// import <* as id> from ...; | ||
def("ImportNamespaceSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id"); | ||
// import <id> from ...; | ||
def("ImportDefaultSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id"); | ||
def("ImportDeclaration") | ||
.bases("Declaration") | ||
.build("specifiers", "source", "importKind") | ||
.field("specifiers", [or( | ||
def("ImportSpecifier"), | ||
def("ImportNamespaceSpecifier"), | ||
def("ImportDefaultSpecifier") | ||
)], defaults.emptyArray) | ||
.field("source", def("Literal")) | ||
.field("importKind", or( | ||
"value", | ||
"type" | ||
), function() { | ||
return "value"; | ||
.bases("Declaration") | ||
.build("key") | ||
.field("key", or(def("Literal"), def("Identifier"), def("Expression"))) | ||
.field("computed", Boolean, defaults["false"]); | ||
def("ClassPropertyDefinition") // static property | ||
.bases("Declaration") | ||
.build("definition") | ||
// Yes, Virginia, circular definitions are permitted. | ||
.field("definition", ClassBodyElement); | ||
def("ClassBody") | ||
.bases("Declaration") | ||
.build("body") | ||
.field("body", [ClassBodyElement]); | ||
def("ClassDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body", "superClass") | ||
.field("id", or(def("Identifier"), null)) | ||
.field("body", def("ClassBody")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]); | ||
def("ClassExpression") | ||
.bases("Expression") | ||
.build("id", "body", "superClass") | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("body", def("ClassBody")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]); | ||
// Specifier and ModuleSpecifier are abstract non-standard types | ||
// introduced for definitional convenience. | ||
def("Specifier").bases("Node"); | ||
// This supertype is shared/abused by both def/babel.js and | ||
// def/esprima.js. In the future, it will be possible to load only one set | ||
// of definitions appropriate for a given parser, but until then we must | ||
// rely on default functions to reconcile the conflicting AST formats. | ||
def("ModuleSpecifier") | ||
.bases("Specifier") | ||
// This local field is used by Babel/Acorn. It should not technically | ||
// be optional in the Babel/Acorn AST format, but it must be optional | ||
// in the Esprima AST format. | ||
.field("local", or(def("Identifier"), null), defaults["null"]) | ||
// The id and name fields are used by Esprima. The id field should not | ||
// technically be optional in the Esprima AST format, but it must be | ||
// optional in the Babel/Acorn AST format. | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("name", or(def("Identifier"), null), defaults["null"]); | ||
// Like ModuleSpecifier, except type:"ImportSpecifier" and buildable. | ||
// import {<id [as name]>} from ...; | ||
def("ImportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id", "name"); | ||
// import <* as id> from ...; | ||
def("ImportNamespaceSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id"); | ||
// import <id> from ...; | ||
def("ImportDefaultSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id"); | ||
def("ImportDeclaration") | ||
.bases("Declaration") | ||
.build("specifiers", "source", "importKind") | ||
.field("specifiers", [or(def("ImportSpecifier"), def("ImportNamespaceSpecifier"), def("ImportDefaultSpecifier"))], defaults.emptyArray) | ||
.field("source", def("Literal")) | ||
.field("importKind", or("value", "type"), function () { | ||
return "value"; | ||
}); | ||
def("TaggedTemplateExpression") | ||
.bases("Expression") | ||
.build("tag", "quasi") | ||
.field("tag", def("Expression")) | ||
.field("quasi", def("TemplateLiteral")); | ||
def("TemplateLiteral") | ||
.bases("Expression") | ||
.build("quasis", "expressions") | ||
.field("quasis", [def("TemplateElement")]) | ||
.field("expressions", [def("Expression")]); | ||
def("TemplateElement") | ||
.bases("Node") | ||
.build("value", "tail") | ||
.field("value", {"cooked": String, "raw": String}) | ||
.field("tail", Boolean); | ||
}; | ||
def("TaggedTemplateExpression") | ||
.bases("Expression") | ||
.build("tag", "quasi") | ||
.field("tag", def("Expression")) | ||
.field("quasi", def("TemplateLiteral")); | ||
def("TemplateLiteral") | ||
.bases("Expression") | ||
.build("quasis", "expressions") | ||
.field("quasis", [def("TemplateElement")]) | ||
.field("expressions", [def("Expression")]); | ||
def("TemplateElement") | ||
.bases("Node") | ||
.build("value", "tail") | ||
.field("value", { "cooked": String, "raw": String }) | ||
.field("tail", Boolean); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,42 +0,36 @@ | ||
module.exports = function (fork) { | ||
fork.use(require('./es6')); | ||
var types = fork.use(require("../lib/types")); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var builtin = types.builtInTypes; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
def("Function") | ||
.field("async", Boolean, defaults["false"]); | ||
def("SpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ObjectExpression") | ||
.field("properties", [or( | ||
def("Property"), | ||
def("SpreadProperty"), | ||
def("SpreadElement") | ||
)]); | ||
def("SpreadPropertyPattern") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")); | ||
def("ObjectPattern") | ||
.field("properties", [or( | ||
def("Property"), | ||
def("PropertyPattern"), | ||
def("SpreadPropertyPattern") | ||
)]); | ||
def("AwaitExpression") | ||
.bases("Expression") | ||
.build("argument", "all") | ||
.field("argument", or(def("Expression"), null)) | ||
.field("all", Boolean, defaults["false"]); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var es6_1 = __importDefault(require("./es6")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(es6_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
def("Function") | ||
.field("async", Boolean, defaults["false"]); | ||
def("SpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
def("ObjectExpression") | ||
.field("properties", [or(def("Property"), def("SpreadProperty"), def("SpreadElement"))]); | ||
def("SpreadPropertyPattern") | ||
.bases("Pattern") | ||
.build("argument") | ||
.field("argument", def("Pattern")); | ||
def("ObjectPattern") | ||
.field("properties", [or(def("Property"), def("PropertyPattern"), def("SpreadPropertyPattern"))]); | ||
def("AwaitExpression") | ||
.bases("Expression") | ||
.build("argument", "all") | ||
.field("argument", or(def("Expression"), null)) | ||
.field("all", Boolean, defaults["false"]); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,72 +0,51 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./es7")); | ||
var types = fork.use(require("../lib/types")); | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
def("VariableDeclaration") | ||
.field("declarations", [or( | ||
def("VariableDeclarator"), | ||
def("Identifier") // Esprima deviation. | ||
)]); | ||
def("Property") | ||
.field("value", or( | ||
def("Expression"), | ||
def("Pattern") // Esprima deviation. | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var es7_1 = __importDefault(require("./es7")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(es7_1.default); | ||
var types = fork.use(types_1.default); | ||
var defaults = fork.use(shared_1.default).defaults; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
def("VariableDeclaration") | ||
.field("declarations", [or(def("VariableDeclarator"), def("Identifier") // Esprima deviation. | ||
)]); | ||
def("Property") | ||
.field("value", or(def("Expression"), def("Pattern") // Esprima deviation. | ||
)); | ||
def("ArrayPattern") | ||
.field("elements", [or( | ||
def("Pattern"), | ||
def("SpreadElement"), | ||
null | ||
)]); | ||
def("ObjectPattern") | ||
.field("properties", [or( | ||
def("Property"), | ||
def("PropertyPattern"), | ||
def("SpreadPropertyPattern"), | ||
def("SpreadProperty") // Used by Esprima. | ||
)]); | ||
// Like ModuleSpecifier, except type:"ExportSpecifier" and buildable. | ||
// export {<id [as name]>} [from ...]; | ||
def("ExportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id", "name"); | ||
// export <*> from ...; | ||
def("ExportBatchSpecifier") | ||
.bases("Specifier") | ||
.build(); | ||
def("ExportDeclaration") | ||
.bases("Declaration") | ||
.build("default", "declaration", "specifiers", "source") | ||
.field("default", Boolean) | ||
.field("declaration", or( | ||
def("Declaration"), | ||
def("Expression"), // Implies default. | ||
null | ||
)) | ||
.field("specifiers", [or( | ||
def("ExportSpecifier"), | ||
def("ExportBatchSpecifier") | ||
)], defaults.emptyArray) | ||
.field("source", or( | ||
def("Literal"), | ||
null | ||
), defaults["null"]); | ||
def("Block") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("Line") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
}; | ||
def("ArrayPattern") | ||
.field("elements", [or(def("Pattern"), def("SpreadElement"), null)]); | ||
def("ObjectPattern") | ||
.field("properties", [or(def("Property"), def("PropertyPattern"), def("SpreadPropertyPattern"), def("SpreadProperty") // Used by Esprima. | ||
)]); | ||
// Like ModuleSpecifier, except type:"ExportSpecifier" and buildable. | ||
// export {<id [as name]>} [from ...]; | ||
def("ExportSpecifier") | ||
.bases("ModuleSpecifier") | ||
.build("id", "name"); | ||
// export <*> from ...; | ||
def("ExportBatchSpecifier") | ||
.bases("Specifier") | ||
.build(); | ||
def("ExportDeclaration") | ||
.bases("Declaration") | ||
.build("default", "declaration", "specifiers", "source") | ||
.field("default", Boolean) | ||
.field("declaration", or(def("Declaration"), def("Expression"), // Implies default. | ||
null)) | ||
.field("specifiers", [or(def("ExportSpecifier"), def("ExportBatchSpecifier"))], defaults.emptyArray) | ||
.field("source", or(def("Literal"), null), defaults["null"]); | ||
def("Block") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
def("Line") | ||
.bases("Comment") | ||
.build("value", /*optional:*/ "leading", "trailing"); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
645
def/flow.js
@@ -1,358 +0,289 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./es7")); | ||
fork.use(require("./type-annotations")); | ||
var types = fork.use(require("../lib/types")); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
// Base types | ||
def("Flow").bases("Node"); | ||
def("FlowType").bases("Flow"); | ||
// Type annotations | ||
def("AnyTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("EmptyTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("MixedTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("VoidTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NumberTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NumberLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Number) | ||
.field("raw", String); | ||
// Babylon 6 differs in AST from Flow | ||
// same as NumberLiteralTypeAnnotation | ||
def("NumericLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Number) | ||
.field("raw", String); | ||
def("StringTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("StringLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", String) | ||
.field("raw", String); | ||
def("BooleanTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("BooleanLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Boolean) | ||
.field("raw", String); | ||
def("TypeAnnotation") | ||
.bases("Node") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("FlowType")); | ||
def("NullableTypeAnnotation") | ||
.bases("FlowType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("FlowType")); | ||
def("NullLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NullTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ThisTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ExistsTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ExistentialTypeParam") | ||
.bases("FlowType") | ||
.build(); | ||
def("FunctionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("params", "returnType", "rest", "typeParameters") | ||
.field("params", [def("FunctionTypeParam")]) | ||
.field("returnType", def("FlowType")) | ||
.field("rest", or(def("FunctionTypeParam"), null)) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)); | ||
def("FunctionTypeParam") | ||
.bases("Node") | ||
.build("name", "typeAnnotation", "optional") | ||
.field("name", def("Identifier")) | ||
.field("typeAnnotation", def("FlowType")) | ||
.field("optional", Boolean); | ||
def("ArrayTypeAnnotation") | ||
.bases("FlowType") | ||
.build("elementType") | ||
.field("elementType", def("FlowType")); | ||
def("ObjectTypeAnnotation") | ||
.bases("FlowType") | ||
.build("properties", "indexers", "callProperties") | ||
.field("properties", [ | ||
or(def("ObjectTypeProperty"), | ||
def("ObjectTypeSpreadProperty")) | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var es7_1 = __importDefault(require("./es7")); | ||
var type_annotations_1 = __importDefault(require("./type-annotations")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(es7_1.default); | ||
fork.use(type_annotations_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
// Base types | ||
def("Flow").bases("Node"); | ||
def("FlowType").bases("Flow"); | ||
// Type annotations | ||
def("AnyTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("EmptyTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("MixedTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("VoidTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NumberTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NumberLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Number) | ||
.field("raw", String); | ||
// Babylon 6 differs in AST from Flow | ||
// same as NumberLiteralTypeAnnotation | ||
def("NumericLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Number) | ||
.field("raw", String); | ||
def("StringTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("StringLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", String) | ||
.field("raw", String); | ||
def("BooleanTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("BooleanLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build("value", "raw") | ||
.field("value", Boolean) | ||
.field("raw", String); | ||
def("TypeAnnotation") | ||
.bases("Node") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("FlowType")); | ||
def("NullableTypeAnnotation") | ||
.bases("FlowType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("FlowType")); | ||
def("NullLiteralTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("NullTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ThisTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ExistsTypeAnnotation") | ||
.bases("FlowType") | ||
.build(); | ||
def("ExistentialTypeParam") | ||
.bases("FlowType") | ||
.build(); | ||
def("FunctionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("params", "returnType", "rest", "typeParameters") | ||
.field("params", [def("FunctionTypeParam")]) | ||
.field("returnType", def("FlowType")) | ||
.field("rest", or(def("FunctionTypeParam"), null)) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)); | ||
def("FunctionTypeParam") | ||
.bases("Node") | ||
.build("name", "typeAnnotation", "optional") | ||
.field("name", def("Identifier")) | ||
.field("typeAnnotation", def("FlowType")) | ||
.field("optional", Boolean); | ||
def("ArrayTypeAnnotation") | ||
.bases("FlowType") | ||
.build("elementType") | ||
.field("elementType", def("FlowType")); | ||
def("ObjectTypeAnnotation") | ||
.bases("FlowType") | ||
.build("properties", "indexers", "callProperties") | ||
.field("properties", [ | ||
or(def("ObjectTypeProperty"), def("ObjectTypeSpreadProperty")) | ||
]) | ||
.field("indexers", [def("ObjectTypeIndexer")], defaults.emptyArray) | ||
.field("callProperties", | ||
[def("ObjectTypeCallProperty")], | ||
defaults.emptyArray) | ||
.field("exact", Boolean, defaults["false"]); | ||
def("Variance") | ||
.bases("Node") | ||
.build("kind") | ||
.field("kind", or("plus", "minus")); | ||
var LegacyVariance = or( | ||
def("Variance"), | ||
"plus", | ||
"minus", | ||
null | ||
); | ||
def("ObjectTypeProperty") | ||
.bases("Node") | ||
.build("key", "value", "optional") | ||
.field("key", or(def("Literal"), def("Identifier"))) | ||
.field("value", def("FlowType")) | ||
.field("optional", Boolean) | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ObjectTypeIndexer") | ||
.bases("Node") | ||
.build("id", "key", "value") | ||
.field("id", def("Identifier")) | ||
.field("key", def("FlowType")) | ||
.field("value", def("FlowType")) | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ObjectTypeCallProperty") | ||
.bases("Node") | ||
.build("value") | ||
.field("value", def("FunctionTypeAnnotation")) | ||
.field("static", Boolean, defaults["false"]); | ||
def("QualifiedTypeIdentifier") | ||
.bases("Node") | ||
.build("qualification", "id") | ||
.field("qualification", | ||
or(def("Identifier"), | ||
def("QualifiedTypeIdentifier"))) | ||
.field("id", def("Identifier")); | ||
def("GenericTypeAnnotation") | ||
.bases("FlowType") | ||
.build("id", "typeParameters") | ||
.field("id", or(def("Identifier"), def("QualifiedTypeIdentifier"))) | ||
.field("typeParameters", or(def("TypeParameterInstantiation"), null)); | ||
def("MemberTypeAnnotation") | ||
.bases("FlowType") | ||
.build("object", "property") | ||
.field("object", def("Identifier")) | ||
.field("property", | ||
or(def("MemberTypeAnnotation"), | ||
def("GenericTypeAnnotation"))); | ||
def("UnionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("IntersectionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("TypeofTypeAnnotation") | ||
.bases("FlowType") | ||
.build("argument") | ||
.field("argument", def("FlowType")); | ||
def("ObjectTypeSpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("FlowType")); | ||
def("TypeParameterDeclaration") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("TypeParameter")]); | ||
def("TypeParameterInstantiation") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("FlowType")]); | ||
def("TypeParameter") | ||
.bases("FlowType") | ||
.build("name", "variance", "bound") | ||
.field("name", String) | ||
.field("variance", LegacyVariance, defaults["null"]) | ||
.field("bound", | ||
or(def("TypeAnnotation"), null), | ||
defaults["null"]); | ||
def("ClassProperty") | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ClassImplements") | ||
.bases("Node") | ||
.build("id") | ||
.field("id", def("Identifier")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]) | ||
.field("typeParameters", | ||
or(def("TypeParameterInstantiation"), null), | ||
defaults["null"]); | ||
def("InterfaceDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body", "extends") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", | ||
or(def("TypeParameterDeclaration"), null), | ||
defaults["null"]) | ||
.field("body", def("ObjectTypeAnnotation")) | ||
.field("extends", [def("InterfaceExtends")]); | ||
def("DeclareInterface") | ||
.bases("InterfaceDeclaration") | ||
.build("id", "body", "extends"); | ||
def("InterfaceExtends") | ||
.bases("Node") | ||
.build("id") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterInstantiation"), null)); | ||
def("TypeAlias") | ||
.bases("Declaration") | ||
.build("id", "typeParameters", "right") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)) | ||
.field("right", def("FlowType")); | ||
def("OpaqueType") | ||
.bases("Declaration") | ||
.build("id", "typeParameters", "impltype", "supertype") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)) | ||
.field("implType", def("FlowType")) | ||
.field("superType", def("FlowType")); | ||
def("DeclareTypeAlias") | ||
.bases("TypeAlias") | ||
.build("id", "typeParameters", "right"); | ||
def("DeclareOpaqueType") | ||
.bases("TypeAlias") | ||
.build("id", "typeParameters", "supertype"); | ||
def("TypeCastExpression") | ||
.bases("Expression") | ||
.build("expression", "typeAnnotation") | ||
.field("expression", def("Expression")) | ||
.field("typeAnnotation", def("TypeAnnotation")); | ||
def("TupleTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("DeclareVariable") | ||
.bases("Statement") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("DeclareFunction") | ||
.bases("Statement") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("DeclareClass") | ||
.bases("InterfaceDeclaration") | ||
.build("id"); | ||
def("DeclareModule") | ||
.bases("Statement") | ||
.build("id", "body") | ||
.field("id", or(def("Identifier"), def("Literal"))) | ||
.field("body", def("BlockStatement")); | ||
def("DeclareModuleExports") | ||
.bases("Statement") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TypeAnnotation")); | ||
def("DeclareExportDeclaration") | ||
.bases("Declaration") | ||
.build("default", "declaration", "specifiers", "source") | ||
.field("default", Boolean) | ||
.field("declaration", or( | ||
def("DeclareVariable"), | ||
def("DeclareFunction"), | ||
def("DeclareClass"), | ||
def("FlowType"), // Implies default. | ||
null | ||
)) | ||
.field("specifiers", [or( | ||
def("ExportSpecifier"), | ||
def("ExportBatchSpecifier") | ||
)], defaults.emptyArray) | ||
.field("source", or( | ||
def("Literal"), | ||
null | ||
), defaults["null"]); | ||
def("DeclareExportAllDeclaration") | ||
.bases("Declaration") | ||
.build("source") | ||
.field("source", or( | ||
def("Literal"), | ||
null | ||
), defaults["null"]); | ||
def("FlowPredicate").bases("Flow"); | ||
def("InferredPredicate") | ||
.bases("FlowPredicate") | ||
.build(); | ||
def("DeclaredPredicate") | ||
.bases("FlowPredicate") | ||
.build("value") | ||
.field("value", def("Expression")); | ||
}; | ||
.field("indexers", [def("ObjectTypeIndexer")], defaults.emptyArray) | ||
.field("callProperties", [def("ObjectTypeCallProperty")], defaults.emptyArray) | ||
.field("exact", Boolean, defaults["false"]) | ||
.field("internalSlots", [def("ObjectTypeInternalSlot")], defaults.emptyArray); | ||
def("Variance") | ||
.bases("Node") | ||
.build("kind") | ||
.field("kind", or("plus", "minus")); | ||
var LegacyVariance = or(def("Variance"), "plus", "minus", null); | ||
def("ObjectTypeProperty") | ||
.bases("Node") | ||
.build("key", "value", "optional") | ||
.field("key", or(def("Literal"), def("Identifier"))) | ||
.field("value", def("FlowType")) | ||
.field("optional", Boolean) | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ObjectTypeIndexer") | ||
.bases("Node") | ||
.build("id", "key", "value") | ||
.field("id", def("Identifier")) | ||
.field("key", def("FlowType")) | ||
.field("value", def("FlowType")) | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ObjectTypeCallProperty") | ||
.bases("Node") | ||
.build("value") | ||
.field("value", def("FunctionTypeAnnotation")) | ||
.field("static", Boolean, defaults["false"]); | ||
def("QualifiedTypeIdentifier") | ||
.bases("Node") | ||
.build("qualification", "id") | ||
.field("qualification", or(def("Identifier"), def("QualifiedTypeIdentifier"))) | ||
.field("id", def("Identifier")); | ||
def("GenericTypeAnnotation") | ||
.bases("FlowType") | ||
.build("id", "typeParameters") | ||
.field("id", or(def("Identifier"), def("QualifiedTypeIdentifier"))) | ||
.field("typeParameters", or(def("TypeParameterInstantiation"), null)); | ||
def("MemberTypeAnnotation") | ||
.bases("FlowType") | ||
.build("object", "property") | ||
.field("object", def("Identifier")) | ||
.field("property", or(def("MemberTypeAnnotation"), def("GenericTypeAnnotation"))); | ||
def("UnionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("IntersectionTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("TypeofTypeAnnotation") | ||
.bases("FlowType") | ||
.build("argument") | ||
.field("argument", def("FlowType")); | ||
def("ObjectTypeSpreadProperty") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("FlowType")); | ||
def("ObjectTypeInternalSlot") | ||
.bases("Node") | ||
.build("id", "value", "optional", "static", "method") | ||
.field("id", def("Identifier")) | ||
.field("value", def("FlowType")) | ||
.field("optional", Boolean) | ||
.field("static", Boolean) | ||
.field("method", Boolean); | ||
def("TypeParameterDeclaration") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("TypeParameter")]); | ||
def("TypeParameterInstantiation") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("FlowType")]); | ||
def("TypeParameter") | ||
.bases("FlowType") | ||
.build("name", "variance", "bound") | ||
.field("name", String) | ||
.field("variance", LegacyVariance, defaults["null"]) | ||
.field("bound", or(def("TypeAnnotation"), null), defaults["null"]); | ||
def("ClassProperty") | ||
.field("variance", LegacyVariance, defaults["null"]); | ||
def("ClassImplements") | ||
.bases("Node") | ||
.build("id") | ||
.field("id", def("Identifier")) | ||
.field("superClass", or(def("Expression"), null), defaults["null"]) | ||
.field("typeParameters", or(def("TypeParameterInstantiation"), null), defaults["null"]); | ||
def("InterfaceTypeAnnotation") | ||
.bases("FlowType") | ||
.build("body", "extends") | ||
.field("body", def("ObjectTypeAnnotation")) | ||
.field("extends", or([def("InterfaceExtends")], null), defaults["null"]); | ||
def("InterfaceDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body", "extends") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null), defaults["null"]) | ||
.field("body", def("ObjectTypeAnnotation")) | ||
.field("extends", [def("InterfaceExtends")]); | ||
def("DeclareInterface") | ||
.bases("InterfaceDeclaration") | ||
.build("id", "body", "extends"); | ||
def("InterfaceExtends") | ||
.bases("Node") | ||
.build("id") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterInstantiation"), null), defaults["null"]); | ||
def("TypeAlias") | ||
.bases("Declaration") | ||
.build("id", "typeParameters", "right") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)) | ||
.field("right", def("FlowType")); | ||
def("OpaqueType") | ||
.bases("Declaration") | ||
.build("id", "typeParameters", "impltype", "supertype") | ||
.field("id", def("Identifier")) | ||
.field("typeParameters", or(def("TypeParameterDeclaration"), null)) | ||
.field("implType", def("FlowType")) | ||
.field("superType", def("FlowType")); | ||
def("DeclareTypeAlias") | ||
.bases("TypeAlias") | ||
.build("id", "typeParameters", "right"); | ||
def("DeclareOpaqueType") | ||
.bases("TypeAlias") | ||
.build("id", "typeParameters", "supertype"); | ||
def("TypeCastExpression") | ||
.bases("Expression") | ||
.build("expression", "typeAnnotation") | ||
.field("expression", def("Expression")) | ||
.field("typeAnnotation", def("TypeAnnotation")); | ||
def("TupleTypeAnnotation") | ||
.bases("FlowType") | ||
.build("types") | ||
.field("types", [def("FlowType")]); | ||
def("DeclareVariable") | ||
.bases("Statement") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("DeclareFunction") | ||
.bases("Statement") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("DeclareClass") | ||
.bases("InterfaceDeclaration") | ||
.build("id"); | ||
def("DeclareModule") | ||
.bases("Statement") | ||
.build("id", "body") | ||
.field("id", or(def("Identifier"), def("Literal"))) | ||
.field("body", def("BlockStatement")); | ||
def("DeclareModuleExports") | ||
.bases("Statement") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TypeAnnotation")); | ||
def("DeclareExportDeclaration") | ||
.bases("Declaration") | ||
.build("default", "declaration", "specifiers", "source") | ||
.field("default", Boolean) | ||
.field("declaration", or(def("DeclareVariable"), def("DeclareFunction"), def("DeclareClass"), def("FlowType"), // Implies default. | ||
null)) | ||
.field("specifiers", [or(def("ExportSpecifier"), def("ExportBatchSpecifier"))], defaults.emptyArray) | ||
.field("source", or(def("Literal"), null), defaults["null"]); | ||
def("DeclareExportAllDeclaration") | ||
.bases("Declaration") | ||
.build("source") | ||
.field("source", or(def("Literal"), null), defaults["null"]); | ||
def("FlowPredicate").bases("Flow"); | ||
def("InferredPredicate") | ||
.bases("FlowPredicate") | ||
.build(); | ||
def("DeclaredPredicate") | ||
.bases("FlowPredicate") | ||
.build("value") | ||
.field("value", def("Expression")); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
224
def/jsx.js
@@ -1,131 +0,105 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./es7")); | ||
var types = fork.use(require("../lib/types")); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
def("JSXAttribute") | ||
.bases("Node") | ||
.build("name", "value") | ||
.field("name", or(def("JSXIdentifier"), def("JSXNamespacedName"))) | ||
.field("value", or( | ||
def("Literal"), // attr="value" | ||
def("JSXExpressionContainer"), // attr={value} | ||
null // attr= or just attr | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var es7_1 = __importDefault(require("./es7")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(es7_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
def("JSXAttribute") | ||
.bases("Node") | ||
.build("name", "value") | ||
.field("name", or(def("JSXIdentifier"), def("JSXNamespacedName"))) | ||
.field("value", or(def("Literal"), // attr="value" | ||
def("JSXExpressionContainer"), // attr={value} | ||
null // attr= or just attr | ||
), defaults["null"]); | ||
def("JSXIdentifier") | ||
.bases("Identifier") | ||
.build("name") | ||
.field("name", String); | ||
def("JSXNamespacedName") | ||
.bases("Node") | ||
.build("namespace", "name") | ||
.field("namespace", def("JSXIdentifier")) | ||
.field("name", def("JSXIdentifier")); | ||
def("JSXMemberExpression") | ||
.bases("MemberExpression") | ||
.build("object", "property") | ||
.field("object", or(def("JSXIdentifier"), def("JSXMemberExpression"))) | ||
.field("property", def("JSXIdentifier")) | ||
.field("computed", Boolean, defaults.false); | ||
var JSXElementName = or( | ||
def("JSXIdentifier"), | ||
def("JSXNamespacedName"), | ||
def("JSXIdentifier") | ||
.bases("Identifier") | ||
.build("name") | ||
.field("name", String); | ||
def("JSXNamespacedName") | ||
.bases("Node") | ||
.build("namespace", "name") | ||
.field("namespace", def("JSXIdentifier")) | ||
.field("name", def("JSXIdentifier")); | ||
def("JSXMemberExpression") | ||
); | ||
def("JSXSpreadAttribute") | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
var JSXAttributes = [or( | ||
def("JSXAttribute"), | ||
.bases("MemberExpression") | ||
.build("object", "property") | ||
.field("object", or(def("JSXIdentifier"), def("JSXMemberExpression"))) | ||
.field("property", def("JSXIdentifier")) | ||
.field("computed", Boolean, defaults.false); | ||
var JSXElementName = or(def("JSXIdentifier"), def("JSXNamespacedName"), def("JSXMemberExpression")); | ||
def("JSXSpreadAttribute") | ||
)]; | ||
def("JSXExpressionContainer") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("JSXElement") | ||
.bases("Expression") | ||
.build("openingElement", "closingElement", "children") | ||
.field("openingElement", def("JSXOpeningElement")) | ||
.field("closingElement", or(def("JSXClosingElement"), null), defaults["null"]) | ||
.field("children", [or( | ||
def("JSXElement"), | ||
def("JSXExpressionContainer"), | ||
def("JSXFragment"), | ||
def("JSXText"), | ||
def("Literal") // TODO Esprima should return JSXText instead. | ||
)], defaults.emptyArray) | ||
.field("name", JSXElementName, function () { | ||
// Little-known fact: the `this` object inside a default function | ||
// is none other than the partially-built object itself, and any | ||
// fields initialized directly from builder function arguments | ||
// (like openingElement, closingElement, and children) are | ||
// guaranteed to be available. | ||
return this.openingElement.name; | ||
.bases("Node") | ||
.build("argument") | ||
.field("argument", def("Expression")); | ||
var JSXAttributes = [or(def("JSXAttribute"), def("JSXSpreadAttribute"))]; | ||
def("JSXExpressionContainer") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("JSXElement") | ||
.bases("Expression") | ||
.build("openingElement", "closingElement", "children") | ||
.field("openingElement", def("JSXOpeningElement")) | ||
.field("closingElement", or(def("JSXClosingElement"), null), defaults["null"]) | ||
.field("children", [or(def("JSXElement"), def("JSXExpressionContainer"), def("JSXFragment"), def("JSXText"), def("Literal") // TODO Esprima should return JSXText instead. | ||
)], defaults.emptyArray) | ||
.field("name", JSXElementName, function () { | ||
// Little-known fact: the `this` object inside a default function | ||
// is none other than the partially-built object itself, and any | ||
// fields initialized directly from builder function arguments | ||
// (like openingElement, closingElement, and children) are | ||
// guaranteed to be available. | ||
return this.openingElement.name; | ||
}, true) // hidden from traversal | ||
.field("selfClosing", Boolean, function () { | ||
return this.openingElement.selfClosing; | ||
.field("selfClosing", Boolean, function () { | ||
return this.openingElement.selfClosing; | ||
}, true) // hidden from traversal | ||
.field("attributes", JSXAttributes, function () { | ||
return this.openingElement.attributes; | ||
.field("attributes", JSXAttributes, function () { | ||
return this.openingElement.attributes; | ||
}, true); // hidden from traversal | ||
def("JSXOpeningElement") | ||
.bases("Node") // TODO Does this make sense? Can't really be an JSXElement. | ||
.build("name", "attributes", "selfClosing") | ||
.field("name", JSXElementName) | ||
.field("attributes", JSXAttributes, defaults.emptyArray) | ||
.field("selfClosing", Boolean, defaults["false"]); | ||
def("JSXClosingElement") | ||
.bases("Node") // TODO Same concern. | ||
.build("name") | ||
.field("name", JSXElementName); | ||
def("JSXFragment") | ||
.bases("Expression") | ||
.build("openingElement", "closingElement", "children") | ||
.field("openingElement", def("JSXOpeningFragment")) | ||
.field("closingElement", def("JSXClosingFragment")) | ||
.field("children", [or( | ||
def("JSXElement"), | ||
def("JSXExpressionContainer"), | ||
def("JSXFragment"), | ||
def("JSXText"), | ||
def("Literal") // TODO Esprima should return JSXText instead. | ||
)], defaults.emptyArray) | ||
def("JSXOpeningFragment") | ||
.bases("Node") // TODO Same concern. | ||
.build(); | ||
def("JSXClosingFragment") | ||
.bases("Node") // TODO Same concern. | ||
.build(); | ||
def("JSXText") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", String); | ||
def("JSXEmptyExpression").bases("Expression").build(); | ||
// This PR has caused many people issues, but supporting it seems like a | ||
// good idea anyway: https://github.com/babel/babel/pull/4988 | ||
def("JSXSpreadChild") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
}; | ||
def("JSXOpeningElement") | ||
.bases("Node") // TODO Does this make sense? Can't really be an JSXElement. | ||
.build("name", "attributes", "selfClosing") | ||
.field("name", JSXElementName) | ||
.field("attributes", JSXAttributes, defaults.emptyArray) | ||
.field("selfClosing", Boolean, defaults["false"]); | ||
def("JSXClosingElement") | ||
.bases("Node") // TODO Same concern. | ||
.build("name") | ||
.field("name", JSXElementName); | ||
def("JSXFragment") | ||
.bases("Expression") | ||
.build("openingElement", "closingElement", "children") | ||
.field("openingElement", def("JSXOpeningFragment")) | ||
.field("closingElement", def("JSXClosingFragment")) | ||
.field("children", [or(def("JSXElement"), def("JSXExpressionContainer"), def("JSXFragment"), def("JSXText"), def("Literal") // TODO Esprima should return JSXText instead. | ||
)], defaults.emptyArray); | ||
def("JSXOpeningFragment") | ||
.bases("Node") // TODO Same concern. | ||
.build(); | ||
def("JSXClosingFragment") | ||
.bases("Node") // TODO Same concern. | ||
.build(); | ||
def("JSXText") | ||
.bases("Literal") | ||
.build("value") | ||
.field("value", String); | ||
def("JSXEmptyExpression").bases("Expression").build(); | ||
// This PR has caused many people issues, but supporting it seems like a | ||
// good idea anyway: https://github.com/babel/babel/pull/4988 | ||
def("JSXSpreadChild") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,18 +0,23 @@ | ||
module.exports = function (fork) { | ||
fork.use(require("./core")); | ||
var types = fork.use(require("../lib/types")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var core_1 = __importDefault(require("./core")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
fork.use(core_1.default); | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var shared = fork.use(require("../lib/shared")); | ||
var shared = fork.use(shared_1.default); | ||
var geq = shared.geq; | ||
var defaults = shared.defaults; | ||
def("Function") | ||
// SpiderMonkey allows expression closures: function(x) x+1 | ||
.field("body", or(def("BlockStatement"), def("Expression"))); | ||
def("ForInStatement") | ||
.build("left", "right", "body", "each") | ||
.field("each", Boolean, defaults["false"]); | ||
def("LetStatement") | ||
@@ -24,3 +29,2 @@ .bases("Statement") | ||
.field("body", def("Statement")); | ||
def("LetExpression") | ||
@@ -32,3 +36,2 @@ .bases("Expression") | ||
.field("body", def("Expression")); | ||
def("GraphExpression") | ||
@@ -39,3 +42,2 @@ .bases("Expression") | ||
.field("expression", def("Literal")); | ||
def("GraphIndexExpression") | ||
@@ -45,2 +47,4 @@ .bases("Expression") | ||
.field("index", geq(0)); | ||
}; | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -0,61 +1,42 @@ | ||
"use strict";; | ||
/** | ||
* Type annotation defs shared between Flow and TypeScript. | ||
* These defs could not be defined in ./flow.js or ./typescript.js directly | ||
* These defs could not be defined in ./flow.ts or ./typescript.ts directly | ||
* because they use the same name. | ||
*/ | ||
module.exports = function (fork) { | ||
var types = fork.use(require("../lib/types")); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
var TypeAnnotation = or( | ||
def("TypeAnnotation"), | ||
def("TSTypeAnnotation"), | ||
null | ||
); | ||
var TypeParamDecl = or( | ||
def("TypeParameterDeclaration"), | ||
def("TSTypeParameterDeclaration"), | ||
null | ||
); | ||
def("Identifier") | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
def("ObjectPattern") | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
def("Function") | ||
.field("returnType", TypeAnnotation, defaults["null"]) | ||
.field("typeParameters", TypeParamDecl, defaults["null"]); | ||
def("ClassProperty") | ||
.build("key", "value", "typeAnnotation", "static") | ||
.field("value", or(def("Expression"), null)) | ||
.field("static", Boolean, defaults["false"]) | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
["ClassDeclaration", | ||
"ClassExpression", | ||
].forEach(typeName => { | ||
def(typeName) | ||
.field("typeParameters", TypeParamDecl, defaults["null"]) | ||
.field("superTypeParameters", | ||
or(def("TypeParameterInstantiation"), | ||
def("TSTypeParameterInstantiation"), | ||
null), | ||
defaults["null"]); | ||
}); | ||
["ClassDeclaration", | ||
"ClassExpression", | ||
].forEach(typeName => { | ||
def(typeName) | ||
.field("implements", | ||
or([def("ClassImplements")], | ||
[def("TSExpressionWithTypeArguments")]), | ||
defaults.emptyArray); | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
var types = fork.use(types_1.default); | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
var TypeAnnotation = or(def("TypeAnnotation"), def("TSTypeAnnotation"), null); | ||
var TypeParamDecl = or(def("TypeParameterDeclaration"), def("TSTypeParameterDeclaration"), null); | ||
def("Identifier") | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
def("ObjectPattern") | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
def("Function") | ||
.field("returnType", TypeAnnotation, defaults["null"]) | ||
.field("typeParameters", TypeParamDecl, defaults["null"]); | ||
def("ClassProperty") | ||
.build("key", "value", "typeAnnotation", "static") | ||
.field("value", or(def("Expression"), null)) | ||
.field("static", Boolean, defaults["false"]) | ||
.field("typeAnnotation", TypeAnnotation, defaults["null"]); | ||
["ClassDeclaration", | ||
"ClassExpression", | ||
].forEach(function (typeName) { | ||
def(typeName) | ||
.field("typeParameters", TypeParamDecl, defaults["null"]) | ||
.field("superTypeParameters", or(def("TypeParameterInstantiation"), def("TSTypeParameterInstantiation"), null), defaults["null"]) | ||
.field("implements", or([def("ClassImplements")], [def("TSExpressionWithTypeArguments")]), defaults.emptyArray); | ||
}); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,450 +0,328 @@ | ||
module.exports = function (fork) { | ||
// Since TypeScript is parsed by Babylon, include the core Babylon types | ||
// but omit the Flow-related types. | ||
fork.use(require("./babel-core")); | ||
fork.use(require("./type-annotations")); | ||
var types = fork.use(require("../lib/types")); | ||
var n = types.namedTypes; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(require("../lib/shared")).defaults; | ||
var StringLiteral = new types.Type(function (value, deep) { | ||
if (n.StringLiteral && | ||
n.StringLiteral.check(value, deep)) { | ||
return true | ||
} | ||
if (n.Literal && | ||
n.Literal.check(value, deep) && | ||
typeof value.value === "string") { | ||
return true; | ||
} | ||
return false; | ||
}, "StringLiteral"); | ||
def("TSType") | ||
.bases("Node"); | ||
var IdOrQualifiedName = or( | ||
def("Identifier"), | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var babel_core_1 = __importDefault(require("./babel-core")); | ||
var type_annotations_1 = __importDefault(require("./type-annotations")); | ||
var types_1 = __importDefault(require("../lib/types")); | ||
var shared_1 = __importDefault(require("../lib/shared")); | ||
function default_1(fork) { | ||
// Since TypeScript is parsed by Babylon, include the core Babylon types | ||
// but omit the Flow-related types. | ||
fork.use(babel_core_1.default); | ||
fork.use(type_annotations_1.default); | ||
var types = fork.use(types_1.default); | ||
var n = types.namedTypes; | ||
var def = types.Type.def; | ||
var or = types.Type.or; | ||
var defaults = fork.use(shared_1.default).defaults; | ||
var StringLiteral = types.Type.from(function (value, deep) { | ||
if (n.StringLiteral && | ||
n.StringLiteral.check(value, deep)) { | ||
return true; | ||
} | ||
if (n.Literal && | ||
n.Literal.check(value, deep) && | ||
typeof value.value === "string") { | ||
return true; | ||
} | ||
return false; | ||
}, "StringLiteral"); | ||
def("TSType") | ||
.bases("Node"); | ||
var IdOrQualifiedName = or(def("Identifier"), def("TSQualifiedName")); | ||
def("TSTypeReference") | ||
.bases("TSType") | ||
.build("typeName", "typeParameters") | ||
.field("typeName", IdOrQualifiedName) | ||
.field("typeParameters", or(def("TSTypeParameterInstantiation"), null), defaults["null"]); | ||
// An abstract (non-buildable) base type that provide a commonly-needed | ||
// optional .typeParameters field. | ||
def("TSHasOptionalTypeParameters") | ||
.field("typeParameters", or(def("TSTypeParameterDeclaration"), null, void 0), defaults["null"]); | ||
// An abstract (non-buildable) base type that provide a commonly-needed | ||
// optional .typeAnnotation field. | ||
def("TSHasOptionalTypeAnnotation") | ||
.field("typeAnnotation", or(def("TSTypeAnnotation"), null), defaults["null"]); | ||
def("TSQualifiedName") | ||
); | ||
def("TSTypeReference") | ||
.bases("TSType") | ||
.build("typeName", "typeParameters") | ||
.field("typeName", IdOrQualifiedName) | ||
.field("typeParameters", | ||
or(def("TSTypeParameterInstantiation"), null), | ||
defaults["null"]); | ||
// An abstract (non-buildable) base type that provide a commonly-needed | ||
// optional .typeParameters field. | ||
def("TSHasOptionalTypeParameters") | ||
.field("typeParameters", | ||
or(def("TSTypeParameterDeclaration"), null), | ||
defaults["null"]); | ||
// An abstract (non-buildable) base type that provide a commonly-needed | ||
// optional .typeAnnotation field. | ||
def("TSHasOptionalTypeAnnotation") | ||
.field("typeAnnotation", | ||
or(def("TSTypeAnnotation"), null), | ||
defaults["null"]); | ||
def("TSQualifiedName") | ||
.bases("Node") | ||
.build("left", "right") | ||
.field("left", IdOrQualifiedName) | ||
.field("right", IdOrQualifiedName); | ||
def("TSAsExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")) | ||
.field("typeAnnotation", def("TSType")) | ||
.field("extra", | ||
or({ parenthesized: Boolean }, null), | ||
defaults["null"]); | ||
def("TSNonNullExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
[ // Define all the simple keyword types. | ||
"TSAnyKeyword", | ||
"TSBooleanKeyword", | ||
"TSNeverKeyword", | ||
"TSNullKeyword", | ||
"TSNumberKeyword", | ||
"TSObjectKeyword", | ||
"TSStringKeyword", | ||
"TSSymbolKeyword", | ||
"TSUndefinedKeyword", | ||
"TSUnknownKeyword", | ||
"TSVoidKeyword", | ||
"TSThisType", | ||
].forEach(keywordType => { | ||
def(keywordType) | ||
.bases("TSType") | ||
.build(); | ||
}); | ||
def("TSArrayType") | ||
.bases("TSType") | ||
.build("elementType") | ||
.field("elementType", def("TSType")) | ||
def("TSLiteralType") | ||
.bases("TSType") | ||
.build("literal") | ||
.field("literal", | ||
or(def("NumericLiteral"), | ||
def("StringLiteral"), | ||
def("BooleanLiteral"))); | ||
["TSUnionType", | ||
"TSIntersectionType", | ||
].forEach(typeName => { | ||
def(typeName) | ||
.bases("TSType") | ||
.build("types") | ||
.field("types", [def("TSType")]); | ||
}); | ||
def("TSConditionalType") | ||
.bases("TSType") | ||
.build("checkType", "extendsType", "trueType", "falseType") | ||
.field("checkType", def("TSType")) | ||
.field("extendsType", def("TSType")) | ||
.field("trueType", def("TSType")) | ||
.field("falseType", def("TSType")); | ||
def("TSInferType") | ||
.bases("TSType") | ||
.build("typeParameter") | ||
.field("typeParameter", def("TSTypeParameter")); | ||
def("TSParenthesizedType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
var ParametersType = [or( | ||
def("Identifier"), | ||
def("RestElement") | ||
)]; | ||
["TSFunctionType", | ||
"TSConstructorType", | ||
].forEach(typeName => { | ||
def(typeName) | ||
.bases("TSType", | ||
"TSHasOptionalTypeParameters", | ||
"TSHasOptionalTypeAnnotation") | ||
.build("parameters") | ||
.field("parameters", ParametersType); | ||
}); | ||
def("TSDeclareFunction") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id", "params", "returnType") | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("params", [def("Pattern")]) | ||
// tSFunctionTypeAnnotationCommon | ||
.field("returnType", | ||
or(def("TSTypeAnnotation"), | ||
def("Noop"), // Still used? | ||
null), | ||
defaults["null"]); | ||
def("TSDeclareMethod") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("key", "params", "returnType") | ||
.field("async", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("params", [def("Pattern")]) | ||
// classMethodOrPropertyCommon | ||
.field("abstract", Boolean, defaults["false"]) | ||
.field("accessibility", | ||
or("public", "private", "protected", void 0), | ||
defaults["undefined"]) | ||
.field("static", Boolean, defaults["false"]) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("key", or( | ||
def("Identifier"), | ||
def("StringLiteral"), | ||
def("NumericLiteral"), | ||
// Only allowed if .computed is true. | ||
def("Expression") | ||
)) | ||
// classMethodOrDeclareMethodCommon | ||
.field("kind", | ||
or("get", "set", "method", "constructor"), | ||
function getDefault() { return "method"; }) | ||
.field("access", // Not "accessibility"? | ||
or("public", "private", "protected", void 0), | ||
defaults["undefined"]) | ||
.field("decorators", | ||
or([def("Decorator")], null), | ||
defaults["null"]) | ||
// tSFunctionTypeAnnotationCommon | ||
.field("returnType", | ||
or(def("TSTypeAnnotation"), | ||
def("Noop"), // Still used? | ||
null), | ||
defaults["null"]); | ||
def("TSMappedType") | ||
.bases("TSType") | ||
.build("typeParameter", "typeAnnotation") | ||
.field("readonly", or(Boolean, "+", "-"), defaults["false"]) | ||
.field("typeParameter", def("TSTypeParameter")) | ||
.field("optional", or(Boolean, "+", "-"), defaults["false"]) | ||
.field("typeAnnotation", | ||
or(def("TSType"), null), | ||
defaults["null"]); | ||
def("TSTupleType") | ||
.bases("TSType") | ||
.build("elementTypes") | ||
.field("elementTypes", [def("TSType")]); | ||
def("TSRestType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSOptionalType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSIndexedAccessType") | ||
.bases("TSType") | ||
.build("objectType", "indexType") | ||
.field("objectType", def("TSType")) | ||
.field("indexType", def("TSType")) | ||
def("TSTypeOperator") | ||
.bases("TSType") | ||
.build("operator") | ||
.field("operator", String) | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSTypeAnnotation") | ||
.bases("Node") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", | ||
or(def("TSType"), | ||
def("TSTypeAnnotation"))); | ||
def("TSIndexSignature") | ||
.bases("Declaration", "TSHasOptionalTypeAnnotation") | ||
.build("parameters") | ||
.field("parameters", [def("Identifier")]) // Length === 1 | ||
.field("readonly", Boolean, defaults["false"]); | ||
def("TSPropertySignature") | ||
.bases("Declaration", "TSHasOptionalTypeAnnotation") | ||
.build("key") | ||
.field("key", def("Expression")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("readonly", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("initializer", | ||
or(def("Expression"), null), | ||
defaults["null"]); | ||
def("TSMethodSignature") | ||
.bases("Declaration", | ||
"TSHasOptionalTypeParameters", | ||
"TSHasOptionalTypeAnnotation") | ||
.build("key") | ||
.field("key", def("Expression")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("parameters", ParametersType); | ||
def("TSTypePredicate") | ||
.bases("TSTypeAnnotation") | ||
.build("parameterName", "typeAnnotation") | ||
.field("parameterName", | ||
or(def("Identifier"), | ||
def("TSThisType"))) | ||
.field("typeAnnotation", def("TSTypeAnnotation")); | ||
["TSCallSignatureDeclaration", | ||
"TSConstructSignatureDeclaration", | ||
].forEach(typeName => { | ||
def(typeName) | ||
.bases("Declaration", | ||
"TSHasOptionalTypeParameters", | ||
"TSHasOptionalTypeAnnotation") | ||
.build("parameters") | ||
.field("parameters", ParametersType); | ||
}); | ||
def("TSEnumMember") | ||
.bases("Node") | ||
.build("id", "initializer") | ||
.field("id", or(def("Identifier"), StringLiteral)) | ||
.field("initializer", | ||
or(def("Expression"), null), | ||
defaults["null"]); | ||
def("TSTypeQuery") | ||
.bases("TSType") | ||
.build("exprName") | ||
.field("exprName", IdOrQualifiedName); | ||
// Inferred from Babylon's tsParseTypeMember method. | ||
var TSTypeMember = or( | ||
def("TSCallSignatureDeclaration"), | ||
def("TSConstructSignatureDeclaration"), | ||
def("TSIndexSignature"), | ||
def("TSMethodSignature"), | ||
.bases("Node") | ||
.build("left", "right") | ||
.field("left", IdOrQualifiedName) | ||
.field("right", IdOrQualifiedName); | ||
def("TSAsExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")) | ||
.field("typeAnnotation", def("TSType")) | ||
.field("extra", or({ parenthesized: Boolean }, null), defaults["null"]); | ||
def("TSNonNullExpression") | ||
.bases("Expression") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
[ | ||
"TSAnyKeyword", | ||
"TSBooleanKeyword", | ||
"TSNeverKeyword", | ||
"TSNullKeyword", | ||
"TSNumberKeyword", | ||
"TSObjectKeyword", | ||
"TSStringKeyword", | ||
"TSSymbolKeyword", | ||
"TSUndefinedKeyword", | ||
"TSUnknownKeyword", | ||
"TSVoidKeyword", | ||
"TSThisType", | ||
].forEach(function (keywordType) { | ||
def(keywordType) | ||
.bases("TSType") | ||
.build(); | ||
}); | ||
def("TSArrayType") | ||
.bases("TSType") | ||
.build("elementType") | ||
.field("elementType", def("TSType")); | ||
def("TSLiteralType") | ||
.bases("TSType") | ||
.build("literal") | ||
.field("literal", or(def("NumericLiteral"), def("StringLiteral"), def("BooleanLiteral"))); | ||
["TSUnionType", | ||
"TSIntersectionType", | ||
].forEach(function (typeName) { | ||
def(typeName) | ||
.bases("TSType") | ||
.build("types") | ||
.field("types", [def("TSType")]); | ||
}); | ||
def("TSConditionalType") | ||
.bases("TSType") | ||
.build("checkType", "extendsType", "trueType", "falseType") | ||
.field("checkType", def("TSType")) | ||
.field("extendsType", def("TSType")) | ||
.field("trueType", def("TSType")) | ||
.field("falseType", def("TSType")); | ||
def("TSInferType") | ||
.bases("TSType") | ||
.build("typeParameter") | ||
.field("typeParameter", def("TSTypeParameter")); | ||
def("TSParenthesizedType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
var ParametersType = [or(def("Identifier"), def("RestElement"), def("ObjectPattern"))]; | ||
["TSFunctionType", | ||
"TSConstructorType", | ||
].forEach(function (typeName) { | ||
def(typeName) | ||
.bases("TSType", "TSHasOptionalTypeParameters", "TSHasOptionalTypeAnnotation") | ||
.build("parameters") | ||
.field("parameters", ParametersType); | ||
}); | ||
def("TSDeclareFunction") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id", "params", "returnType") | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("async", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("id", or(def("Identifier"), null), defaults["null"]) | ||
.field("params", [def("Pattern")]) | ||
// tSFunctionTypeAnnotationCommon | ||
.field("returnType", or(def("TSTypeAnnotation"), def("Noop"), // Still used? | ||
null), defaults["null"]); | ||
def("TSDeclareMethod") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("key", "params", "returnType") | ||
.field("async", Boolean, defaults["false"]) | ||
.field("generator", Boolean, defaults["false"]) | ||
.field("params", [def("Pattern")]) | ||
// classMethodOrPropertyCommon | ||
.field("abstract", Boolean, defaults["false"]) | ||
.field("accessibility", or("public", "private", "protected", void 0), defaults["undefined"]) | ||
.field("static", Boolean, defaults["false"]) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("key", or(def("Identifier"), def("StringLiteral"), def("NumericLiteral"), | ||
// Only allowed if .computed is true. | ||
def("Expression"))) | ||
// classMethodOrDeclareMethodCommon | ||
.field("kind", or("get", "set", "method", "constructor"), function getDefault() { return "method"; }) | ||
.field("access", // Not "accessibility"? | ||
or("public", "private", "protected", void 0), defaults["undefined"]) | ||
.field("decorators", or([def("Decorator")], null), defaults["null"]) | ||
// tSFunctionTypeAnnotationCommon | ||
.field("returnType", or(def("TSTypeAnnotation"), def("Noop"), // Still used? | ||
null), defaults["null"]); | ||
def("TSMappedType") | ||
.bases("TSType") | ||
.build("typeParameter", "typeAnnotation") | ||
.field("readonly", or(Boolean, "+", "-"), defaults["false"]) | ||
.field("typeParameter", def("TSTypeParameter")) | ||
.field("optional", or(Boolean, "+", "-"), defaults["false"]) | ||
.field("typeAnnotation", or(def("TSType"), null), defaults["null"]); | ||
def("TSTupleType") | ||
.bases("TSType") | ||
.build("elementTypes") | ||
.field("elementTypes", [def("TSType")]); | ||
def("TSRestType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSOptionalType") | ||
.bases("TSType") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSIndexedAccessType") | ||
.bases("TSType") | ||
.build("objectType", "indexType") | ||
.field("objectType", def("TSType")) | ||
.field("indexType", def("TSType")); | ||
def("TSTypeOperator") | ||
.bases("TSType") | ||
.build("operator") | ||
.field("operator", String) | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSTypeAnnotation") | ||
.bases("Node") | ||
.build("typeAnnotation") | ||
.field("typeAnnotation", or(def("TSType"), def("TSTypeAnnotation"))); | ||
def("TSIndexSignature") | ||
.bases("Declaration", "TSHasOptionalTypeAnnotation") | ||
.build("parameters", "typeAnnotation") | ||
.field("parameters", [def("Identifier")]) // Length === 1 | ||
.field("readonly", Boolean, defaults["false"]); | ||
def("TSPropertySignature") | ||
); | ||
def("TSTypeLiteral") | ||
.bases("TSType") | ||
.build("members") | ||
.field("members", [TSTypeMember]); | ||
def("TSTypeParameter") | ||
.bases("Identifier") | ||
.build("name", "constraint", "default") | ||
.field("name", String) | ||
.field("constraint", or(def("TSType"), null), defaults["null"]) | ||
.field("default", or(def("TSType"), null), defaults["null"]); | ||
def("TSTypeAssertion") | ||
.bases("Expression") | ||
.build("typeAnnotation", "expression") | ||
.field("typeAnnotation", def("TSType")) | ||
.field("expression", def("Expression")) | ||
.field("extra", | ||
or({ parenthesized: Boolean }, null), | ||
defaults["null"]); | ||
def("TSTypeParameterDeclaration") | ||
.bases("Declaration") | ||
.build("params") | ||
.field("params", [def("TSTypeParameter")]); | ||
def("TSTypeParameterInstantiation") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("TSType")]); | ||
def("TSEnumDeclaration") | ||
.bases("Declaration") | ||
.build("id", "members") | ||
.field("id", def("Identifier")) | ||
.field("const", Boolean, defaults["false"]) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("members", [def("TSEnumMember")]) | ||
.field("initializer", | ||
or(def("Expression"), null), | ||
defaults["null"]); | ||
def("TSTypeAliasDeclaration") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id") | ||
.field("id", def("Identifier")) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSModuleBlock") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [def("Statement")]); | ||
def("TSModuleDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body") | ||
.field("id", or(StringLiteral, IdOrQualifiedName)) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("global", Boolean, defaults["false"]) | ||
.field("body", | ||
or(def("TSModuleBlock"), | ||
def("TSModuleDeclaration"), | ||
null), | ||
defaults["null"]); | ||
def("TSImportEqualsDeclaration") | ||
.bases("Declaration") | ||
.build("id", "moduleReference") | ||
.field("id", def("Identifier")) | ||
.field("isExport", Boolean, defaults["false"]) | ||
.field("moduleReference", | ||
or(IdOrQualifiedName, | ||
def("TSExternalModuleReference"))); | ||
def("TSExternalModuleReference") | ||
.bases("Declaration") | ||
.build("expression") | ||
.field("expression", StringLiteral); | ||
def("TSExportAssignment") | ||
.bases("Statement") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("TSNamespaceExportDeclaration") | ||
.bases("Declaration") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("TSInterfaceBody") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [TSTypeMember]); | ||
def("TSExpressionWithTypeArguments") | ||
.bases("TSType") | ||
.build("expression", "typeParameters") | ||
.field("expression", IdOrQualifiedName) | ||
.field("typeParameters", | ||
or(def("TSTypeParameterInstantiation"), null), | ||
defaults["null"]); | ||
def("TSInterfaceDeclaration") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id", "body") | ||
.field("id", IdOrQualifiedName) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("extends", | ||
or([def("TSExpressionWithTypeArguments")], null), | ||
defaults["null"]) | ||
.field("body", def("TSInterfaceBody")); | ||
def("TSParameterProperty") | ||
.bases("Pattern") | ||
.build("parameter") | ||
.field("accessibility", | ||
or("public", "private", "protected", void 0), | ||
defaults["undefined"]) | ||
.field("readonly", Boolean, defaults["false"]) | ||
.field("parameter", or(def("Identifier"), | ||
def("AssignmentPattern"))); | ||
// Defined already in es6 and babel-core. | ||
def("ClassBody") | ||
.field("body", [or( | ||
def("MethodDefinition"), | ||
def("VariableDeclarator"), | ||
def("ClassPropertyDefinition"), | ||
def("ClassProperty"), | ||
def("ClassMethod"), | ||
// Just need to add these types: | ||
def("TSDeclareMethod"), | ||
TSTypeMember | ||
)]); | ||
}; | ||
.bases("Declaration", "TSHasOptionalTypeAnnotation") | ||
.build("key", "typeAnnotation", "optional") | ||
.field("key", def("Expression")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("readonly", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("initializer", or(def("Expression"), null), defaults["null"]); | ||
def("TSMethodSignature") | ||
.bases("Declaration", "TSHasOptionalTypeParameters", "TSHasOptionalTypeAnnotation") | ||
.build("key", "parameters", "typeAnnotation") | ||
.field("key", def("Expression")) | ||
.field("computed", Boolean, defaults["false"]) | ||
.field("optional", Boolean, defaults["false"]) | ||
.field("parameters", ParametersType); | ||
def("TSTypePredicate") | ||
.bases("TSTypeAnnotation") | ||
.build("parameterName", "typeAnnotation") | ||
.field("parameterName", or(def("Identifier"), def("TSThisType"))) | ||
.field("typeAnnotation", def("TSTypeAnnotation")); | ||
["TSCallSignatureDeclaration", | ||
"TSConstructSignatureDeclaration", | ||
].forEach(function (typeName) { | ||
def(typeName) | ||
.bases("Declaration", "TSHasOptionalTypeParameters", "TSHasOptionalTypeAnnotation") | ||
.build("parameters", "typeAnnotation") | ||
.field("parameters", ParametersType); | ||
}); | ||
def("TSEnumMember") | ||
.bases("Node") | ||
.build("id", "initializer") | ||
.field("id", or(def("Identifier"), StringLiteral)) | ||
.field("initializer", or(def("Expression"), null), defaults["null"]); | ||
def("TSTypeQuery") | ||
.bases("TSType") | ||
.build("exprName") | ||
.field("exprName", IdOrQualifiedName); | ||
// Inferred from Babylon's tsParseTypeMember method. | ||
var TSTypeMember = or(def("TSCallSignatureDeclaration"), def("TSConstructSignatureDeclaration"), def("TSIndexSignature"), def("TSMethodSignature"), def("TSPropertySignature")); | ||
def("TSTypeLiteral") | ||
.bases("TSType") | ||
.build("members") | ||
.field("members", [TSTypeMember]); | ||
def("TSTypeParameter") | ||
.bases("Identifier") | ||
.build("name", "constraint", "default") | ||
.field("name", String) | ||
.field("constraint", or(def("TSType"), void 0), defaults["undefined"]) | ||
.field("default", or(def("TSType"), void 0), defaults["undefined"]); | ||
def("TSTypeAssertion") | ||
.bases("Expression") | ||
.build("typeAnnotation", "expression") | ||
.field("typeAnnotation", def("TSType")) | ||
.field("expression", def("Expression")) | ||
.field("extra", or({ parenthesized: Boolean }, null), defaults["null"]); | ||
def("TSTypeParameterDeclaration") | ||
.bases("Declaration") | ||
.build("params") | ||
.field("params", [def("TSTypeParameter")]); | ||
def("TSTypeParameterInstantiation") | ||
.bases("Node") | ||
.build("params") | ||
.field("params", [def("TSType")]); | ||
def("TSEnumDeclaration") | ||
.bases("Declaration") | ||
.build("id", "members") | ||
.field("id", def("Identifier")) | ||
.field("const", Boolean, defaults["false"]) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("members", [def("TSEnumMember")]) | ||
.field("initializer", or(def("Expression"), null), defaults["null"]); | ||
def("TSTypeAliasDeclaration") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id", "typeAnnotation") | ||
.field("id", def("Identifier")) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("typeAnnotation", def("TSType")); | ||
def("TSModuleBlock") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [def("Statement")]); | ||
def("TSModuleDeclaration") | ||
.bases("Declaration") | ||
.build("id", "body") | ||
.field("id", or(StringLiteral, IdOrQualifiedName)) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("global", Boolean, defaults["false"]) | ||
.field("body", or(def("TSModuleBlock"), def("TSModuleDeclaration"), null), defaults["null"]); | ||
def("TSImportEqualsDeclaration") | ||
.bases("Declaration") | ||
.build("id", "moduleReference") | ||
.field("id", def("Identifier")) | ||
.field("isExport", Boolean, defaults["false"]) | ||
.field("moduleReference", or(IdOrQualifiedName, def("TSExternalModuleReference"))); | ||
def("TSExternalModuleReference") | ||
.bases("Declaration") | ||
.build("expression") | ||
.field("expression", StringLiteral); | ||
def("TSExportAssignment") | ||
.bases("Statement") | ||
.build("expression") | ||
.field("expression", def("Expression")); | ||
def("TSNamespaceExportDeclaration") | ||
.bases("Declaration") | ||
.build("id") | ||
.field("id", def("Identifier")); | ||
def("TSInterfaceBody") | ||
.bases("Node") | ||
.build("body") | ||
.field("body", [TSTypeMember]); | ||
def("TSExpressionWithTypeArguments") | ||
.bases("TSType") | ||
.build("expression", "typeParameters") | ||
.field("expression", IdOrQualifiedName) | ||
.field("typeParameters", or(def("TSTypeParameterInstantiation"), null), defaults["null"]); | ||
def("TSInterfaceDeclaration") | ||
.bases("Declaration", "TSHasOptionalTypeParameters") | ||
.build("id", "body") | ||
.field("id", IdOrQualifiedName) | ||
.field("declare", Boolean, defaults["false"]) | ||
.field("extends", or([def("TSExpressionWithTypeArguments")], null), defaults["null"]) | ||
.field("body", def("TSInterfaceBody")); | ||
def("TSParameterProperty") | ||
.bases("Pattern") | ||
.build("parameter") | ||
.field("accessibility", or("public", "private", "protected", void 0), defaults["undefined"]) | ||
.field("readonly", Boolean, defaults["false"]) | ||
.field("parameter", or(def("Identifier"), def("AssignmentPattern"))); | ||
// Defined already in es6 and babel-core. | ||
def("ClassBody") | ||
.field("body", [or(def("MethodDefinition"), def("VariableDeclarator"), def("ClassPropertyDefinition"), def("ClassProperty"), def("ClassPrivateProperty"), def("ClassMethod"), def("ClassPrivateMethod"), | ||
// Just need to add these types: | ||
def("TSDeclareMethod"), TSTypeMember)]); | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
71
fork.js
@@ -1,24 +0,17 @@ | ||
module.exports = function (defs) { | ||
var used = []; | ||
var usedResult = []; | ||
var fork = {}; | ||
function use(plugin) { | ||
var idx = used.indexOf(plugin); | ||
if (idx === -1) { | ||
idx = used.length; | ||
used.push(plugin); | ||
usedResult[idx] = plugin(fork); | ||
} | ||
return usedResult[idx]; | ||
} | ||
fork.use = use; | ||
var types = use(require('./lib/types')); | ||
defs.forEach(use); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./lib/types")); | ||
var path_visitor_1 = __importDefault(require("./lib/path-visitor")); | ||
var equiv_1 = __importDefault(require("./lib/equiv")); | ||
var path_1 = __importDefault(require("./lib/path")); | ||
var node_path_1 = __importDefault(require("./lib/node-path")); | ||
function default_1(defs) { | ||
var fork = createFork(); | ||
var types = fork.use(types_1.default); | ||
defs.forEach(fork.use); | ||
types.finalize(); | ||
var PathVisitor = fork.use(path_visitor_1.default); | ||
var exports = { | ||
@@ -35,13 +28,29 @@ Type: types.Type, | ||
getSupertypeNames: types.getSupertypeNames, | ||
astNodesAreEquivalent: use(require("./lib/equiv")), | ||
getBuilderName: types.getBuilderName, | ||
astNodesAreEquivalent: fork.use(equiv_1.default), | ||
finalize: types.finalize, | ||
Path: use(require('./lib/path')), | ||
NodePath: use(require("./lib/node-path")), | ||
PathVisitor: use(require("./lib/path-visitor")), | ||
use: use | ||
Path: fork.use(path_1.default), | ||
NodePath: fork.use(node_path_1.default), | ||
PathVisitor: PathVisitor, | ||
use: fork.use, | ||
visit: PathVisitor.visit, | ||
}; | ||
exports.visit = exports.PathVisitor.visit; | ||
return exports; | ||
}; | ||
} | ||
exports.default = default_1; | ||
function createFork() { | ||
var used = []; | ||
var usedResult = []; | ||
function use(plugin) { | ||
var idx = used.indexOf(plugin); | ||
if (idx === -1) { | ||
idx = used.length; | ||
used.push(plugin); | ||
usedResult[idx] = plugin(fork); | ||
} | ||
return usedResult[idx]; | ||
} | ||
var fork = { use: use }; | ||
return fork; | ||
} | ||
module.exports = exports["default"]; |
@@ -1,3 +0,9 @@ | ||
module.exports = function (fork) { | ||
var types = fork.use(require('../lib/types')); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
function default_1(fork) { | ||
var types = fork.use(types_1.default); | ||
var getFieldNames = types.getFieldNames; | ||
@@ -10,13 +16,11 @@ var getFieldValue = types.getFieldValue; | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
function astNodesAreEquivalent(a, b, problemPath) { | ||
if (isArray.check(problemPath)) { | ||
problemPath.length = 0; | ||
} else { | ||
} | ||
else { | ||
problemPath = null; | ||
} | ||
return areEquivalent(a, b, problemPath); | ||
} | ||
astNodesAreEquivalent.assert = function (a, b) { | ||
@@ -29,11 +33,9 @@ var problemPath = []; | ||
} | ||
} else { | ||
throw new Error( | ||
"Nodes differ in the following path: " + | ||
problemPath.map(subscriptForProperty).join("") | ||
); | ||
} | ||
else { | ||
throw new Error("Nodes differ in the following path: " + | ||
problemPath.map(subscriptForProperty).join("")); | ||
} | ||
} | ||
}; | ||
function subscriptForProperty(property) { | ||
@@ -45,3 +47,2 @@ if (/[_$a-z][_$a-z0-9]*/i.test(property)) { | ||
} | ||
function areEquivalent(a, b, problemPath) { | ||
@@ -51,31 +52,22 @@ if (a === b) { | ||
} | ||
if (isArray.check(a)) { | ||
return arraysAreEquivalent(a, b, problemPath); | ||
} | ||
if (isObject.check(a)) { | ||
return objectsAreEquivalent(a, b, problemPath); | ||
} | ||
if (isDate.check(a)) { | ||
return isDate.check(b) && (+a === +b); | ||
} | ||
if (isRegExp.check(a)) { | ||
return isRegExp.check(b) && ( | ||
a.source === b.source && | ||
return isRegExp.check(b) && (a.source === b.source && | ||
a.global === b.global && | ||
a.multiline === b.multiline && | ||
a.ignoreCase === b.ignoreCase | ||
); | ||
a.ignoreCase === b.ignoreCase); | ||
} | ||
return a == b; | ||
} | ||
function arraysAreEquivalent(a, b, problemPath) { | ||
isArray.assert(a); | ||
var aLength = a.length; | ||
if (!isArray.check(b) || b.length !== aLength) { | ||
@@ -87,3 +79,2 @@ if (problemPath) { | ||
} | ||
for (var i = 0; i < aLength; ++i) { | ||
@@ -93,11 +84,8 @@ if (problemPath) { | ||
} | ||
if (i in a !== i in b) { | ||
return false; | ||
} | ||
if (!areEquivalent(a[i], b[i], problemPath)) { | ||
return false; | ||
} | ||
if (problemPath) { | ||
@@ -110,6 +98,4 @@ var problemPathTail = problemPath.pop(); | ||
} | ||
return true; | ||
} | ||
function objectsAreEquivalent(a, b, problemPath) { | ||
@@ -120,3 +106,2 @@ isObject.assert(a); | ||
} | ||
// Fast path for a common property of AST nodes. | ||
@@ -129,9 +114,6 @@ if (a.type !== b.type) { | ||
} | ||
var aNames = getFieldNames(a); | ||
var aNameCount = aNames.length; | ||
var bNames = getFieldNames(b); | ||
var bNameCount = bNames.length; | ||
if (aNameCount === bNameCount) { | ||
@@ -142,11 +124,8 @@ for (var i = 0; i < aNameCount; ++i) { | ||
var bChild = getFieldValue(b, name); | ||
if (problemPath) { | ||
problemPath.push(name); | ||
} | ||
if (!areEquivalent(aChild, bChild, problemPath)) { | ||
return false; | ||
} | ||
if (problemPath) { | ||
@@ -159,22 +138,15 @@ var problemPathTail = problemPath.pop(); | ||
} | ||
return true; | ||
} | ||
if (!problemPath) { | ||
return false; | ||
} | ||
// Since aNameCount !== bNameCount, we need to find some name that's | ||
// missing in aNames but present in bNames, or vice-versa. | ||
var seenNames = Object.create(null); | ||
for (i = 0; i < aNameCount; ++i) { | ||
seenNames[aNames[i]] = true; | ||
} | ||
for (i = 0; i < bNameCount; ++i) { | ||
name = bNames[i]; | ||
if (!hasOwn.call(seenNames, name)) { | ||
@@ -184,6 +156,4 @@ problemPath.push(name); | ||
} | ||
delete seenNames[name]; | ||
} | ||
for (name in seenNames) { | ||
@@ -193,7 +163,7 @@ problemPath.push(name); | ||
} | ||
return false; | ||
} | ||
return astNodesAreEquivalent; | ||
}; | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
@@ -1,3 +0,11 @@ | ||
module.exports = function (fork) { | ||
var types = fork.use(require("./types")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
var path_1 = __importDefault(require("./path")); | ||
var scope_1 = __importDefault(require("./scope")); | ||
function nodePathPlugin(fork) { | ||
var types = fork.use(types_1.default); | ||
var n = types.namedTypes; | ||
@@ -7,6 +15,5 @@ var b = types.builders; | ||
var isArray = types.builtInTypes.array; | ||
var Path = fork.use(require("./path")); | ||
var Scope = fork.use(require("./scope")); | ||
function NodePath(value, parentPath, name) { | ||
var Path = fork.use(path_1.default); | ||
var Scope = fork.use(scope_1.default); | ||
var NodePath = function NodePath(value, parentPath, name) { | ||
if (!(this instanceof NodePath)) { | ||
@@ -16,4 +23,3 @@ throw new Error("NodePath constructor cannot be invoked without 'new'"); | ||
Path.call(this, value, parentPath, name); | ||
} | ||
}; | ||
var NPp = NodePath.prototype = Object.create(Path.prototype, { | ||
@@ -27,3 +33,2 @@ constructor: { | ||
}); | ||
Object.defineProperties(NPp, { | ||
@@ -33,28 +38,23 @@ node: { | ||
Object.defineProperty(this, "node", { | ||
configurable: true, // Enable deletion. | ||
configurable: true, | ||
value: this._computeNode() | ||
}); | ||
return this.node; | ||
} | ||
}, | ||
parent: { | ||
get: function () { | ||
Object.defineProperty(this, "parent", { | ||
configurable: true, // Enable deletion. | ||
configurable: true, | ||
value: this._computeParent() | ||
}); | ||
return this.parent; | ||
} | ||
}, | ||
scope: { | ||
get: function () { | ||
Object.defineProperty(this, "scope", { | ||
configurable: true, // Enable deletion. | ||
configurable: true, | ||
value: this._computeScope() | ||
}); | ||
return this.scope; | ||
@@ -64,3 +64,2 @@ } | ||
}); | ||
NPp.replace = function () { | ||
@@ -72,11 +71,7 @@ delete this.node; | ||
}; | ||
NPp.prune = function () { | ||
var remainingNodePath = this.parent; | ||
this.replace(); | ||
return cleanUpNodesAfterPrune(remainingNodePath); | ||
}; | ||
// The value of the first ancestor Path whose value is a Node. | ||
@@ -88,7 +83,5 @@ NPp._computeNode = function () { | ||
} | ||
var pp = this.parentPath; | ||
return pp && pp.node || null; | ||
}; | ||
// The first ancestor Path whose value is a Node distinct from this.node. | ||
@@ -98,3 +91,2 @@ NPp._computeParent = function () { | ||
var pp = this.parentPath; | ||
if (!n.Node.check(value)) { | ||
@@ -104,3 +96,2 @@ while (pp && !n.Node.check(pp.value)) { | ||
} | ||
if (pp) { | ||
@@ -110,10 +101,7 @@ pp = pp.parentPath; | ||
} | ||
while (pp && !n.Node.check(pp.value)) { | ||
pp = pp.parentPath; | ||
} | ||
return pp || null; | ||
}; | ||
// The closest enclosing scope that governs this node. | ||
@@ -124,15 +112,11 @@ NPp._computeScope = function () { | ||
var scope = pp && pp.scope; | ||
if (n.Node.check(value) && | ||
Scope.isEstablishedBy(value)) { | ||
Scope.isEstablishedBy(value)) { | ||
scope = new Scope(this, scope); | ||
} | ||
return scope || null; | ||
}; | ||
NPp.getValueProperty = function (name) { | ||
return types.getFieldValue(this.value, name); | ||
}; | ||
/** | ||
@@ -156,5 +140,3 @@ * Determine whether this.node needs to be wrapped in parentheses in order | ||
} | ||
var node = this.value; | ||
// Only expressions need parentheses. | ||
@@ -164,3 +146,2 @@ if (!n.Expression.check(node)) { | ||
} | ||
// Identifiers never need parentheses. | ||
@@ -170,3 +151,2 @@ if (node.type === "Identifier") { | ||
} | ||
while (!n.Node.check(pp.value)) { | ||
@@ -178,5 +158,3 @@ pp = pp.parentPath; | ||
} | ||
var parent = pp.value; | ||
switch (node.type) { | ||
@@ -187,5 +165,4 @@ case "UnaryExpression": | ||
return parent.type === "MemberExpression" | ||
&& this.name === "object" | ||
&& parent.object === node; | ||
&& this.name === "object" | ||
&& parent.object === node; | ||
case "BinaryExpression": | ||
@@ -196,4 +173,3 @@ case "LogicalExpression": | ||
return this.name === "callee" | ||
&& parent.callee === node; | ||
&& parent.callee === node; | ||
case "UnaryExpression": | ||
@@ -203,7 +179,5 @@ case "SpreadElement": | ||
return true; | ||
case "MemberExpression": | ||
return this.name === "object" | ||
&& parent.object === node; | ||
&& parent.object === node; | ||
case "BinaryExpression": | ||
@@ -215,7 +189,5 @@ case "LogicalExpression": | ||
var np = PRECEDENCE[no]; | ||
if (pp > np) { | ||
return true; | ||
} | ||
if (pp === np && this.name === "right") { | ||
@@ -227,7 +199,5 @@ if (parent.right !== node) { | ||
} | ||
default: | ||
return false; | ||
} | ||
case "SequenceExpression": | ||
@@ -241,6 +211,4 @@ switch (parent.type) { | ||
return false; | ||
case "ExpressionStatement": | ||
return this.name !== "expression"; | ||
default: | ||
@@ -251,3 +219,2 @@ // Otherwise err on the side of overparenthesization, adding | ||
} | ||
case "YieldExpression": | ||
@@ -266,13 +233,10 @@ switch (parent.type) { | ||
return true; | ||
default: | ||
return false; | ||
} | ||
case "Literal": | ||
return parent.type === "MemberExpression" | ||
&& isNumber.check(node.value) | ||
&& this.name === "object" | ||
&& parent.object === node; | ||
&& isNumber.check(node.value) | ||
&& this.name === "object" | ||
&& parent.object === node; | ||
case "AssignmentExpression": | ||
@@ -287,48 +251,39 @@ case "ConditionalExpression": | ||
return true; | ||
case "CallExpression": | ||
return this.name === "callee" | ||
&& parent.callee === node; | ||
&& parent.callee === node; | ||
case "ConditionalExpression": | ||
return this.name === "test" | ||
&& parent.test === node; | ||
&& parent.test === node; | ||
case "MemberExpression": | ||
return this.name === "object" | ||
&& parent.object === node; | ||
&& parent.object === node; | ||
default: | ||
return false; | ||
} | ||
default: | ||
if (parent.type === "NewExpression" && | ||
this.name === "callee" && | ||
parent.callee === node) { | ||
this.name === "callee" && | ||
parent.callee === node) { | ||
return containsCallExpression(node); | ||
} | ||
} | ||
if (assumeExpressionContext !== true && | ||
!this.canBeFirstInStatement() && | ||
this.firstInStatement()) | ||
!this.canBeFirstInStatement() && | ||
this.firstInStatement()) | ||
return true; | ||
return false; | ||
}; | ||
function isBinary(node) { | ||
return n.BinaryExpression.check(node) | ||
|| n.LogicalExpression.check(node); | ||
|| n.LogicalExpression.check(node); | ||
} | ||
// @ts-ignore 'isUnaryLike' is declared but its value is never read. [6133] | ||
function isUnaryLike(node) { | ||
return n.UnaryExpression.check(node) | ||
// I considered making SpreadElement and SpreadProperty subtypes | ||
// of UnaryExpression, but they're not really Expression nodes. | ||
|| (n.SpreadElement && n.SpreadElement.check(node)) | ||
|| (n.SpreadProperty && n.SpreadProperty.check(node)); | ||
// I considered making SpreadElement and SpreadProperty subtypes | ||
// of UnaryExpression, but they're not really Expression nodes. | ||
|| (n.SpreadElement && n.SpreadElement.check(node)) | ||
|| (n.SpreadProperty && n.SpreadProperty.check(node)); | ||
} | ||
var PRECEDENCE = {}; | ||
@@ -350,3 +305,2 @@ [["||"], | ||
}); | ||
function containsCallExpression(node) { | ||
@@ -356,26 +310,20 @@ if (n.CallExpression.check(node)) { | ||
} | ||
if (isArray.check(node)) { | ||
return node.some(containsCallExpression); | ||
} | ||
if (n.Node.check(node)) { | ||
return types.someField(node, function (name, child) { | ||
return types.someField(node, function (_name, child) { | ||
return containsCallExpression(child); | ||
}); | ||
} | ||
return false; | ||
} | ||
NPp.canBeFirstInStatement = function () { | ||
var node = this.node; | ||
return !n.FunctionExpression.check(node) | ||
&& !n.ObjectExpression.check(node); | ||
&& !n.ObjectExpression.check(node); | ||
}; | ||
NPp.firstInStatement = function () { | ||
return firstInStatement(this); | ||
}; | ||
function firstInStatement(path) { | ||
@@ -385,6 +333,5 @@ for (var node, parent; path.parent; path = path.parent) { | ||
parent = path.parent.node; | ||
if (n.BlockStatement.check(parent) && | ||
path.parent.name === "body" && | ||
path.name === 0) { | ||
path.parent.name === "body" && | ||
path.name === 0) { | ||
if (parent.body[0] !== node) { | ||
@@ -395,5 +342,4 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.ExpressionStatement.check(parent) && | ||
path.name === "expression") { | ||
path.name === "expression") { | ||
if (parent.expression !== node) { | ||
@@ -404,6 +350,5 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.SequenceExpression.check(parent) && | ||
path.parent.name === "expressions" && | ||
path.name === 0) { | ||
path.parent.name === "expressions" && | ||
path.name === 0) { | ||
if (parent.expressions[0] !== node) { | ||
@@ -414,5 +359,4 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.CallExpression.check(parent) && | ||
path.name === "callee") { | ||
path.name === "callee") { | ||
if (parent.callee !== node) { | ||
@@ -423,5 +367,4 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.MemberExpression.check(parent) && | ||
path.name === "object") { | ||
path.name === "object") { | ||
if (parent.object !== node) { | ||
@@ -432,5 +375,4 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.ConditionalExpression.check(parent) && | ||
path.name === "test") { | ||
path.name === "test") { | ||
if (parent.test !== node) { | ||
@@ -441,5 +383,4 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (isBinary(parent) && | ||
path.name === "left") { | ||
path.name === "left") { | ||
if (parent.left !== node) { | ||
@@ -450,6 +391,5 @@ throw new Error("Nodes must be equal"); | ||
} | ||
if (n.UnaryExpression.check(parent) && | ||
!parent.prefix && | ||
path.name === "argument") { | ||
!parent.prefix && | ||
path.name === "argument") { | ||
if (parent.argument !== node) { | ||
@@ -460,9 +400,6 @@ throw new Error("Nodes must be equal"); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
@@ -477,13 +414,13 @@ * Pruning certain nodes will result in empty or incomplete nodes, here we clean those nodes up. | ||
} | ||
} else if (n.ExpressionStatement.check(remainingNodePath.node)) { | ||
} | ||
else if (n.ExpressionStatement.check(remainingNodePath.node)) { | ||
if (!remainingNodePath.get('expression').value) { | ||
return remainingNodePath.prune(); | ||
} | ||
} else if (n.IfStatement.check(remainingNodePath.node)) { | ||
} | ||
else if (n.IfStatement.check(remainingNodePath.node)) { | ||
cleanUpIfStatementAfterPrune(remainingNodePath); | ||
} | ||
return remainingNodePath; | ||
} | ||
function cleanUpIfStatementAfterPrune(ifStatement) { | ||
@@ -493,14 +430,11 @@ var testExpression = ifStatement.get('test').value; | ||
var consequent = ifStatement.get('consequent').value; | ||
if (!consequent && !alternate) { | ||
var testExpressionStatement = b.expressionStatement(testExpression); | ||
ifStatement.replace(testExpressionStatement); | ||
} else if (!consequent && alternate) { | ||
} | ||
else if (!consequent && alternate) { | ||
var negatedTestExpression = b.unaryExpression('!', testExpression, true); | ||
if (n.UnaryExpression.check(testExpression) && testExpression.operator === '!') { | ||
negatedTestExpression = testExpression.argument; | ||
} | ||
ifStatement.get("test").replace(negatedTestExpression); | ||
@@ -511,4 +445,5 @@ ifStatement.get("consequent").replace(alternate); | ||
} | ||
return NodePath; | ||
}; | ||
} | ||
exports.default = nodePathPlugin; | ||
module.exports = exports["default"]; |
@@ -0,7 +1,12 @@ | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
var node_path_1 = __importDefault(require("./node-path")); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
module.exports = function (fork) { | ||
var types = fork.use(require("./types")); | ||
var NodePath = fork.use(require("./node-path")); | ||
var Printable = types.namedTypes.Printable; | ||
function pathVisitorPlugin(fork) { | ||
var types = fork.use(types_1.default); | ||
var NodePath = fork.use(node_path_1.default); | ||
var isArray = types.builtInTypes.array; | ||
@@ -11,28 +16,19 @@ var isObject = types.builtInTypes.object; | ||
var undefined; | ||
function PathVisitor() { | ||
var PathVisitor = function PathVisitor() { | ||
if (!(this instanceof PathVisitor)) { | ||
throw new Error( | ||
"PathVisitor constructor cannot be invoked without 'new'" | ||
); | ||
throw new Error("PathVisitor constructor cannot be invoked without 'new'"); | ||
} | ||
// Permanent state. | ||
this._reusableContextStack = []; | ||
this._methodNameTable = computeMethodNameTable(this); | ||
this._shouldVisitComments = | ||
hasOwn.call(this._methodNameTable, "Block") || | ||
hasOwn.call(this._methodNameTable, "Line"); | ||
hasOwn.call(this._methodNameTable, "Block") || | ||
hasOwn.call(this._methodNameTable, "Line"); | ||
this.Context = makeContextConstructor(this); | ||
// State reset every time PathVisitor.prototype.visit is called. | ||
this._visiting = false; | ||
this._changeReported = false; | ||
} | ||
}; | ||
function computeMethodNameTable(visitor) { | ||
var typeNames = Object.create(null); | ||
for (var methodName in visitor) { | ||
@@ -43,10 +39,8 @@ if (/^visit[A-Z]/.test(methodName)) { | ||
} | ||
var supertypeTable = types.computeSupertypeLookupTable(typeNames); | ||
var methodNameTable = Object.create(null); | ||
var typeNames = Object.keys(supertypeTable); | ||
var typeNameCount = typeNames.length; | ||
var typeNameKeys = Object.keys(supertypeTable); | ||
var typeNameCount = typeNameKeys.length; | ||
for (var i = 0; i < typeNameCount; ++i) { | ||
var typeName = typeNames[i]; | ||
var typeName = typeNameKeys[i]; | ||
methodName = "visit" + supertypeTable[typeName]; | ||
@@ -57,6 +51,4 @@ if (isFunction.check(visitor[methodName])) { | ||
} | ||
return methodNameTable; | ||
} | ||
PathVisitor.fromMethodsObject = function fromMethodsObject(methods) { | ||
@@ -66,3 +58,2 @@ if (methods instanceof PathVisitor) { | ||
} | ||
if (!isObject.check(methods)) { | ||
@@ -72,24 +63,16 @@ // An empty visitor? | ||
} | ||
function Visitor() { | ||
var Visitor = function Visitor() { | ||
if (!(this instanceof Visitor)) { | ||
throw new Error( | ||
"Visitor constructor cannot be invoked without 'new'" | ||
); | ||
throw new Error("Visitor constructor cannot be invoked without 'new'"); | ||
} | ||
PathVisitor.call(this); | ||
} | ||
}; | ||
var Vp = Visitor.prototype = Object.create(PVp); | ||
Vp.constructor = Visitor; | ||
extend(Vp, methods); | ||
extend(Visitor, PathVisitor); | ||
isFunction.assert(Visitor.fromMethodsObject); | ||
isFunction.assert(Visitor.visit); | ||
return new Visitor; | ||
}; | ||
function extend(target, source) { | ||
@@ -101,20 +84,13 @@ for (var property in source) { | ||
} | ||
return target; | ||
} | ||
PathVisitor.visit = function visit(node, methods) { | ||
return PathVisitor.fromMethodsObject(methods).visit(node); | ||
}; | ||
var PVp = PathVisitor.prototype; | ||
PVp.visit = function () { | ||
if (this._visiting) { | ||
throw new Error( | ||
"Recursively calling visitor.visit(path) resets visitor state. " + | ||
"Try this.visit(path) or this.traverse(path) instead." | ||
); | ||
throw new Error("Recursively calling visitor.visit(path) resets visitor state. " + | ||
"Try this.visit(path) or this.traverse(path) instead."); | ||
} | ||
// Private state that needs to be reset before every traversal. | ||
@@ -124,22 +100,19 @@ this._visiting = true; | ||
this._abortRequested = false; | ||
var argc = arguments.length; | ||
var args = new Array(argc) | ||
var args = new Array(argc); | ||
for (var i = 0; i < argc; ++i) { | ||
args[i] = arguments[i]; | ||
} | ||
if (!(args[0] instanceof NodePath)) { | ||
args[0] = new NodePath({root: args[0]}).get("root"); | ||
args[0] = new NodePath({ root: args[0] }).get("root"); | ||
} | ||
// Called with the same arguments as .visit. | ||
this.reset.apply(this, args); | ||
var didNotThrow; | ||
try { | ||
var root = this.visitWithoutReset(args[0]); | ||
var didNotThrow = true; | ||
} finally { | ||
didNotThrow = true; | ||
} | ||
finally { | ||
this._visiting = false; | ||
if (!didNotThrow && this._abortRequested) { | ||
@@ -157,7 +130,5 @@ // If this.visitWithoutReset threw an exception and | ||
} | ||
return root; | ||
}; | ||
PVp.AbortRequest = function AbortRequest() {}; | ||
PVp.AbortRequest = function AbortRequest() { }; | ||
PVp.abort = function () { | ||
@@ -167,3 +138,2 @@ var visitor = this; | ||
var request = new visitor.AbortRequest(); | ||
// If you decide to catch this exception and stop it from propagating, | ||
@@ -175,10 +145,7 @@ // make sure to call its cancel method to avoid silencing other | ||
}; | ||
throw request; | ||
}; | ||
PVp.reset = function (path/*, additional arguments */) { | ||
PVp.reset = function (_path /*, additional arguments */) { | ||
// Empty stub; may be reassigned or overridden by subclasses. | ||
}; | ||
PVp.visitWithoutReset = function (path) { | ||
@@ -191,14 +158,10 @@ if (this instanceof this.Context) { | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
var value = path.value; | ||
var methodName = value && | ||
typeof value === "object" && | ||
typeof value.type === "string" && | ||
this._methodNameTable[value.type]; | ||
typeof value === "object" && | ||
typeof value.type === "string" && | ||
this._methodNameTable[value.type]; | ||
if (methodName) { | ||
@@ -208,7 +171,8 @@ var context = this.acquireContext(path); | ||
return context.invokeVisitorMethod(methodName); | ||
} finally { | ||
} | ||
finally { | ||
this.releaseContext(context); | ||
} | ||
} else { | ||
} | ||
else { | ||
// If there was no visitor method to call, visit the children of | ||
@@ -219,3 +183,2 @@ // this node generically. | ||
}; | ||
function visitChildren(path, visitor) { | ||
@@ -228,12 +191,11 @@ if (!(path instanceof NodePath)) { | ||
} | ||
var value = path.value; | ||
if (isArray.check(value)) { | ||
path.each(visitor.visitWithoutReset, visitor); | ||
} else if (!isObject.check(value)) { | ||
} | ||
else if (!isObject.check(value)) { | ||
// No children to visit. | ||
} else { | ||
} | ||
else { | ||
var childNames = types.getFieldNames(value); | ||
// The .comments field of the Node type is hidden, so we only | ||
@@ -243,10 +205,8 @@ // visit it if the visitor defines visitBlock or visitLine, and | ||
if (visitor._shouldVisitComments && | ||
value.comments && | ||
childNames.indexOf("comments") < 0) { | ||
value.comments && | ||
childNames.indexOf("comments") < 0) { | ||
childNames.push("comments"); | ||
} | ||
var childCount = childNames.length; | ||
var childPaths = []; | ||
for (var i = 0; i < childCount; ++i) { | ||
@@ -259,3 +219,2 @@ var childName = childNames[i]; | ||
} | ||
for (var i = 0; i < childCount; ++i) { | ||
@@ -265,6 +224,4 @@ visitor.visitWithoutReset(childPaths[i]); | ||
} | ||
return path.value; | ||
} | ||
PVp.acquireContext = function (path) { | ||
@@ -276,3 +233,2 @@ if (this._reusableContextStack.length === 0) { | ||
}; | ||
PVp.releaseContext = function (context) { | ||
@@ -285,11 +241,8 @@ if (!(context instanceof this.Context)) { | ||
}; | ||
PVp.reportChanged = function () { | ||
this._changeReported = true; | ||
}; | ||
PVp.wasChangeReported = function () { | ||
return this._changeReported; | ||
}; | ||
function makeContextConstructor(visitor) { | ||
@@ -306,3 +259,2 @@ function Context(path) { | ||
} | ||
Object.defineProperty(this, "visitor", { | ||
@@ -314,124 +266,94 @@ value: visitor, | ||
}); | ||
this.currentPath = path; | ||
this.needToCallTraverse = true; | ||
Object.seal(this); | ||
} | ||
if (!(visitor instanceof PathVisitor)) { | ||
throw new Error(""); | ||
} | ||
// Note that the visitor object is the prototype of Context.prototype, | ||
// so all visitor methods are inherited by context objects. | ||
var Cp = Context.prototype = Object.create(visitor); | ||
Cp.constructor = Context; | ||
extend(Cp, sharedContextProtoMethods); | ||
return Context; | ||
} | ||
// Every PathVisitor has a different this.Context constructor and | ||
// this.Context.prototype object, but those prototypes can all use the | ||
// same reset, invokeVisitorMethod, and traverse function objects. | ||
// Every PathVisitor has a different this.Context constructor and | ||
// this.Context.prototype object, but those prototypes can all use the | ||
// same reset, invokeVisitorMethod, and traverse function objects. | ||
var sharedContextProtoMethods = Object.create(null); | ||
sharedContextProtoMethods.reset = | ||
function reset(path) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.currentPath = path; | ||
this.needToCallTraverse = true; | ||
return this; | ||
}; | ||
function reset(path) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.currentPath = path; | ||
this.needToCallTraverse = true; | ||
return this; | ||
}; | ||
sharedContextProtoMethods.invokeVisitorMethod = | ||
function invokeVisitorMethod(methodName) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
var result = this.visitor[methodName].call(this, this.currentPath); | ||
if (result === false) { | ||
// Visitor methods return false to indicate that they have handled | ||
// their own traversal needs, and we should not complain if | ||
// this.needToCallTraverse is still true. | ||
this.needToCallTraverse = false; | ||
} else if (result !== undefined) { | ||
// Any other non-undefined value returned from the visitor method | ||
// is interpreted as a replacement value. | ||
this.currentPath = this.currentPath.replace(result)[0]; | ||
if (this.needToCallTraverse) { | ||
// If this.traverse still hasn't been called, visit the | ||
// children of the replacement node. | ||
this.traverse(this.currentPath); | ||
} | ||
} | ||
if (this.needToCallTraverse !== false) { | ||
throw new Error( | ||
"Must either call this.traverse or return false in " + methodName | ||
); | ||
} | ||
var path = this.currentPath; | ||
return path && path.value; | ||
}; | ||
function invokeVisitorMethod(methodName) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
var result = this.visitor[methodName].call(this, this.currentPath); | ||
if (result === false) { | ||
// Visitor methods return false to indicate that they have handled | ||
// their own traversal needs, and we should not complain if | ||
// this.needToCallTraverse is still true. | ||
this.needToCallTraverse = false; | ||
} | ||
else if (result !== undefined) { | ||
// Any other non-undefined value returned from the visitor method | ||
// is interpreted as a replacement value. | ||
this.currentPath = this.currentPath.replace(result)[0]; | ||
if (this.needToCallTraverse) { | ||
// If this.traverse still hasn't been called, visit the | ||
// children of the replacement node. | ||
this.traverse(this.currentPath); | ||
} | ||
} | ||
if (this.needToCallTraverse !== false) { | ||
throw new Error("Must either call this.traverse or return false in " + methodName); | ||
} | ||
var path = this.currentPath; | ||
return path && path.value; | ||
}; | ||
sharedContextProtoMethods.traverse = | ||
function traverse(path, newVisitor) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.needToCallTraverse = false; | ||
return visitChildren(path, PathVisitor.fromMethodsObject( | ||
newVisitor || this.visitor | ||
)); | ||
}; | ||
function traverse(path, newVisitor) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.needToCallTraverse = false; | ||
return visitChildren(path, PathVisitor.fromMethodsObject(newVisitor || this.visitor)); | ||
}; | ||
sharedContextProtoMethods.visit = | ||
function visit(path, newVisitor) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.needToCallTraverse = false; | ||
return PathVisitor.fromMethodsObject( | ||
newVisitor || this.visitor | ||
).visitWithoutReset(path); | ||
}; | ||
function visit(path, newVisitor) { | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
this.needToCallTraverse = false; | ||
return PathVisitor.fromMethodsObject(newVisitor || this.visitor).visitWithoutReset(path); | ||
}; | ||
sharedContextProtoMethods.reportChanged = function reportChanged() { | ||
this.visitor.reportChanged(); | ||
}; | ||
sharedContextProtoMethods.abort = function abort() { | ||
@@ -441,4 +363,5 @@ this.needToCallTraverse = false; | ||
}; | ||
return PathVisitor; | ||
}; | ||
} | ||
exports.default = pathVisitorPlugin; | ||
module.exports = exports["default"]; |
178
lib/path.js
@@ -1,17 +0,17 @@ | ||
var Ap = Array.prototype; | ||
var slice = Ap.slice; | ||
var map = Ap.map; | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
var Op = Object.prototype; | ||
var hasOwn = Op.hasOwnProperty; | ||
module.exports = function (fork) { | ||
var types = fork.use(require("./types")); | ||
function pathPlugin(fork) { | ||
var types = fork.use(types_1.default); | ||
var isArray = types.builtInTypes.array; | ||
var isNumber = types.builtInTypes.number; | ||
function Path(value, parentPath, name) { | ||
var Path = function Path(value, parentPath, name) { | ||
if (!(this instanceof Path)) { | ||
throw new Error("Path constructor cannot be invoked without 'new'"); | ||
} | ||
if (parentPath) { | ||
@@ -21,25 +21,20 @@ if (!(parentPath instanceof Path)) { | ||
} | ||
} else { | ||
} | ||
else { | ||
parentPath = null; | ||
name = null; | ||
} | ||
// The value encapsulated by this Path, generally equal to | ||
// parentPath.value[name] if we have a parentPath. | ||
this.value = value; | ||
// The immediate parent Path of this Path. | ||
this.parentPath = parentPath; | ||
// The name of the property of parentPath.value through which this | ||
// Path's value was reached. | ||
this.name = name; | ||
// Calling path.get("child") multiple times always returns the same | ||
// child Path object, for both performance and consistency reasons. | ||
this.__childCache = null; | ||
} | ||
}; | ||
var Pp = Path.prototype; | ||
function getChildCache(path) { | ||
@@ -50,3 +45,2 @@ // Lazily create the child cache. This also cheapens cache | ||
} | ||
function getChildPath(path, name) { | ||
@@ -57,29 +51,25 @@ var cache = getChildCache(path); | ||
if (!hasOwn.call(cache, name) || | ||
// Ensure consistency between cache and reality. | ||
childPath.value !== actualChildValue) { | ||
childPath = cache[name] = new path.constructor( | ||
actualChildValue, path, name | ||
); | ||
// Ensure consistency between cache and reality. | ||
childPath.value !== actualChildValue) { | ||
childPath = cache[name] = new path.constructor(actualChildValue, path, name); | ||
} | ||
return childPath; | ||
} | ||
// This method is designed to be overridden by subclasses that need to | ||
// handle missing properties, etc. | ||
// This method is designed to be overridden by subclasses that need to | ||
// handle missing properties, etc. | ||
Pp.getValueProperty = function getValueProperty(name) { | ||
return this.value[name]; | ||
}; | ||
Pp.get = function get(name) { | ||
Pp.get = function get() { | ||
var names = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
names[_i] = arguments[_i]; | ||
} | ||
var path = this; | ||
var names = arguments; | ||
var count = names.length; | ||
for (var i = 0; i < count; ++i) { | ||
path = getChildPath(path, names[i]); | ||
} | ||
return path; | ||
}; | ||
Pp.each = function each(callback, context) { | ||
@@ -89,3 +79,2 @@ var childPaths = []; | ||
var i = 0; | ||
// Collect all the original child paths before invoking the callback. | ||
@@ -97,3 +86,2 @@ for (var i = 0; i < len; ++i) { | ||
} | ||
// Invoke the callback on just the original child paths, regardless of | ||
@@ -110,16 +98,11 @@ // any modifications made to the array by the callback. I chose these | ||
}; | ||
Pp.map = function map(callback, context) { | ||
var result = []; | ||
this.each(function (childPath) { | ||
result.push(callback.call(this, childPath)); | ||
}, context); | ||
return result; | ||
}; | ||
Pp.filter = function filter(callback, context) { | ||
var result = []; | ||
this.each(function (childPath) { | ||
@@ -130,14 +113,10 @@ if (callback.call(this, childPath)) { | ||
}, context); | ||
return result; | ||
}; | ||
function emptyMoves() {} | ||
function emptyMoves() { } | ||
function getMoves(path, offset, start, end) { | ||
isArray.assert(path.value); | ||
if (offset === 0) { | ||
return emptyMoves; | ||
} | ||
var length = path.value.length; | ||
@@ -147,3 +126,2 @@ if (length < 1) { | ||
} | ||
var argc = arguments.length; | ||
@@ -153,16 +131,15 @@ if (argc === 2) { | ||
end = length; | ||
} else if (argc === 3) { | ||
} | ||
else if (argc === 3) { | ||
start = Math.max(start, 0); | ||
end = length; | ||
} else { | ||
} | ||
else { | ||
start = Math.max(start, 0); | ||
end = Math.min(end, length); | ||
} | ||
isNumber.assert(start); | ||
isNumber.assert(end); | ||
var moves = Object.create(null); | ||
var cache = getChildCache(path); | ||
for (var i = start; i < end; ++i) { | ||
@@ -180,5 +157,3 @@ if (hasOwn.call(path.value, i)) { | ||
} | ||
delete cache.length; | ||
return function () { | ||
@@ -195,3 +170,2 @@ for (var newIndex in moves) { | ||
} | ||
Pp.shift = function shift() { | ||
@@ -203,16 +177,21 @@ var move = getMoves(this, -1); | ||
}; | ||
Pp.unshift = function unshift(node) { | ||
var move = getMoves(this, arguments.length); | ||
var result = this.value.unshift.apply(this.value, arguments); | ||
Pp.unshift = function unshift() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var move = getMoves(this, args.length); | ||
var result = this.value.unshift.apply(this.value, args); | ||
move(); | ||
return result; | ||
}; | ||
Pp.push = function push(node) { | ||
Pp.push = function push() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
isArray.assert(this.value); | ||
delete getChildCache(this).length | ||
return this.value.push.apply(this.value, arguments); | ||
delete getChildCache(this).length; | ||
return this.value.push.apply(this.value, args); | ||
}; | ||
Pp.pop = function pop() { | ||
@@ -225,4 +204,3 @@ isArray.assert(this.value); | ||
}; | ||
Pp.insertAt = function insertAt(index, node) { | ||
Pp.insertAt = function insertAt(index) { | ||
var argc = arguments.length; | ||
@@ -233,34 +211,35 @@ var move = getMoves(this, argc - 1, index); | ||
} | ||
index = Math.max(index, 0); | ||
for (var i = 1; i < argc; ++i) { | ||
this.value[index + i - 1] = arguments[i]; | ||
} | ||
move(); | ||
return this; | ||
}; | ||
Pp.insertBefore = function insertBefore(node) { | ||
Pp.insertBefore = function insertBefore() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var pp = this.parentPath; | ||
var argc = arguments.length; | ||
var argc = args.length; | ||
var insertAtArgs = [this.name]; | ||
for (var i = 0; i < argc; ++i) { | ||
insertAtArgs.push(arguments[i]); | ||
insertAtArgs.push(args[i]); | ||
} | ||
return pp.insertAt.apply(pp, insertAtArgs); | ||
}; | ||
Pp.insertAfter = function insertAfter(node) { | ||
Pp.insertAfter = function insertAfter() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var pp = this.parentPath; | ||
var argc = arguments.length; | ||
var argc = args.length; | ||
var insertAtArgs = [this.name + 1]; | ||
for (var i = 0; i < argc; ++i) { | ||
insertAtArgs.push(arguments[i]); | ||
insertAtArgs.push(args[i]); | ||
} | ||
return pp.insertAt.apply(pp, insertAtArgs); | ||
}; | ||
function repairRelationshipWithParent(path) { | ||
@@ -270,3 +249,2 @@ if (!(path instanceof Path)) { | ||
} | ||
var pp = path.parentPath; | ||
@@ -277,10 +255,9 @@ if (!pp) { | ||
} | ||
var parentValue = pp.value; | ||
var parentCache = getChildCache(pp); | ||
// Make sure parentCache[path.name] is populated. | ||
if (parentValue[path.name] === path.value) { | ||
parentCache[path.name] = path; | ||
} else if (isArray.check(parentValue)) { | ||
} | ||
else if (isArray.check(parentValue)) { | ||
// Something caused path.name to become out of date, so attempt to | ||
@@ -292,3 +269,4 @@ // recover by searching for path.value in parentValue. | ||
} | ||
} else { | ||
} | ||
else { | ||
// If path.value disagrees with parentValue[path.name], and | ||
@@ -300,3 +278,2 @@ // path.name is not an array index, let path.value become the new | ||
} | ||
if (parentValue[path.name] !== path.value) { | ||
@@ -308,6 +285,4 @@ throw new Error(""); | ||
} | ||
return path; | ||
} | ||
Pp.replace = function replace(replacement) { | ||
@@ -318,9 +293,6 @@ var results = []; | ||
var count = arguments.length; | ||
repairRelationshipWithParent(this); | ||
if (isArray.check(parentValue)) { | ||
var originalLength = parentValue.length; | ||
var move = getMoves(this.parentPath, count - 1, this.name + 1); | ||
var spliceArgs = [this.name, 1]; | ||
@@ -330,5 +302,3 @@ for (var i = 0; i < count; ++i) { | ||
} | ||
var splicedOut = parentValue.splice.apply(parentValue, spliceArgs); | ||
if (splicedOut[0] !== this.value) { | ||
@@ -340,5 +310,3 @@ throw new Error(""); | ||
} | ||
move(); | ||
if (count === 0) { | ||
@@ -348,8 +316,7 @@ delete this.value; | ||
this.__childCache = null; | ||
} else { | ||
} | ||
else { | ||
if (parentValue[this.name] !== replacement) { | ||
throw new Error(""); | ||
} | ||
if (this.value !== replacement) { | ||
@@ -359,7 +326,5 @@ this.value = replacement; | ||
} | ||
for (i = 0; i < count; ++i) { | ||
results.push(this.parentPath.get(this.name + i)); | ||
} | ||
if (results[0] !== this) { | ||
@@ -369,4 +334,4 @@ throw new Error(""); | ||
} | ||
} else if (count === 1) { | ||
} | ||
else if (count === 1) { | ||
if (this.value !== replacement) { | ||
@@ -377,19 +342,18 @@ this.__childCache = null; | ||
results.push(this); | ||
} else if (count === 0) { | ||
} | ||
else if (count === 0) { | ||
delete parentValue[this.name]; | ||
delete this.value; | ||
this.__childCache = null; | ||
// Leave this path cached as parentCache[this.name], even though | ||
// it no longer has a value defined. | ||
} else { | ||
} | ||
else { | ||
throw new Error("Could not replace path"); | ||
} | ||
return results; | ||
}; | ||
return Path; | ||
}; | ||
} | ||
exports.default = pathPlugin; | ||
module.exports = exports["default"]; |
243
lib/scope.js
@@ -0,5 +1,10 @@ | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
module.exports = function (fork) { | ||
var types = fork.use(require("./types")); | ||
function scopePlugin(fork) { | ||
var types = fork.use(types_1.default); | ||
var Type = types.Type; | ||
@@ -11,14 +16,8 @@ var namedTypes = types.namedTypes; | ||
var b = types.builders; | ||
function Scope(path, parentScope) { | ||
var Scope = function Scope(path, parentScope) { | ||
if (!(this instanceof Scope)) { | ||
throw new Error("Scope constructor cannot be invoked without 'new'"); | ||
} | ||
if (!(path instanceof fork.use(require("./node-path")))) { | ||
throw new Error(""); | ||
} | ||
ScopeType.assert(path.value); | ||
var depth; | ||
if (parentScope) { | ||
@@ -29,7 +28,7 @@ if (!(parentScope instanceof Scope)) { | ||
depth = parentScope.depth + 1; | ||
} else { | ||
} | ||
else { | ||
parentScope = null; | ||
depth = 0; | ||
} | ||
Object.defineProperties(this, { | ||
@@ -44,12 +43,9 @@ path: { value: path }, | ||
}); | ||
} | ||
}; | ||
var scopeTypes = [ | ||
// Program nodes introduce global scopes. | ||
namedTypes.Program, | ||
// Function is the supertype of FunctionExpression, | ||
// FunctionDeclaration, ArrowExpression, etc. | ||
namedTypes.Function, | ||
// In case you didn't know, the caught parameter shadows any variable | ||
@@ -59,25 +55,18 @@ // of the same name in an outer scope. | ||
]; | ||
var ScopeType = Type.or.apply(Type, scopeTypes); | ||
Scope.isEstablishedBy = function(node) { | ||
Scope.isEstablishedBy = function (node) { | ||
return ScopeType.check(node); | ||
}; | ||
var Sp = Scope.prototype; | ||
// Will be overridden after an instance lazily calls scanScope. | ||
// Will be overridden after an instance lazily calls scanScope. | ||
Sp.didScan = false; | ||
Sp.declares = function(name) { | ||
Sp.declares = function (name) { | ||
this.scan(); | ||
return hasOwn.call(this.bindings, name); | ||
}; | ||
Sp.declaresType = function(name) { | ||
Sp.declaresType = function (name) { | ||
this.scan(); | ||
return hasOwn.call(this.types, name); | ||
}; | ||
Sp.declareTemporary = function(prefix) { | ||
Sp.declareTemporary = function (prefix) { | ||
if (prefix) { | ||
@@ -87,12 +76,10 @@ if (!/^[a-z$_]/i.test(prefix)) { | ||
} | ||
} else { | ||
} | ||
else { | ||
prefix = "t$"; | ||
} | ||
// Include this.depth in the name to make sure the name does not | ||
// collide with any variables in nested/enclosing scopes. | ||
prefix += this.depth.toString(36) + "$"; | ||
this.scan(); | ||
var index = 0; | ||
@@ -102,10 +89,7 @@ while (this.declares(prefix + index)) { | ||
} | ||
var name = prefix + index; | ||
return this.bindings[name] = types.builders.identifier(name); | ||
}; | ||
Sp.injectTemporary = function(identifier, init) { | ||
Sp.injectTemporary = function (identifier, init) { | ||
identifier || (identifier = this.declareTemporary()); | ||
var bodyPath = this.path.get("body"); | ||
@@ -115,14 +99,6 @@ if (namedTypes.BlockStatement.check(bodyPath.value)) { | ||
} | ||
bodyPath.unshift( | ||
b.variableDeclaration( | ||
"var", | ||
[b.variableDeclarator(identifier, init || null)] | ||
) | ||
); | ||
bodyPath.unshift(b.variableDeclaration("var", [b.variableDeclarator(identifier, init || null)])); | ||
return identifier; | ||
}; | ||
Sp.scan = function(force) { | ||
Sp.scan = function (force) { | ||
if (force || !this.didScan) { | ||
@@ -137,3 +113,2 @@ for (var name in this.bindings) { | ||
}; | ||
Sp.getBindings = function () { | ||
@@ -143,3 +118,2 @@ this.scan(); | ||
}; | ||
Sp.getTypes = function () { | ||
@@ -149,7 +123,5 @@ this.scan(); | ||
}; | ||
function scanScope(path, bindings, scopeTypes) { | ||
var node = path.value; | ||
ScopeType.assert(node); | ||
if (namedTypes.CatchClause.check(node)) { | ||
@@ -160,54 +132,48 @@ // A catch clause establishes a new scope but the only variable | ||
addPattern(path.get("param"), bindings); | ||
} else { | ||
} | ||
else { | ||
recursiveScanScope(path, bindings, scopeTypes); | ||
} | ||
} | ||
function recursiveScanScope(path, bindings, scopeTypes) { | ||
var node = path.value; | ||
if (path.parent && | ||
namedTypes.FunctionExpression.check(path.parent.node) && | ||
path.parent.node.id) { | ||
namedTypes.FunctionExpression.check(path.parent.node) && | ||
path.parent.node.id) { | ||
addPattern(path.parent.get("id"), bindings); | ||
} | ||
if (!node) { | ||
// None of the remaining cases matter if node is falsy. | ||
} else if (isArray.check(node)) { | ||
path.each(function(childPath) { | ||
} | ||
else if (isArray.check(node)) { | ||
path.each(function (childPath) { | ||
recursiveScanChild(childPath, bindings, scopeTypes); | ||
}); | ||
} else if (namedTypes.Function.check(node)) { | ||
path.get("params").each(function(paramPath) { | ||
} | ||
else if (namedTypes.Function.check(node)) { | ||
path.get("params").each(function (paramPath) { | ||
addPattern(paramPath, bindings); | ||
}); | ||
recursiveScanChild(path.get("body"), bindings, scopeTypes); | ||
} else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) { | ||
} | ||
else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) { | ||
addTypePattern(path.get("id"), scopeTypes); | ||
} else if (namedTypes.VariableDeclarator.check(node)) { | ||
} | ||
else if (namedTypes.VariableDeclarator.check(node)) { | ||
addPattern(path.get("id"), bindings); | ||
recursiveScanChild(path.get("init"), bindings, scopeTypes); | ||
} else if (node.type === "ImportSpecifier" || | ||
node.type === "ImportNamespaceSpecifier" || | ||
node.type === "ImportDefaultSpecifier") { | ||
} | ||
else if (node.type === "ImportSpecifier" || | ||
node.type === "ImportNamespaceSpecifier" || | ||
node.type === "ImportDefaultSpecifier") { | ||
addPattern( | ||
// Esprima used to use the .name field to refer to the local | ||
// binding identifier for ImportSpecifier nodes, but .id for | ||
// ImportNamespaceSpecifier and ImportDefaultSpecifier nodes. | ||
// ESTree/Acorn/ESpree use .local for all three node types. | ||
path.get(node.local ? "local" : | ||
node.name ? "name" : "id"), | ||
bindings | ||
); | ||
} else if (Node.check(node) && !Expression.check(node)) { | ||
types.eachField(node, function(name, child) { | ||
// Esprima used to use the .name field to refer to the local | ||
// binding identifier for ImportSpecifier nodes, but .id for | ||
// ImportNamespaceSpecifier and ImportDefaultSpecifier nodes. | ||
// ESTree/Acorn/ESpree use .local for all three node types. | ||
path.get(node.local ? "local" : | ||
node.name ? "name" : "id"), bindings); | ||
} | ||
else if (Node.check(node) && !Expression.check(node)) { | ||
types.eachField(node, function (name, child) { | ||
var childPath = path.get(name); | ||
@@ -221,3 +187,2 @@ if (!pathHasValue(childPath, child)) { | ||
} | ||
function pathHasValue(path, value) { | ||
@@ -227,34 +192,29 @@ if (path.value === value) { | ||
} | ||
// Empty arrays are probably produced by defaults.emptyArray, in which | ||
// case is makes sense to regard them as equivalent, if not ===. | ||
if (Array.isArray(path.value) && | ||
path.value.length === 0 && | ||
Array.isArray(value) && | ||
value.length === 0) { | ||
path.value.length === 0 && | ||
Array.isArray(value) && | ||
value.length === 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function recursiveScanChild(path, bindings, scopeTypes) { | ||
var node = path.value; | ||
if (!node || Expression.check(node)) { | ||
// Ignore falsy values and Expressions. | ||
} else if (namedTypes.FunctionDeclaration.check(node) && | ||
node.id !== null) { | ||
} | ||
else if (namedTypes.FunctionDeclaration.check(node) && | ||
node.id !== null) { | ||
addPattern(path.get("id"), bindings); | ||
} else if (namedTypes.ClassDeclaration && | ||
namedTypes.ClassDeclaration.check(node)) { | ||
} | ||
else if (namedTypes.ClassDeclaration && | ||
namedTypes.ClassDeclaration.check(node)) { | ||
addPattern(path.get("id"), bindings); | ||
} else if (ScopeType.check(node)) { | ||
} | ||
else if (ScopeType.check(node)) { | ||
if (namedTypes.CatchClause.check(node)) { | ||
var catchParamName = node.param.name; | ||
var hadBinding = hasOwn.call(bindings, catchParamName); | ||
// Any declarations that occur inside the catch body that do | ||
@@ -264,3 +224,2 @@ // not have the same name as the catch parameter should count | ||
recursiveScanScope(path.get("body"), bindings, scopeTypes); | ||
// If a new binding matching the catch parameter name was | ||
@@ -274,76 +233,75 @@ // created while scanning the catch body, ignore it because it | ||
} | ||
} else { | ||
} | ||
else { | ||
recursiveScanScope(path, bindings, scopeTypes); | ||
} | ||
} | ||
function addPattern(patternPath, bindings) { | ||
var pattern = patternPath.value; | ||
namedTypes.Pattern.assert(pattern); | ||
if (namedTypes.Identifier.check(pattern)) { | ||
if (hasOwn.call(bindings, pattern.name)) { | ||
bindings[pattern.name].push(patternPath); | ||
} else { | ||
} | ||
else { | ||
bindings[pattern.name] = [patternPath]; | ||
} | ||
} else if (namedTypes.AssignmentPattern && | ||
namedTypes.AssignmentPattern.check(pattern)) { | ||
addPattern(patternPath.get('left'), bindings); | ||
} else if (namedTypes.ObjectPattern && | ||
namedTypes.ObjectPattern.check(pattern)) { | ||
patternPath.get('properties').each(function(propertyPath) { | ||
} | ||
else if (namedTypes.AssignmentPattern && | ||
namedTypes.AssignmentPattern.check(pattern)) { | ||
addPattern(patternPath.get('left'), bindings); | ||
} | ||
else if (namedTypes.ObjectPattern && | ||
namedTypes.ObjectPattern.check(pattern)) { | ||
patternPath.get('properties').each(function (propertyPath) { | ||
var property = propertyPath.value; | ||
if (namedTypes.Pattern.check(property)) { | ||
addPattern(propertyPath, bindings); | ||
} else if (namedTypes.Property.check(property)) { | ||
} | ||
else if (namedTypes.Property.check(property)) { | ||
addPattern(propertyPath.get('value'), bindings); | ||
} else if (namedTypes.SpreadProperty && | ||
namedTypes.SpreadProperty.check(property)) { | ||
} | ||
else if (namedTypes.SpreadProperty && | ||
namedTypes.SpreadProperty.check(property)) { | ||
addPattern(propertyPath.get('argument'), bindings); | ||
} | ||
}); | ||
} else if (namedTypes.ArrayPattern && | ||
namedTypes.ArrayPattern.check(pattern)) { | ||
patternPath.get('elements').each(function(elementPath) { | ||
} | ||
else if (namedTypes.ArrayPattern && | ||
namedTypes.ArrayPattern.check(pattern)) { | ||
patternPath.get('elements').each(function (elementPath) { | ||
var element = elementPath.value; | ||
if (namedTypes.Pattern.check(element)) { | ||
addPattern(elementPath, bindings); | ||
} else if (namedTypes.SpreadElement && | ||
namedTypes.SpreadElement.check(element)) { | ||
} | ||
else if (namedTypes.SpreadElement && | ||
namedTypes.SpreadElement.check(element)) { | ||
addPattern(elementPath.get("argument"), bindings); | ||
} | ||
}); | ||
} else if (namedTypes.PropertyPattern && | ||
namedTypes.PropertyPattern.check(pattern)) { | ||
} | ||
else if (namedTypes.PropertyPattern && | ||
namedTypes.PropertyPattern.check(pattern)) { | ||
addPattern(patternPath.get('pattern'), bindings); | ||
} else if ((namedTypes.SpreadElementPattern && | ||
namedTypes.SpreadElementPattern.check(pattern)) || | ||
(namedTypes.SpreadPropertyPattern && | ||
namedTypes.SpreadPropertyPattern.check(pattern))) { | ||
} | ||
else if ((namedTypes.SpreadElementPattern && | ||
namedTypes.SpreadElementPattern.check(pattern)) || | ||
(namedTypes.SpreadPropertyPattern && | ||
namedTypes.SpreadPropertyPattern.check(pattern))) { | ||
addPattern(patternPath.get('argument'), bindings); | ||
} | ||
} | ||
function addTypePattern(patternPath, types) { | ||
var pattern = patternPath.value; | ||
namedTypes.Pattern.assert(pattern); | ||
if (namedTypes.Identifier.check(pattern)) { | ||
if (hasOwn.call(types, pattern.name)) { | ||
types[pattern.name].push(patternPath); | ||
} else { | ||
} | ||
else { | ||
types[pattern.name] = [patternPath]; | ||
} | ||
} | ||
} | ||
Sp.lookup = function(name) { | ||
Sp.lookup = function (name) { | ||
for (var scope = this; scope; scope = scope.parent) | ||
@@ -354,4 +312,3 @@ if (scope.declares(name)) | ||
}; | ||
Sp.lookupType = function(name) { | ||
Sp.lookupType = function (name) { | ||
for (var scope = this; scope; scope = scope.parent) | ||
@@ -362,4 +319,3 @@ if (scope.declaresType(name)) | ||
}; | ||
Sp.getGlobalScope = function() { | ||
Sp.getGlobalScope = function () { | ||
var scope = this; | ||
@@ -370,4 +326,5 @@ while (!scope.isGlobal) | ||
}; | ||
return Scope; | ||
}; | ||
} | ||
exports.default = scopePlugin; | ||
module.exports = exports["default"]; |
@@ -1,46 +0,49 @@ | ||
module.exports = function (fork) { | ||
var exports = {}; | ||
var types = fork.use(require("../lib/types")); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = __importDefault(require("./types")); | ||
function default_1(fork) { | ||
var types = fork.use(types_1.default); | ||
var Type = types.Type; | ||
var builtin = types.builtInTypes; | ||
var isNumber = builtin.number; | ||
// An example of constructing a new type with arbitrary constraints from | ||
// an existing type. | ||
exports.geq = function (than) { | ||
return new Type(function (value) { | ||
return isNumber.check(value) && value >= than; | ||
}, isNumber + " >= " + than); | ||
}; | ||
function geq(than) { | ||
return Type.from(function (value) { return isNumber.check(value) && value >= than; }, isNumber + " >= " + than); | ||
} | ||
; | ||
// Default value-returning functions that may optionally be passed as a | ||
// third argument to Def.prototype.field. | ||
exports.defaults = { | ||
var defaults = { | ||
// Functions were used because (among other reasons) that's the most | ||
// elegant way to allow for the emptyArray one always to give a new | ||
// array instance. | ||
"null": function () { return null }, | ||
"emptyArray": function () { return [] }, | ||
"false": function () { return false }, | ||
"true": function () { return true }, | ||
"undefined": function () {} | ||
"null": function () { return null; }, | ||
"emptyArray": function () { return []; }, | ||
"false": function () { return false; }, | ||
"true": function () { return true; }, | ||
"undefined": function () { }, | ||
"use strict": function () { return "use strict"; } | ||
}; | ||
var naiveIsPrimitive = Type.or( | ||
builtin.string, | ||
builtin.number, | ||
builtin.boolean, | ||
builtin.null, | ||
builtin.undefined | ||
); | ||
exports.isPrimitive = new Type(function (value) { | ||
var naiveIsPrimitive = Type.or(builtin.string, builtin.number, builtin.boolean, builtin.null, builtin.undefined); | ||
var isPrimitive = Type.from(function (value) { | ||
if (value === null) | ||
return true; | ||
var type = typeof value; | ||
return !(type === "object" || | ||
type === "function"); | ||
if (type === "object" || | ||
type === "function") { | ||
return false; | ||
} | ||
return true; | ||
}, naiveIsPrimitive.toString()); | ||
return exports; | ||
}; | ||
return { | ||
geq: geq, | ||
defaults: defaults, | ||
isPrimitive: isPrimitive, | ||
}; | ||
} | ||
exports.default = default_1; | ||
module.exports = exports["default"]; |
1260
lib/types.js
@@ -1,103 +0,312 @@ | ||
var Ap = Array.prototype; | ||
var slice = Ap.slice; | ||
var map = Ap.map; | ||
var each = Ap.forEach; | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Op = Object.prototype; | ||
var objToStr = Op.toString; | ||
var funObjStr = objToStr.call(function(){}); | ||
var strObjStr = objToStr.call(""); | ||
var hasOwn = Op.hasOwnProperty; | ||
module.exports = function () { | ||
var exports = {}; | ||
// A type is an object with a .check method that takes a value and returns | ||
// true or false according to whether the value matches the type. | ||
function Type(check, name) { | ||
var self = this; | ||
if (!(self instanceof Type)) { | ||
throw new Error("Type constructor cannot be invoked without 'new'"); | ||
var BaseType = /** @class */ (function () { | ||
function BaseType() { | ||
} | ||
BaseType.prototype.assert = function (value, deep) { | ||
if (!this.check(value, deep)) { | ||
var str = shallowStringify(value); | ||
throw new Error(str + " does not match type " + this); | ||
} | ||
// Unfortunately we can't elegantly reuse isFunction and isString, | ||
// here, because this code is executed while defining those types. | ||
if (objToStr.call(check) !== funObjStr) { | ||
throw new Error(check + " is not a function"); | ||
return true; | ||
}; | ||
BaseType.prototype.arrayOf = function () { | ||
var elemType = this; | ||
return new ArrayType(elemType); | ||
}; | ||
return BaseType; | ||
}()); | ||
var ArrayType = /** @class */ (function (_super) { | ||
__extends(ArrayType, _super); | ||
function ArrayType(elemType) { | ||
var _this = _super.call(this) || this; | ||
_this.elemType = elemType; | ||
_this.kind = "ArrayType"; | ||
return _this; | ||
} | ||
ArrayType.prototype.toString = function () { | ||
return "[" + this.elemType + "]"; | ||
}; | ||
ArrayType.prototype.check = function (value, deep) { | ||
var _this = this; | ||
return Array.isArray(value) && value.every(function (elem) { return _this.elemType.check(elem, deep); }); | ||
}; | ||
return ArrayType; | ||
}(BaseType)); | ||
var IdentityType = /** @class */ (function (_super) { | ||
__extends(IdentityType, _super); | ||
function IdentityType(value) { | ||
var _this = _super.call(this) || this; | ||
_this.value = value; | ||
_this.kind = "IdentityType"; | ||
return _this; | ||
} | ||
IdentityType.prototype.toString = function () { | ||
return String(this.value); | ||
}; | ||
IdentityType.prototype.check = function (value, deep) { | ||
var result = value === this.value; | ||
if (!result && typeof deep === "function") { | ||
deep(this, value); | ||
} | ||
// The `name` parameter can be either a function or a string. | ||
var nameObjStr = objToStr.call(name); | ||
if (!(nameObjStr === funObjStr || | ||
nameObjStr === strObjStr)) { | ||
throw new Error(name + " is neither a function nor a string"); | ||
return result; | ||
}; | ||
return IdentityType; | ||
}(BaseType)); | ||
var ObjectType = /** @class */ (function (_super) { | ||
__extends(ObjectType, _super); | ||
function ObjectType(fields) { | ||
var _this = _super.call(this) || this; | ||
_this.fields = fields; | ||
_this.kind = "ObjectType"; | ||
return _this; | ||
} | ||
ObjectType.prototype.toString = function () { | ||
return "{ " + this.fields.join(", ") + " }"; | ||
}; | ||
ObjectType.prototype.check = function (value, deep) { | ||
return (objToStr.call(value) === objToStr.call({}) && | ||
this.fields.every(function (field) { | ||
return field.type.check(value[field.name], deep); | ||
})); | ||
}; | ||
return ObjectType; | ||
}(BaseType)); | ||
var OrType = /** @class */ (function (_super) { | ||
__extends(OrType, _super); | ||
function OrType(types) { | ||
var _this = _super.call(this) || this; | ||
_this.types = types; | ||
_this.kind = "OrType"; | ||
return _this; | ||
} | ||
OrType.prototype.toString = function () { | ||
return this.types.join(" | "); | ||
}; | ||
OrType.prototype.check = function (value, deep) { | ||
return this.types.some(function (type) { | ||
return type.check(value, deep); | ||
}); | ||
}; | ||
return OrType; | ||
}(BaseType)); | ||
var PredicateType = /** @class */ (function (_super) { | ||
__extends(PredicateType, _super); | ||
function PredicateType(name, predicate) { | ||
var _this = _super.call(this) || this; | ||
_this.name = name; | ||
_this.predicate = predicate; | ||
_this.kind = "PredicateType"; | ||
return _this; | ||
} | ||
PredicateType.prototype.toString = function () { | ||
return this.name; | ||
}; | ||
PredicateType.prototype.check = function (value, deep) { | ||
var result = this.predicate(value, deep); | ||
if (!result && typeof deep === "function") { | ||
deep(this, value); | ||
} | ||
Object.defineProperties(self, { | ||
name: {value: name}, | ||
check: { | ||
value: function (value, deep) { | ||
var result = check.call(self, value, deep); | ||
if (!result && deep && objToStr.call(deep) === funObjStr) | ||
deep(self, value); | ||
return result; | ||
return result; | ||
}; | ||
return PredicateType; | ||
}(BaseType)); | ||
var Def = /** @class */ (function () { | ||
function Def(type, typeName) { | ||
this.type = type; | ||
this.typeName = typeName; | ||
this.baseNames = []; | ||
this.ownFields = Object.create(null); | ||
// Includes own typeName. Populated during finalization. | ||
this.allSupertypes = Object.create(null); | ||
// Linear inheritance hierarchy. Populated during finalization. | ||
this.supertypeList = []; | ||
// Includes inherited fields. | ||
this.allFields = Object.create(null); | ||
// Non-hidden keys of allFields. | ||
this.fieldNames = []; | ||
// This property will be overridden as true by individual Def instances | ||
// when they are finalized. | ||
this.finalized = false; | ||
// False by default until .build(...) is called on an instance. | ||
this.buildable = false; | ||
this.buildParams = []; | ||
} | ||
Def.prototype.isSupertypeOf = function (that) { | ||
if (that instanceof Def) { | ||
if (this.finalized !== true || | ||
that.finalized !== true) { | ||
throw new Error(""); | ||
} | ||
return hasOwn.call(that.allSupertypes, this.typeName); | ||
} | ||
else { | ||
throw new Error(that + " is not a Def"); | ||
} | ||
}; | ||
Def.prototype.checkAllFields = function (value, deep) { | ||
var allFields = this.allFields; | ||
if (this.finalized !== true) { | ||
throw new Error("" + this.typeName); | ||
} | ||
function checkFieldByName(name) { | ||
var field = allFields[name]; | ||
var type = field.type; | ||
var child = field.getValue(value); | ||
return type.check(child, deep); | ||
} | ||
return value !== null && | ||
typeof value === "object" && | ||
Object.keys(allFields).every(checkFieldByName); | ||
}; | ||
Def.prototype.bases = function () { | ||
var supertypeNames = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
supertypeNames[_i] = arguments[_i]; | ||
} | ||
var bases = this.baseNames; | ||
if (this.finalized) { | ||
if (supertypeNames.length !== bases.length) { | ||
throw new Error(""); | ||
} | ||
for (var i = 0; i < supertypeNames.length; i++) { | ||
if (supertypeNames[i] !== bases[i]) { | ||
throw new Error(""); | ||
} | ||
} | ||
return this; | ||
} | ||
supertypeNames.forEach(function (baseName) { | ||
// This indexOf lookup may be O(n), but the typical number of base | ||
// names is very small, and indexOf is a native Array method. | ||
if (bases.indexOf(baseName) < 0) { | ||
bases.push(baseName); | ||
} | ||
}); | ||
return this; // For chaining. | ||
}; | ||
return Def; | ||
}()); | ||
exports.Def = Def; | ||
var Field = /** @class */ (function () { | ||
function Field(name, type, defaultFn, hidden) { | ||
this.name = name; | ||
this.type = type; | ||
this.defaultFn = defaultFn; | ||
this.hidden = !!hidden; | ||
} | ||
var Tp = Type.prototype; | ||
// Throughout this file we use Object.defineProperty to prevent | ||
// redefinition of exported properties. | ||
exports.Type = Type; | ||
// Like .check, except that failure triggers an AssertionError. | ||
Tp.assert = function (value, deep) { | ||
if (!this.check(value, deep)) { | ||
var str = shallowStringify(value); | ||
throw new Error(str + " does not match type " + this); | ||
Field.prototype.toString = function () { | ||
return JSON.stringify(this.name) + ": " + this.type; | ||
}; | ||
Field.prototype.getValue = function (obj) { | ||
var value = obj[this.name]; | ||
if (typeof value !== "undefined") { | ||
return value; | ||
} | ||
return true; | ||
if (typeof this.defaultFn === "function") { | ||
value = this.defaultFn.call(obj); | ||
} | ||
return value; | ||
}; | ||
function shallowStringify(value) { | ||
if (isObject.check(value)) | ||
return "{" + Object.keys(value).map(function (key) { | ||
return key + ": " + value[key]; | ||
}).join(", ") + "}"; | ||
if (isArray.check(value)) | ||
return "[" + value.map(shallowStringify).join(", ") + "]"; | ||
return JSON.stringify(value); | ||
return Field; | ||
}()); | ||
function shallowStringify(value) { | ||
if (Array.isArray(value)) { | ||
return "[" + value.map(shallowStringify).join(", ") + "]"; | ||
} | ||
Tp.toString = function () { | ||
var name = this.name; | ||
if (isString.check(name)) | ||
return name; | ||
if (isFunction.check(name)) | ||
return name.call(this) + ""; | ||
return name + " type"; | ||
if (value && typeof value === "object") { | ||
return "{ " + Object.keys(value).map(function (key) { | ||
return key + ": " + value[key]; | ||
}).join(", ") + " }"; | ||
} | ||
return JSON.stringify(value); | ||
} | ||
function typesPlugin(_fork) { | ||
var Type = { | ||
or: function () { | ||
var types = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
types[_i] = arguments[_i]; | ||
} | ||
return new OrType(types.map(function (type) { return Type.from(type); })); | ||
}, | ||
from: function (value, name) { | ||
if (value instanceof ArrayType || | ||
value instanceof IdentityType || | ||
value instanceof ObjectType || | ||
value instanceof OrType || | ||
value instanceof PredicateType) { | ||
return value; | ||
} | ||
// The Def type is used as a helper for constructing compound | ||
// interface types for AST nodes. | ||
if (value instanceof Def) { | ||
return value.type; | ||
} | ||
// Support [ElemType] syntax. | ||
if (isArray.check(value)) { | ||
if (value.length !== 1) { | ||
throw new Error("only one element type is permitted for typed arrays"); | ||
} | ||
return new ArrayType(Type.from(value[0])); | ||
} | ||
// Support { someField: FieldType, ... } syntax. | ||
if (isObject.check(value)) { | ||
return new ObjectType(Object.keys(value).map(function (name) { | ||
return new Field(name, Type.from(value[name], name)); | ||
})); | ||
} | ||
if (typeof value === "function") { | ||
var bicfIndex = builtInCtorFns.indexOf(value); | ||
if (bicfIndex >= 0) { | ||
return builtInCtorTypes[bicfIndex]; | ||
} | ||
if (typeof name !== "string") { | ||
throw new Error("missing name"); | ||
} | ||
return new PredicateType(name, value); | ||
} | ||
// As a last resort, toType returns a type that matches any value that | ||
// is === from. This is primarily useful for literal values like | ||
// toType(null), but it has the additional advantage of allowing | ||
// toType to be a total function. | ||
return new IdentityType(value); | ||
}, | ||
// Define a type whose name is registered in a namespace (the defCache) so | ||
// that future definitions will return the same type given the same name. | ||
// In particular, this system allows for circular and forward definitions. | ||
// The Def object d returned from Type.def may be used to configure the | ||
// type d.type by calling methods such as d.bases, d.build, and d.field. | ||
def: function (typeName) { | ||
return hasOwn.call(defCache, typeName) | ||
? defCache[typeName] | ||
: defCache[typeName] = new DefImpl(typeName); | ||
}, | ||
hasDef: function (typeName) { | ||
return hasOwn.call(defCache, typeName); | ||
} | ||
}; | ||
var builtInCtorFns = []; | ||
var builtInCtorTypes = []; | ||
var builtInTypes = {}; | ||
exports.builtInTypes = builtInTypes; | ||
function defBuiltInType(example, name) { | ||
var objStr = objToStr.call(example); | ||
var type = new Type(function (value) { | ||
return objToStr.call(value) === objStr; | ||
}, name); | ||
var type = new PredicateType(name, function (value) { return objToStr.call(value) === objStr; }); | ||
builtInTypes[name] = type; | ||
if (example && typeof example.constructor === "function") { | ||
@@ -107,6 +316,4 @@ builtInCtorFns.push(example.constructor); | ||
} | ||
return type; | ||
} | ||
// These types check the underlying [[Class]] attribute of the given | ||
@@ -117,3 +324,3 @@ // value, rather than using the problematic typeof operator. Note however | ||
var isString = defBuiltInType("truthy", "string"); | ||
var isFunction = defBuiltInType(function () {}, "function"); | ||
var isFunction = defBuiltInType(function () { }, "function"); | ||
var isArray = defBuiltInType([], "array"); | ||
@@ -127,187 +334,10 @@ var isObject = defBuiltInType({}, "object"); | ||
var isUndefined = defBuiltInType(void 0, "undefined"); | ||
// There are a number of idiomatic ways of expressing types, so this | ||
// function serves to coerce them all to actual Type objects. Note that | ||
// providing the name argument is not necessary in most cases. | ||
function toType(from, name) { | ||
// The toType function should of course be idempotent. | ||
if (from instanceof Type) | ||
return from; | ||
// The Def type is used as a helper for constructing compound | ||
// interface types for AST nodes. | ||
if (from instanceof Def) | ||
return from.type; | ||
// Support [ElemType] syntax. | ||
if (isArray.check(from)) | ||
return Type.fromArray(from); | ||
// Support { someField: FieldType, ... } syntax. | ||
if (isObject.check(from)) | ||
return Type.fromObject(from); | ||
if (isFunction.check(from)) { | ||
var bicfIndex = builtInCtorFns.indexOf(from); | ||
if (bicfIndex >= 0) { | ||
return builtInCtorTypes[bicfIndex]; | ||
} | ||
// If isFunction.check(from), and from is not a built-in | ||
// constructor, assume from is a binary predicate function we can | ||
// use to define the type. | ||
return new Type(from, name); | ||
} | ||
// As a last resort, toType returns a type that matches any value that | ||
// is === from. This is primarily useful for literal values like | ||
// toType(null), but it has the additional advantage of allowing | ||
// toType to be a total function. | ||
return new Type(function (value) { | ||
return value === from; | ||
}, isUndefined.check(name) ? function () { | ||
return from + ""; | ||
} : name); | ||
} | ||
// Returns a type that matches the given value iff any of type1, type2, | ||
// etc. match the value. | ||
Type.or = function (/* type1, type2, ... */) { | ||
var types = []; | ||
var len = arguments.length; | ||
for (var i = 0; i < len; ++i) | ||
types.push(toType(arguments[i])); | ||
return new Type(function (value, deep) { | ||
for (var i = 0; i < len; ++i) | ||
if (types[i].check(value, deep)) | ||
return true; | ||
return false; | ||
}, function () { | ||
return types.join(" | "); | ||
}); | ||
}; | ||
Type.fromArray = function (arr) { | ||
if (!isArray.check(arr)) { | ||
throw new Error(""); | ||
} | ||
if (arr.length !== 1) { | ||
throw new Error("only one element type is permitted for typed arrays"); | ||
} | ||
return toType(arr[0]).arrayOf(); | ||
}; | ||
Tp.arrayOf = function () { | ||
var elemType = this; | ||
return new Type(function (value, deep) { | ||
return isArray.check(value) && value.every(function (elem) { | ||
return elemType.check(elem, deep); | ||
}); | ||
}, function () { | ||
return "[" + elemType + "]"; | ||
}); | ||
}; | ||
Type.fromObject = function (obj) { | ||
var fields = Object.keys(obj).map(function (name) { | ||
return new Field(name, obj[name]); | ||
}); | ||
return new Type(function (value, deep) { | ||
return isObject.check(value) && fields.every(function (field) { | ||
return field.type.check(value[field.name], deep); | ||
}); | ||
}, function () { | ||
return "{ " + fields.join(", ") + " }"; | ||
}); | ||
}; | ||
function Field(name, type, defaultFn, hidden) { | ||
var self = this; | ||
if (!(self instanceof Field)) { | ||
throw new Error("Field constructor cannot be invoked without 'new'"); | ||
} | ||
isString.assert(name); | ||
type = toType(type); | ||
var properties = { | ||
name: {value: name}, | ||
type: {value: type}, | ||
hidden: {value: !!hidden} | ||
}; | ||
if (isFunction.check(defaultFn)) { | ||
properties.defaultFn = {value: defaultFn}; | ||
} | ||
Object.defineProperties(self, properties); | ||
} | ||
var Fp = Field.prototype; | ||
Fp.toString = function () { | ||
return JSON.stringify(this.name) + ": " + this.type; | ||
}; | ||
Fp.getValue = function (obj) { | ||
var value = obj[this.name]; | ||
if (!isUndefined.check(value)) | ||
return value; | ||
if (this.defaultFn) | ||
value = this.defaultFn.call(obj); | ||
return value; | ||
}; | ||
// Define a type whose name is registered in a namespace (the defCache) so | ||
// that future definitions will return the same type given the same name. | ||
// In particular, this system allows for circular and forward definitions. | ||
// The Def object d returned from Type.def may be used to configure the | ||
// type d.type by calling methods such as d.bases, d.build, and d.field. | ||
Type.def = function (typeName) { | ||
isString.assert(typeName); | ||
return hasOwn.call(defCache, typeName) | ||
? defCache[typeName] | ||
: defCache[typeName] = new Def(typeName); | ||
}; | ||
// In order to return the same Def instance every time Type.def is called | ||
// with a particular name, those instances need to be stored in a cache. | ||
var defCache = Object.create(null); | ||
function Def(typeName) { | ||
var self = this; | ||
if (!(self instanceof Def)) { | ||
throw new Error("Def constructor cannot be invoked without 'new'"); | ||
} | ||
Object.defineProperties(self, { | ||
typeName: {value: typeName}, | ||
baseNames: {value: []}, | ||
ownFields: {value: Object.create(null)}, | ||
// These two are populated during finalization. | ||
allSupertypes: {value: Object.create(null)}, // Includes own typeName. | ||
supertypeList: {value: []}, // Linear inheritance hierarchy. | ||
allFields: {value: Object.create(null)}, // Includes inherited fields. | ||
fieldNames: {value: []}, // Non-hidden keys of allFields. | ||
type: { | ||
value: new Type(function (value, deep) { | ||
return self.check(value, deep); | ||
}, typeName) | ||
} | ||
}); | ||
} | ||
Def.fromValue = function (value) { | ||
function defFromValue(value) { | ||
if (value && typeof value === "object") { | ||
var type = value.type; | ||
if (typeof type === "string" && | ||
hasOwn.call(defCache, type)) { | ||
hasOwn.call(defCache, type)) { | ||
var d = defCache[type]; | ||
@@ -319,23 +349,236 @@ if (d.finalized) { | ||
} | ||
return null; | ||
}; | ||
var Dp = Def.prototype; | ||
Dp.isSupertypeOf = function (that) { | ||
if (that instanceof Def) { | ||
if (this.finalized !== true || | ||
that.finalized !== true) { | ||
throw new Error(""); | ||
} | ||
var DefImpl = /** @class */ (function (_super) { | ||
__extends(DefImpl, _super); | ||
function DefImpl(typeName) { | ||
var _this = _super.call(this, new PredicateType(typeName, function (value, deep) { return _this.check(value, deep); }), typeName) || this; | ||
return _this; | ||
} | ||
DefImpl.prototype.check = function (value, deep) { | ||
if (this.finalized !== true) { | ||
throw new Error("prematurely checking unfinalized type " + this.typeName); | ||
} | ||
return hasOwn.call(that.allSupertypes, this.typeName); | ||
} else { | ||
throw new Error(that + " is not a Def"); | ||
} | ||
}; | ||
// A Def type can only match an object value. | ||
if (value === null || typeof value !== "object") { | ||
return false; | ||
} | ||
var vDef = defFromValue(value); | ||
if (!vDef) { | ||
// If we couldn't infer the Def associated with the given value, | ||
// and we expected it to be a SourceLocation or a Position, it was | ||
// probably just missing a "type" field (because Esprima does not | ||
// assign a type property to such nodes). Be optimistic and let | ||
// this.checkAllFields make the final decision. | ||
if (this.typeName === "SourceLocation" || | ||
this.typeName === "Position") { | ||
return this.checkAllFields(value, deep); | ||
} | ||
// Calling this.checkAllFields for any other type of node is both | ||
// bad for performance and way too forgiving. | ||
return false; | ||
} | ||
// If checking deeply and vDef === this, then we only need to call | ||
// checkAllFields once. Calling checkAllFields is too strict when deep | ||
// is false, because then we only care about this.isSupertypeOf(vDef). | ||
if (deep && vDef === this) { | ||
return this.checkAllFields(value, deep); | ||
} | ||
// In most cases we rely exclusively on isSupertypeOf to make O(1) | ||
// subtyping determinations. This suffices in most situations outside | ||
// of unit tests, since interface conformance is checked whenever new | ||
// instances are created using builder functions. | ||
if (!this.isSupertypeOf(vDef)) { | ||
return false; | ||
} | ||
// The exception is when deep is true; then, we recursively check all | ||
// fields. | ||
if (!deep) { | ||
return true; | ||
} | ||
// Use the more specific Def (vDef) to perform the deep check, but | ||
// shallow-check fields defined by the less specific Def (this). | ||
return vDef.checkAllFields(value, deep) | ||
&& this.checkAllFields(value, false); | ||
}; | ||
DefImpl.prototype.build = function () { | ||
var _this = this; | ||
var buildParams = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
buildParams[_i] = arguments[_i]; | ||
} | ||
// Calling Def.prototype.build multiple times has the effect of merely | ||
// redefining this property. | ||
this.buildParams = buildParams; | ||
if (this.buildable) { | ||
// If this Def is already buildable, update self.buildParams and | ||
// continue using the old builder function. | ||
return this; | ||
} | ||
// Every buildable type will have its "type" field filled in | ||
// automatically. This includes types that are not subtypes of Node, | ||
// like SourceLocation, but that seems harmless (TODO?). | ||
this.field("type", String, function () { return _this.typeName; }); | ||
// Override Dp.buildable for this Def instance. | ||
this.buildable = true; | ||
var addParam = function (built, param, arg, isArgAvailable) { | ||
if (hasOwn.call(built, param)) | ||
return; | ||
var all = _this.allFields; | ||
if (!hasOwn.call(all, param)) { | ||
throw new Error("" + param); | ||
} | ||
var field = all[param]; | ||
var type = field.type; | ||
var value; | ||
if (isArgAvailable) { | ||
value = arg; | ||
} | ||
else if (field.defaultFn) { | ||
// Expose the partially-built object to the default | ||
// function as its `this` object. | ||
value = field.defaultFn.call(built); | ||
} | ||
else { | ||
var message = "no value or default function given for field " + | ||
JSON.stringify(param) + " of " + _this.typeName + "(" + | ||
_this.buildParams.map(function (name) { | ||
return all[name]; | ||
}).join(", ") + ")"; | ||
throw new Error(message); | ||
} | ||
if (!type.check(value)) { | ||
throw new Error(shallowStringify(value) + | ||
" does not match field " + field + | ||
" of type " + _this.typeName); | ||
} | ||
built[param] = value; | ||
}; | ||
// Calling the builder function will construct an instance of the Def, | ||
// with positional arguments mapped to the fields original passed to .build. | ||
// If not enough arguments are provided, the default value for the remaining fields | ||
// will be used. | ||
var builder = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var argc = args.length; | ||
if (!_this.finalized) { | ||
throw new Error("attempting to instantiate unfinalized type " + | ||
_this.typeName); | ||
} | ||
var built = Object.create(nodePrototype); | ||
_this.buildParams.forEach(function (param, i) { | ||
if (i < argc) { | ||
addParam(built, param, args[i], true); | ||
} | ||
else { | ||
addParam(built, param, null, false); | ||
} | ||
}); | ||
Object.keys(_this.allFields).forEach(function (param) { | ||
// Use the default value. | ||
addParam(built, param, null, false); | ||
}); | ||
// Make sure that the "type" field was filled automatically. | ||
if (built.type !== _this.typeName) { | ||
throw new Error(""); | ||
} | ||
return built; | ||
}; | ||
// Calling .from on the builder function will construct an instance of the Def, | ||
// using field values from the passed object. For fields missing from the passed object, | ||
// their default value will be used. | ||
builder.from = function (obj) { | ||
if (!_this.finalized) { | ||
throw new Error("attempting to instantiate unfinalized type " + | ||
_this.typeName); | ||
} | ||
var built = Object.create(nodePrototype); | ||
Object.keys(_this.allFields).forEach(function (param) { | ||
if (hasOwn.call(obj, param)) { | ||
addParam(built, param, obj[param], true); | ||
} | ||
else { | ||
addParam(built, param, null, false); | ||
} | ||
}); | ||
// Make sure that the "type" field was filled automatically. | ||
if (built.type !== _this.typeName) { | ||
throw new Error(""); | ||
} | ||
return built; | ||
}; | ||
Object.defineProperty(builders, getBuilderName(this.typeName), { | ||
enumerable: true, | ||
value: builder | ||
}); | ||
return this; | ||
}; | ||
// The reason fields are specified using .field(...) instead of an object | ||
// literal syntax is somewhat subtle: the object literal syntax would | ||
// support only one key and one value, but with .field(...) we can pass | ||
// any number of arguments to specify the field. | ||
DefImpl.prototype.field = function (name, type, defaultFn, hidden) { | ||
if (this.finalized) { | ||
console.error("Ignoring attempt to redefine field " + | ||
JSON.stringify(name) + " of finalized type " + | ||
JSON.stringify(this.typeName)); | ||
return this; | ||
} | ||
this.ownFields[name] = new Field(name, Type.from(type), defaultFn, hidden); | ||
return this; // For chaining. | ||
}; | ||
DefImpl.prototype.finalize = function () { | ||
var _this = this; | ||
// It's not an error to finalize a type more than once, but only the | ||
// first call to .finalize does anything. | ||
if (!this.finalized) { | ||
var allFields = this.allFields; | ||
var allSupertypes = this.allSupertypes; | ||
this.baseNames.forEach(function (name) { | ||
var def = defCache[name]; | ||
if (def instanceof Def) { | ||
def.finalize(); | ||
extend(allFields, def.allFields); | ||
extend(allSupertypes, def.allSupertypes); | ||
} | ||
else { | ||
var message = "unknown supertype name " + | ||
JSON.stringify(name) + | ||
" for subtype " + | ||
JSON.stringify(_this.typeName); | ||
throw new Error(message); | ||
} | ||
}); | ||
// TODO Warn if fields are overridden with incompatible types. | ||
extend(allFields, this.ownFields); | ||
allSupertypes[this.typeName] = this; | ||
this.fieldNames.length = 0; | ||
for (var fieldName in allFields) { | ||
if (hasOwn.call(allFields, fieldName) && | ||
!allFields[fieldName].hidden) { | ||
this.fieldNames.push(fieldName); | ||
} | ||
} | ||
// Types are exported only once they have been finalized. | ||
Object.defineProperty(namedTypes, this.typeName, { | ||
enumerable: true, | ||
value: this.type | ||
}); | ||
this.finalized = true; | ||
// A linearization of the inheritance hierarchy. | ||
populateSupertypeList(this.typeName, this.supertypeList); | ||
if (this.buildable && | ||
this.supertypeList.lastIndexOf("Expression") >= 0) { | ||
wrapExpressionBuilderWithStatement(this.typeName); | ||
} | ||
} | ||
}; | ||
return DefImpl; | ||
}(Def)); | ||
// Note that the list returned by this function is a copy of the internal | ||
// supertypeList, *without* the typeName itself as the first element. | ||
exports.getSupertypeNames = function (typeName) { | ||
function getSupertypeNames(typeName) { | ||
if (!hasOwn.call(defCache, typeName)) { | ||
@@ -349,12 +592,10 @@ throw new Error(""); | ||
return d.supertypeList.slice(1); | ||
}; | ||
} | ||
// Returns an object mapping from every known type in the defCache to the | ||
// most specific supertype whose name is an own property of the candidates | ||
// object. | ||
exports.computeSupertypeLookupTable = function (candidates) { | ||
function computeSupertypeLookupTable(candidates) { | ||
var table = {}; | ||
var typeNames = Object.keys(defCache); | ||
var typeNameCount = typeNames.length; | ||
for (var i = 0; i < typeNameCount; ++i) { | ||
@@ -374,283 +615,25 @@ var typeName = typeNames[i]; | ||
} | ||
return table; | ||
}; | ||
Dp.checkAllFields = function (value, deep) { | ||
var allFields = this.allFields; | ||
if (this.finalized !== true) { | ||
throw new Error("" + this.typeName); | ||
} | ||
function checkFieldByName(name) { | ||
var field = allFields[name]; | ||
var type = field.type; | ||
var child = field.getValue(value); | ||
return type.check(child, deep); | ||
} | ||
return isObject.check(value) | ||
&& Object.keys(allFields).every(checkFieldByName); | ||
}; | ||
Dp.check = function (value, deep) { | ||
if (this.finalized !== true) { | ||
throw new Error( | ||
"prematurely checking unfinalized type " + this.typeName | ||
); | ||
} | ||
// A Def type can only match an object value. | ||
if (!isObject.check(value)) | ||
return false; | ||
var vDef = Def.fromValue(value); | ||
if (!vDef) { | ||
// If we couldn't infer the Def associated with the given value, | ||
// and we expected it to be a SourceLocation or a Position, it was | ||
// probably just missing a "type" field (because Esprima does not | ||
// assign a type property to such nodes). Be optimistic and let | ||
// this.checkAllFields make the final decision. | ||
if (this.typeName === "SourceLocation" || | ||
this.typeName === "Position") { | ||
return this.checkAllFields(value, deep); | ||
} | ||
// Calling this.checkAllFields for any other type of node is both | ||
// bad for performance and way too forgiving. | ||
return false; | ||
} | ||
// If checking deeply and vDef === this, then we only need to call | ||
// checkAllFields once. Calling checkAllFields is too strict when deep | ||
// is false, because then we only care about this.isSupertypeOf(vDef). | ||
if (deep && vDef === this) | ||
return this.checkAllFields(value, deep); | ||
// In most cases we rely exclusively on isSupertypeOf to make O(1) | ||
// subtyping determinations. This suffices in most situations outside | ||
// of unit tests, since interface conformance is checked whenever new | ||
// instances are created using builder functions. | ||
if (!this.isSupertypeOf(vDef)) | ||
return false; | ||
// The exception is when deep is true; then, we recursively check all | ||
// fields. | ||
if (!deep) | ||
return true; | ||
// Use the more specific Def (vDef) to perform the deep check, but | ||
// shallow-check fields defined by the less specific Def (this). | ||
return vDef.checkAllFields(value, deep) | ||
&& this.checkAllFields(value, false); | ||
}; | ||
Dp.bases = function () { | ||
var args = slice.call(arguments); | ||
var bases = this.baseNames; | ||
if (this.finalized) { | ||
if (args.length !== bases.length) { | ||
throw new Error(""); | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
if (args[i] !== bases[i]) { | ||
throw new Error(""); | ||
} | ||
} | ||
return this; | ||
} | ||
args.forEach(function (baseName) { | ||
isString.assert(baseName); | ||
// This indexOf lookup may be O(n), but the typical number of base | ||
// names is very small, and indexOf is a native Array method. | ||
if (bases.indexOf(baseName) < 0) | ||
bases.push(baseName); | ||
}); | ||
return this; // For chaining. | ||
}; | ||
// False by default until .build(...) is called on an instance. | ||
Object.defineProperty(Dp, "buildable", {value: false}); | ||
var builders = {}; | ||
exports.builders = builders; | ||
} | ||
var builders = Object.create(null); | ||
// This object is used as prototype for any node created by a builder. | ||
var nodePrototype = {}; | ||
// Call this function to define a new method to be shared by all AST | ||
// nodes. The replaced method (if any) is returned for easy wrapping. | ||
exports.defineMethod = function (name, func) { | ||
// nodes. The replaced method (if any) is returned for easy wrapping. | ||
function defineMethod(name, func) { | ||
var old = nodePrototype[name]; | ||
// Pass undefined as func to delete nodePrototype[name]. | ||
if (isUndefined.check(func)) { | ||
delete nodePrototype[name]; | ||
} else { | ||
} | ||
else { | ||
isFunction.assert(func); | ||
Object.defineProperty(nodePrototype, name, { | ||
enumerable: true, // For discoverability. | ||
configurable: true, // For delete proto[name]. | ||
enumerable: true, | ||
configurable: true, | ||
value: func | ||
}); | ||
} | ||
return old; | ||
}; | ||
var isArrayOfString = isString.arrayOf(); | ||
// Calling the .build method of a Def simultaneously marks the type as | ||
// buildable (by defining builders[getBuilderName(typeName)]) and | ||
// specifies the order of arguments that should be passed to the builder | ||
// function to create an instance of the type. | ||
Dp.build = function (/* param1, param2, ... */) { | ||
var self = this; | ||
var newBuildParams = slice.call(arguments); | ||
isArrayOfString.assert(newBuildParams); | ||
// Calling Def.prototype.build multiple times has the effect of merely | ||
// redefining this property. | ||
Object.defineProperty(self, "buildParams", { | ||
value: newBuildParams, | ||
writable: false, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
if (self.buildable) { | ||
// If this Def is already buildable, update self.buildParams and | ||
// continue using the old builder function. | ||
return self; | ||
} | ||
// Every buildable type will have its "type" field filled in | ||
// automatically. This includes types that are not subtypes of Node, | ||
// like SourceLocation, but that seems harmless (TODO?). | ||
self.field("type", String, function () { return self.typeName }); | ||
// Override Dp.buildable for this Def instance. | ||
Object.defineProperty(self, "buildable", {value: true}); | ||
function addParam(built, param, arg, isArgAvailable) { | ||
if (hasOwn.call(built, param)) | ||
return; | ||
var all = self.allFields; | ||
if (!hasOwn.call(all, param)) { | ||
throw new Error("" + param); | ||
} | ||
var field = all[param]; | ||
var type = field.type; | ||
var value; | ||
if (isArgAvailable) { | ||
value = arg; | ||
} else if (field.defaultFn) { | ||
// Expose the partially-built object to the default | ||
// function as its `this` object. | ||
value = field.defaultFn.call(built); | ||
} else { | ||
var message = "no value or default function given for field " + | ||
JSON.stringify(param) + " of " + self.typeName + "(" + | ||
self.buildParams.map(function (name) { | ||
return all[name]; | ||
}).join(", ") + ")"; | ||
throw new Error(message); | ||
} | ||
if (!type.check(value)) { | ||
throw new Error( | ||
shallowStringify(value) + | ||
" does not match field " + field + | ||
" of type " + self.typeName | ||
); | ||
} | ||
built[param] = value; | ||
} | ||
// Calling the builder function will construct an instance of the Def, | ||
// with positional arguments mapped to the fields original passed to .build. | ||
// If not enough arguments are provided, the default value for the remaining fields | ||
// will be used. | ||
function builder() { | ||
var args = arguments; | ||
var argc = args.length; | ||
if (!self.finalized) { | ||
throw new Error( | ||
"attempting to instantiate unfinalized type " + | ||
self.typeName | ||
); | ||
} | ||
var built = Object.create(nodePrototype); | ||
self.buildParams.forEach(function (param, i) { | ||
if (i < argc) { | ||
addParam(built, param, args[i], true) | ||
} else { | ||
addParam(built, param, null, false); | ||
} | ||
}); | ||
Object.keys(self.allFields).forEach(function (param) { | ||
// Use the default value. | ||
addParam(built, param, null, false); | ||
}); | ||
// Make sure that the "type" field was filled automatically. | ||
if (built.type !== self.typeName) { | ||
throw new Error(""); | ||
} | ||
return built; | ||
} | ||
// Calling .from on the builder function will construct an instance of the Def, | ||
// using field values from the passed object. For fields missing from the passed object, | ||
// their default value will be used. | ||
builder.from = function (obj) { | ||
if (!self.finalized) { | ||
throw new Error( | ||
"attempting to instantiate unfinalized type " + | ||
self.typeName | ||
); | ||
} | ||
var built = Object.create(nodePrototype); | ||
Object.keys(self.allFields).forEach(function (param) { | ||
if (hasOwn.call(obj, param)) { | ||
addParam(built, param, obj[param], true); | ||
} else { | ||
addParam(built, param, null, false); | ||
} | ||
}); | ||
// Make sure that the "type" field was filled automatically. | ||
if (built.type !== self.typeName) { | ||
throw new Error(""); | ||
} | ||
return built; | ||
} | ||
Object.defineProperty(builders, getBuilderName(self.typeName), { | ||
enumerable: true, | ||
value: builder | ||
}); | ||
return self; // For chaining. | ||
}; | ||
} | ||
function getBuilderName(typeName) { | ||
@@ -667,10 +650,7 @@ return typeName.replace(/^[A-Z]+/, function (upperCasePrefix) { | ||
// example) becomes xmlDefaultDeclaration. | ||
return upperCasePrefix.slice( | ||
0, len - 1).toLowerCase() + | ||
upperCasePrefix.charAt(len - 1); | ||
return upperCasePrefix.slice(0, len - 1).toLowerCase() + | ||
upperCasePrefix.charAt(len - 1); | ||
} | ||
}); | ||
} | ||
exports.getBuilderName = getBuilderName; | ||
function getStatementBuilderName(typeName) { | ||
@@ -680,44 +660,19 @@ typeName = getBuilderName(typeName); | ||
} | ||
exports.getStatementBuilderName = getStatementBuilderName; | ||
// The reason fields are specified using .field(...) instead of an object | ||
// literal syntax is somewhat subtle: the object literal syntax would | ||
// support only one key and one value, but with .field(...) we can pass | ||
// any number of arguments to specify the field. | ||
Dp.field = function (name, type, defaultFn, hidden) { | ||
if (this.finalized) { | ||
console.error("Ignoring attempt to redefine field " + | ||
JSON.stringify(name) + " of finalized type " + | ||
JSON.stringify(this.typeName)); | ||
return this; | ||
} | ||
this.ownFields[name] = new Field(name, type, defaultFn, hidden); | ||
return this; // For chaining. | ||
}; | ||
var namedTypes = {}; | ||
exports.namedTypes = namedTypes; | ||
// Like Object.keys, but aware of what fields each AST type should have. | ||
function getFieldNames(object) { | ||
var d = Def.fromValue(object); | ||
var d = defFromValue(object); | ||
if (d) { | ||
return d.fieldNames.slice(0); | ||
} | ||
if ("type" in object) { | ||
throw new Error( | ||
"did not recognize object of type " + | ||
JSON.stringify(object.type) | ||
); | ||
throw new Error("did not recognize object of type " + | ||
JSON.stringify(object.type)); | ||
} | ||
return Object.keys(object); | ||
} | ||
exports.getFieldNames = getFieldNames; | ||
// Get the value of an object property, taking object.type and default | ||
// functions into account. | ||
function getFieldValue(object, fieldName) { | ||
var d = Def.fromValue(object); | ||
var d = defFromValue(object); | ||
if (d) { | ||
@@ -729,7 +684,4 @@ var field = d.allFields[fieldName]; | ||
} | ||
return object && object[fieldName]; | ||
} | ||
exports.getFieldValue = getFieldValue; | ||
// Iterate over all defined fields of an object, including those missing | ||
@@ -739,8 +691,7 @@ // or undefined, passing each field name and effective value (as returned | ||
// Def, the callback will never be called. | ||
exports.eachField = function (object, callback, context) { | ||
function eachField(object, callback, context) { | ||
getFieldNames(object).forEach(function (name) { | ||
callback.call(this, name, getFieldValue(object, name)); | ||
}, context); | ||
}; | ||
} | ||
// Similar to eachField, except that iteration stops as soon as the | ||
@@ -750,65 +701,7 @@ // callback returns a truthy value. Like Array.prototype.some, the final | ||
// returned true for any element or not. | ||
exports.someField = function (object, callback, context) { | ||
function someField(object, callback, context) { | ||
return getFieldNames(object).some(function (name) { | ||
return callback.call(this, name, getFieldValue(object, name)); | ||
}, context); | ||
}; | ||
// This property will be overridden as true by individual Def instances | ||
// when they are finalized. | ||
Object.defineProperty(Dp, "finalized", {value: false}); | ||
Dp.finalize = function () { | ||
var self = this; | ||
// It's not an error to finalize a type more than once, but only the | ||
// first call to .finalize does anything. | ||
if (!self.finalized) { | ||
var allFields = self.allFields; | ||
var allSupertypes = self.allSupertypes; | ||
self.baseNames.forEach(function (name) { | ||
var def = defCache[name]; | ||
if (def instanceof Def) { | ||
def.finalize(); | ||
extend(allFields, def.allFields); | ||
extend(allSupertypes, def.allSupertypes); | ||
} else { | ||
var message = "unknown supertype name " + | ||
JSON.stringify(name) + | ||
" for subtype " + | ||
JSON.stringify(self.typeName); | ||
throw new Error(message); | ||
} | ||
}); | ||
// TODO Warn if fields are overridden with incompatible types. | ||
extend(allFields, self.ownFields); | ||
allSupertypes[self.typeName] = self; | ||
self.fieldNames.length = 0; | ||
for (var fieldName in allFields) { | ||
if (hasOwn.call(allFields, fieldName) && | ||
!allFields[fieldName].hidden) { | ||
self.fieldNames.push(fieldName); | ||
} | ||
} | ||
// Types are exported only once they have been finalized. | ||
Object.defineProperty(namedTypes, self.typeName, { | ||
enumerable: true, | ||
value: self.type | ||
}); | ||
Object.defineProperty(self, "finalized", {value: true}); | ||
// A linearization of the inheritance hierarchy. | ||
populateSupertypeList(self.typeName, self.supertypeList); | ||
if (self.buildable && self.supertypeList.lastIndexOf("Expression") >= 0) { | ||
wrapExpressionBuilderWithStatement(self.typeName); | ||
} | ||
} | ||
}; | ||
} | ||
// Adds an additional builder for Expression subtypes | ||
@@ -818,23 +711,30 @@ // that wraps the built Expression in an ExpressionStatements. | ||
var wrapperName = getStatementBuilderName(typeName); | ||
// skip if the builder already exists | ||
if (builders[wrapperName]) return; | ||
if (builders[wrapperName]) | ||
return; | ||
// the builder function to wrap with builders.ExpressionStatement | ||
var wrapped = builders[getBuilderName(typeName)]; | ||
// skip if there is nothing to wrap | ||
if (!wrapped) return; | ||
builders[wrapperName] = function () { | ||
return builders.expressionStatement(wrapped.apply(builders, arguments)); | ||
if (!wrapped) | ||
return; | ||
var builder = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return builders.expressionStatement(wrapped.apply(builders, args)); | ||
}; | ||
builder.from = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return builders.expressionStatement(wrapped.from.apply(builders, args)); | ||
}; | ||
builders[wrapperName] = builder; | ||
} | ||
function populateSupertypeList(typeName, list) { | ||
list.length = 0; | ||
list.push(typeName); | ||
var lastSeen = Object.create(null); | ||
for (var pos = 0; pos < list.length; ++pos) { | ||
@@ -846,3 +746,2 @@ typeName = list[pos]; | ||
} | ||
// If we saw typeName earlier in the breadth-first traversal, | ||
@@ -853,10 +752,7 @@ // delete the last-seen occurrence. | ||
} | ||
// Record the new index of the last-seen occurrence of typeName. | ||
lastSeen[typeName] = pos; | ||
// Enqueue the base names of this type. | ||
list.push.apply(list, d.baseNames); | ||
} | ||
// Compaction loop to remove array holes. | ||
@@ -868,6 +764,4 @@ for (var to = 0, from = to, len = list.length; from < len; ++from) { | ||
} | ||
list.length = to; | ||
} | ||
function extend(into, from) { | ||
@@ -877,13 +771,27 @@ Object.keys(from).forEach(function (name) { | ||
}); | ||
return into; | ||
}; | ||
exports.finalize = function () { | ||
} | ||
function finalize() { | ||
Object.keys(defCache).forEach(function (name) { | ||
defCache[name].finalize(); | ||
}); | ||
} | ||
return { | ||
Type: Type, | ||
builtInTypes: builtInTypes, | ||
getSupertypeNames: getSupertypeNames, | ||
computeSupertypeLookupTable: computeSupertypeLookupTable, | ||
builders: builders, | ||
defineMethod: defineMethod, | ||
getBuilderName: getBuilderName, | ||
getStatementBuilderName: getStatementBuilderName, | ||
namedTypes: namedTypes, | ||
getFieldNames: getFieldNames, | ||
getFieldValue: getFieldValue, | ||
eachField: eachField, | ||
someField: someField, | ||
finalize: finalize, | ||
}; | ||
return exports; | ||
}; | ||
} | ||
exports.default = typesPlugin; | ||
; |
55
main.js
@@ -1,18 +0,37 @@ | ||
module.exports = require('./fork')([ | ||
// This core module of AST types captures ES5 as it is parsed today by | ||
// git://github.com/ariya/esprima.git#master. | ||
require("./def/core"), | ||
// Feel free to add to or remove from this list of extension modules to | ||
// configure the precise type hierarchy that you need. | ||
require("./def/es6"), | ||
require("./def/es7"), | ||
require("./def/mozilla"), | ||
require("./def/e4x"), | ||
require("./def/jsx"), | ||
require("./def/flow"), | ||
require("./def/esprima"), | ||
require("./def/babel"), | ||
require("./def/typescript"), | ||
require("./def/es-proposals") | ||
]); | ||
"use strict";; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fork_1 = __importDefault(require("./fork")); | ||
var core_1 = __importDefault(require("./def/core")); | ||
var es6_1 = __importDefault(require("./def/es6")); | ||
var es7_1 = __importDefault(require("./def/es7")); | ||
var mozilla_1 = __importDefault(require("./def/mozilla")); | ||
var e4x_1 = __importDefault(require("./def/e4x")); | ||
var jsx_1 = __importDefault(require("./def/jsx")); | ||
var flow_1 = __importDefault(require("./def/flow")); | ||
var esprima_1 = __importDefault(require("./def/esprima")); | ||
var babel_1 = __importDefault(require("./def/babel")); | ||
var typescript_1 = __importDefault(require("./def/typescript")); | ||
var es_proposals_1 = __importDefault(require("./def/es-proposals")); | ||
var defs = [ | ||
// This core module of AST types captures ES5 as it is parsed today by | ||
// git://github.com/ariya/esprima.git#master. | ||
core_1.default, | ||
// Feel free to add to or remove from this list of extension modules to | ||
// configure the precise type hierarchy that you need. | ||
es6_1.default, | ||
es7_1.default, | ||
mozilla_1.default, | ||
e4x_1.default, | ||
jsx_1.default, | ||
flow_1.default, | ||
esprima_1.default, | ||
babel_1.default, | ||
typescript_1.default, | ||
es_proposals_1.default, | ||
]; | ||
var main = fork_1.default(defs); | ||
exports.default = main; | ||
module.exports = exports["default"]; |
{ | ||
"author": "Ben Newman <bn@cs.stanford.edu>", | ||
"name": "ast-types", | ||
"version": "0.11.7", | ||
"version": "0.12.0", | ||
"description": "Esprima-compatible implementation of the Mozilla JS Parser API", | ||
@@ -29,9 +29,21 @@ "keywords": [ | ||
"main": "main.js", | ||
"types": "main.d.ts", | ||
"scripts": { | ||
"test": "test/run.sh" | ||
"gen": "ts-node --transpile-only script/gen-types.ts", | ||
"tsc": "tsc --noEmit", | ||
"mocha": "test/run.sh", | ||
"test": "npm run gen && npm run tsc && npm run mocha", | ||
"clean": "ts-emit-clean", | ||
"build": "tsc && ts-add-module-exports", | ||
"prepack": "npm run clean && npm run gen && npm run build", | ||
"postpack": "npm run clean" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-types": "^6.26.0", | ||
"babylon": "^7.0.0-beta.40", | ||
"@babel/parser": "^7.1.6", | ||
"@babel/types": "^7.1.6", | ||
"@types/esprima": "^4.0.2", | ||
"@types/glob": "^7.1.1", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.9", | ||
"espree": "^4.0.0", | ||
@@ -43,3 +55,8 @@ "esprima": "~4.0.0", | ||
"mocha": "^5.0.0", | ||
"reify": "^0.18.0" | ||
"recast": "^0.16.1", | ||
"reify": "^0.18.1", | ||
"ts-add-module-exports": "^1.0.0", | ||
"ts-emit-clean": "^1.0.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.2.2" | ||
}, | ||
@@ -46,0 +63,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
440222
65
9650
18
1