diff --git a/docs/types.md b/docs/types.md index e4b93e862d..32b1057e93 100644 --- a/docs/types.md +++ b/docs/types.md @@ -11,58 +11,103 @@ npm install --save-dev @babel/types ``` ## API -### anyTypeAnnotation + +### AnyTypeAnnotation ```javascript t.anyTypeAnnotation() ``` See also `t.isAnyTypeAnnotation(node, opts)` and `t.assertAnyTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.anyTypeAnnotation(); +const { code } = generate(ast); +console.log(code); // any +``` --- -### arrayExpression +### ArgumentPlaceholder ```javascript -t.arrayExpression(elements) +t.argumentPlaceholder() ``` -See also `t.isArrayExpression(node, opts)` and `t.assertArrayExpression(node, opts)`. +See also `t.isArgumentPlaceholder(node, opts)` and `t.assertArgumentPlaceholder(node, opts)`. -Aliases: `Expression` +Aliases: none - - `elements`: `Array` (default: `[]`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.argumentPlaceholder(); + +const { code } = generate(ast); +console.log(code); // ? +``` --- -### argumentPlaceholder +### ArrayExpression ```javascript -t.argumentPlaceholder() +t.arrayExpression() ``` -See also `t.isArgumentPlaceholder(node, opts)` and `t.assertArgumentPlaceholder(node, opts)`. +See also `t.isArrayExpression(node, opts)` and `t.assertArrayExpression(node, opts)`. -Aliases: none +Aliases: `Expression` + + - `elements`: `Array` (default: `[]`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.arrayExpression(); + +const { code } = generate(ast); +console.log(code); // [] +``` --- -### arrayPattern +### ArrayPattern ```javascript t.arrayPattern(elements) ``` See also `t.isArrayPattern(node, opts)` and `t.assertArrayPattern(node, opts)`. -Aliases: `Pattern`, `PatternLike`, `LVal` +Aliases: `LVal`, `Pattern`, `PatternLike` - `elements`: `Array` (required) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const elements = [ + t.identifier("foo"), + t.identifier("bar"), + t.identifier("baz"), +]; +const ast = t.arrayPattern(elements); + +const { code } = generate(ast); +console.log(code); // [foo, bar, baz] +``` + --- -### arrayTypeAnnotation +### ArrayTypeAnnotation ```javascript t.arrayTypeAnnotation(elementType) ``` @@ -73,28 +118,39 @@ Aliases: `Flow`, `FlowType` - `elementType`: `FlowType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const elementType = t.anyTypeAnnotation(); +const ast = t.arrayTypeAnnotation(elementType); + +const { code } = generate(ast); +console.log(code); // any[] +``` + --- -### arrowFunctionExpression +### ArrowFunctionExpression ```javascript -t.arrowFunctionExpression(params, body, async) +t.arrowFunctionExpression(params, body) ``` See also `t.isArrowFunctionExpression(node, opts)` and `t.assertArrowFunctionExpression(node, opts)`. -Aliases: `Scopable`, `Function`, `BlockParent`, `FunctionParent`, `Expression`, `Pureish` +Aliases: `BlockParent`, `Expression`, `Function`, `FunctionParent`, `Pureish`, `Scopable` - - `params`: `Array` (required) + - `params`: `Array` (required) - `body`: `BlockStatement | Expression` (required) - `async`: `boolean` (default: `false`) - - `expression`: `boolean` (default: `null`) + - `expression`: `boolean` (default: `false`) - `generator`: `boolean` (default: `false`) - `returnType`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) --- -### assignmentExpression +### AssignmentExpression ```javascript t.assignmentExpression(operator, left, right) ``` @@ -107,25 +163,50 @@ Aliases: `Expression` - `left`: `LVal` (required) - `right`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const operator = "="; +const left = t.identifier("foo"); +const right = t.identifier("bar"); +const ast = t.assignmentExpression(operator, left, right); + +const { code } = generate(ast); +console.log(code); // foo = bar +``` + --- -### assignmentPattern +### AssignmentPattern ```javascript t.assignmentPattern(left, right) ``` See also `t.isAssignmentPattern(node, opts)` and `t.assertAssignmentPattern(node, opts)`. -Aliases: `Pattern`, `PatternLike`, `LVal` +Aliases: `LVal`, `Pattern`, `PatternLike` - `left`: `Identifier | ObjectPattern | ArrayPattern` (required) - `right`: `Expression` (required) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const left = t.identifier("foo"); +const right = t.identifier("bar"); +const ast = t.assignmentPattern(left, right); + +const { code } = generate(ast); +console.log(code); // foo = bar +``` + --- -### awaitExpression +### AwaitExpression ```javascript t.awaitExpression(argument) ``` @@ -136,22 +217,44 @@ Aliases: `Expression`, `Terminatorless` - `argument`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.identifier("foo"); +const ast = t.awaitExpression(argument); + +const { code } = generate(ast); +console.log(code); // await foo +``` + --- -### bigIntLiteral +### BigIntLiteral ```javascript t.bigIntLiteral(value) ``` See also `t.isBigIntLiteral(node, opts)` and `t.assertBigIntLiteral(node, opts)`. -Aliases: `Expression`, `Pureish`, `Literal`, `Immutable` +Aliases: `Expression`, `Immutable`, `Literal`, `Pureish` - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.bigIntLiteral(value); + +const { code } = generate(ast); +console.log(code); // 0 +``` + --- -### binaryExpression +### BinaryExpression ```javascript t.binaryExpression(operator, left, right) ``` @@ -164,9 +267,22 @@ Aliases: `Binary`, `Expression` - `left`: `Expression` (required) - `right`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const operator = "+"; +const left = t.identifier("foo"); +const right = t.identifier("bar"); +const ast = t.binaryExpression(operator, left, right); + +const { code } = generate(ast); +console.log(code); // foo + bar +``` + --- -### bindExpression +### BindExpression ```javascript t.bindExpression(object, callee) ``` @@ -175,39 +291,76 @@ See also `t.isBindExpression(node, opts)` and `t.assertBindExpression(node, opts Aliases: `Expression` - - `object` (required) - - `callee` (required) + - `object`: `any` (required) + - `callee`: `any` (required) --- -### blockStatement +### BlockStatement ```javascript -t.blockStatement(body, directives) +t.blockStatement(body) ``` See also `t.isBlockStatement(node, opts)` and `t.assertBlockStatement(node, opts)`. -Aliases: `Scopable`, `BlockParent`, `Block`, `Statement` +Aliases: `Block`, `BlockParent`, `Scopable`, `Statement` - `body`: `Array` (required) - `directives`: `Array` (default: `[]`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const body = [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), + ]), +]; +const ast = t.blockStatement(body); + +const { code } = generate(ast); +console.log(code); +// { +// const foo = bar; +// const baz = qux; +// const quux = corge; +// } +``` + --- -### booleanLiteral +### BooleanLiteral ```javascript t.booleanLiteral(value) ``` See also `t.isBooleanLiteral(node, opts)` and `t.assertBooleanLiteral(node, opts)`. -Aliases: `Expression`, `Pureish`, `Literal`, `Immutable` +Aliases: `Expression`, `Immutable`, `Literal`, `Pureish` - `value`: `boolean` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = true; +const ast = t.booleanLiteral(value); + +const { code } = generate(ast); +console.log(code); // true +``` + --- -### booleanLiteralTypeAnnotation +### BooleanLiteralTypeAnnotation ```javascript t.booleanLiteralTypeAnnotation(value) ``` @@ -218,34 +371,64 @@ Aliases: `Flow`, `FlowType` - `value`: `boolean` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = true; +const ast = t.booleanLiteralTypeAnnotation(value); + +const { code } = generate(ast); +console.log(code); // true +``` + --- -### booleanTypeAnnotation +### BooleanTypeAnnotation ```javascript t.booleanTypeAnnotation() ``` See also `t.isBooleanTypeAnnotation(node, opts)` and `t.assertBooleanTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.booleanTypeAnnotation(); +const { code } = generate(ast); +console.log(code); // boolean +``` --- -### breakStatement +### BreakStatement ```javascript -t.breakStatement(label) +t.breakStatement() ``` See also `t.isBreakStatement(node, opts)` and `t.assertBreakStatement(node, opts)`. -Aliases: `Statement`, `Terminatorless`, `CompletionStatement` +Aliases: `CompletionStatement`, `Statement`, `Terminatorless` - `label`: `Identifier` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.breakStatement(); + +const { code } = generate(ast); +console.log(code); // break; +``` + --- -### callExpression +### CallExpression ```javascript t.callExpression(callee, arguments) ``` @@ -255,83 +438,160 @@ See also `t.isCallExpression(node, opts)` and `t.assertCallExpression(node, opts Aliases: `Expression` - `callee`: `Expression` (required) - - `arguments`: `Array` (required) + - `arguments`: `Array` (required) - `optional`: `true | false` (default: `null`) - `typeArguments`: `TypeParameterInstantiation` (default: `null`) - `typeParameters`: `TSTypeParameterInstantiation` (default: `null`) --- -### catchClause +### CatchClause ```javascript t.catchClause(param, body) ``` See also `t.isCatchClause(node, opts)` and `t.assertCatchClause(node, opts)`. -Aliases: `Scopable`, `BlockParent` +Aliases: `BlockParent`, `Scopable` - - `param`: `Identifier` (default: `null`) + - `param`: `Identifier` (required) - `body`: `BlockStatement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const param = t.identifier("foo"); +const body = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.catchClause(param, body); + +const { code } = generate(ast); +console.log(code); +// catch (foo) { +// return this; +// } +``` + --- -### classBody +### ClassBody ```javascript t.classBody(body) ``` See also `t.isClassBody(node, opts)` and `t.assertClassBody(node, opts)`. - - `body`: `Array` (required) +Aliases: none + + - `body`: `Array` (required) --- -### classDeclaration +### ClassDeclaration ```javascript -t.classDeclaration(id, superClass, body, decorators) +t.classDeclaration(id, superClass, body) ``` See also `t.isClassDeclaration(node, opts)` and `t.assertClassDeclaration(node, opts)`. -Aliases: `Scopable`, `Class`, `Statement`, `Declaration`, `Pureish` +Aliases: `Class`, `Declaration`, `Pureish`, `Scopable`, `Statement` - - `id`: `Identifier` (default: `null`) - - `superClass`: `Expression` (default: `null`) + - `id`: `Identifier` (required) + - `superClass`: `Expression` (required) - `body`: `ClassBody` (required) - - `decorators`: `Array` (default: `null`) - - `abstract`: `boolean` (default: `null`) - - `declare`: `boolean` (default: `null`) - - `implements`: `Array` (default: `null`) - - `mixins` (default: `null`) + - `decorators`: `Array` (default: `[]`) + - `abstract`: `boolean` (default: `false`) + - `declare`: `boolean` (default: `false`) + - `implements`: `Array` (default: `[]`) + - `mixins`: `any` (default: `null`) - `superTypeParameters`: `TypeParameterInstantiation | TSTypeParameterInstantiation` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const superClass = t.identifier("bar"); +const body = t.classBody([ + t.classMethod( + "method", + t.identifier("baz"), + [t.identifier("qux"), t.identifier("quux")], + t.blockStatement([ + t.returnStatement( + t.binaryExpression(">", t.identifier("qux"), t.identifier("quux")) + ), + ]) + ), +]); +const ast = t.classDeclaration(id, superClass, body); + +const { code } = generate(ast); +console.log(code); +// class foo extends bar { +// baz(qux, quux) { +// return qux > quux; +// } +// +// } +``` + --- -### classExpression +### ClassExpression ```javascript -t.classExpression(id, superClass, body, decorators) +t.classExpression(id, superClass, body) ``` See also `t.isClassExpression(node, opts)` and `t.assertClassExpression(node, opts)`. -Aliases: `Scopable`, `Class`, `Expression`, `Pureish` +Aliases: `Class`, `Expression`, `Pureish`, `Scopable` - - `id`: `Identifier` (default: `null`) - - `superClass`: `Expression` (default: `null`) + - `id`: `Identifier` (required) + - `superClass`: `Expression` (required) - `body`: `ClassBody` (required) - - `decorators`: `Array` (default: `null`) - - `implements`: `Array` (default: `null`) - - `mixins` (default: `null`) + - `decorators`: `Array` (default: `[]`) + - `implements`: `Array` (default: `[]`) + - `mixins`: `any` (default: `null`) - `superTypeParameters`: `TypeParameterInstantiation | TSTypeParameterInstantiation` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const superClass = t.identifier("bar"); +const body = t.classBody([ + t.classMethod( + "method", + t.identifier("baz"), + [t.identifier("qux"), t.identifier("quux")], + t.blockStatement([ + t.returnStatement( + t.binaryExpression(">", t.identifier("qux"), t.identifier("quux")) + ), + ]) + ), +]); +const ast = t.classExpression(id, superClass, body); + +const { code } = generate(ast); +console.log(code); +// class foo extends bar { +// baz(qux, quux) { +// return qux > quux; +// } +// +// } +``` + --- -### classImplements +### ClassImplements ```javascript -t.classImplements(id, typeParameters) +t.classImplements(id) ``` See also `t.isClassImplements(node, opts)` and `t.assertClassImplements(node, opts)`. @@ -341,52 +601,101 @@ Aliases: `Flow` - `id`: `Identifier` (required) - `typeParameters`: `TypeParameterInstantiation` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.classImplements(id); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### classMethod +### ClassMethod ```javascript -t.classMethod(kind, key, params, body, computed, static) +t.classMethod(kind, key, params, body) ``` See also `t.isClassMethod(node, opts)` and `t.assertClassMethod(node, opts)`. -Aliases: `Function`, `Scopable`, `BlockParent`, `FunctionParent`, `Method` +Aliases: `BlockParent`, `Function`, `FunctionParent`, `Method`, `Scopable` - - `kind`: `"get" | "set" | "method" | "constructor"` (default: `'method'`) + - `kind`: `"get" | "set" | "method" | "constructor"` (default: `method`) - `key`: if computed then `Expression` else `Identifier | Literal` (required) - - `params`: `Array` (required) + - `params`: `Array` (required) - `body`: `BlockStatement` (required) - `computed`: `boolean` (default: `false`) - - `static`: `boolean` (default: `null`) - - `abstract`: `boolean` (default: `null`) + - `static`: `boolean` (default: `false`) + - `abstract`: `boolean` (default: `false`) - `access`: `"public" | "private" | "protected"` (default: `null`) - `accessibility`: `"public" | "private" | "protected"` (default: `null`) - `async`: `boolean` (default: `false`) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `generator`: `boolean` (default: `false`) - - `optional`: `boolean` (default: `null`) + - `optional`: `boolean` (default: `false`) - `returnType`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) --- -### classPrivateProperty +### ClassPrivateMethod ```javascript -t.classPrivateProperty(key, value) +t.classPrivateMethod(kind, key, params, body) +``` + +See also `t.isClassPrivateMethod(node, opts)` and `t.assertClassPrivateMethod(node, opts)`. + +Aliases: `BlockParent`, `Function`, `FunctionParent`, `Method`, `Private`, `Scopable` + + - `kind`: `"get" | "set" | "method" | "constructor"` (required) + - `key`: `PrivateName` (required) + - `params`: `Array` (required) + - `body`: `BlockStatement` (required) + - `static`: `boolean` (default: `false`) + - `abstract`: `boolean` (default: `false`) + - `access`: `"public" | "private" | "protected"` (default: `null`) + - `accessibility`: `"public" | "private" | "protected"` (default: `null`) + - `async`: `boolean` (default: `false`) + - `computed`: `boolean` (default: `false`) + - `decorators`: `Array` (default: `[]`) + - `generator`: `boolean` (default: `false`) + - `optional`: `boolean` (default: `false`) + - `returnType`: `any` (default: `null`) + - `typeParameters`: `any` (default: `null`) + +--- + +### ClassPrivateProperty +```javascript +t.classPrivateProperty(key) ``` See also `t.isClassPrivateProperty(node, opts)` and `t.assertClassPrivateProperty(node, opts)`. -Aliases: `Property`, `Private` +Aliases: `Private`, `Property` - `key`: `PrivateName` (required) - `value`: `Expression` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const key = t.privateName(t.identifier("foo")); +const ast = t.classPrivateProperty(key); + +const { code } = generate(ast); +console.log(code); // #foo; +``` + --- -### classProperty +### ClassProperty ```javascript -t.classProperty(key, value, typeAnnotation, decorators, computed, static) +t.classProperty(key) ``` See also `t.isClassProperty(node, opts)` and `t.assertClassProperty(node, opts)`. @@ -396,46 +705,80 @@ Aliases: `Property` - `key`: `Identifier | StringLiteral | NumericLiteral | Expression` (required) - `value`: `Expression` (default: `null`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `computed`: `boolean` (default: `false`) - - `abstract`: `boolean` (default: `null`) + - `abstract`: `boolean` (default: `false`) - `accessibility`: `"public" | "private" | "protected"` (default: `null`) - - `definite`: `boolean` (default: `null`) - - `optional`: `boolean` (default: `null`) - - `readonly`: `boolean` (default: `null`) - - `static`: `boolean` (default: `null`) + - `definite`: `boolean` (default: `false`) + - `optional`: `boolean` (default: `false`) + - `readonly`: `boolean` (default: `false`) + - `static`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const key = t.identifier("foo"); +const ast = t.classProperty(key); + +const { code } = generate(ast); +console.log(code); // foo; +``` --- -### conditionalExpression +### ConditionalExpression ```javascript t.conditionalExpression(test, consequent, alternate) ``` See also `t.isConditionalExpression(node, opts)` and `t.assertConditionalExpression(node, opts)`. -Aliases: `Expression`, `Conditional` +Aliases: `Conditional`, `Expression` - `test`: `Expression` (required) - `consequent`: `Expression` (required) - `alternate`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const test = t.identifier("foo"); +const consequent = t.identifier("bar"); +const alternate = t.identifier("baz"); +const ast = t.conditionalExpression(test, consequent, alternate); + +const { code } = generate(ast); +console.log(code); // foo ? bar : baz +``` + --- -### continueStatement +### ContinueStatement ```javascript -t.continueStatement(label) +t.continueStatement() ``` See also `t.isContinueStatement(node, opts)` and `t.assertContinueStatement(node, opts)`. -Aliases: `Statement`, `Terminatorless`, `CompletionStatement` +Aliases: `CompletionStatement`, `Statement`, `Terminatorless` - `label`: `Identifier` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.continueStatement(); + +const { code } = generate(ast); +console.log(code); // continue; +``` + --- -### debuggerStatement +### DebuggerStatement ```javascript t.debuggerStatement() ``` @@ -444,207 +787,325 @@ See also `t.isDebuggerStatement(node, opts)` and `t.assertDebuggerStatement(node Aliases: `Statement` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.debuggerStatement(); + +const { code } = generate(ast); +console.log(code); // debugger; +``` --- -### declareClass +### DeclareClass ```javascript t.declareClass(id, typeParameters, extends, body) ``` See also `t.isDeclareClass(node, opts)` and `t.assertDeclareClass(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterInstantiation` (default: `null`) - - `extends`: `Array` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) + - `extends`: `Array` (required) - `body`: `ObjectTypeAnnotation` (required) - - `implements`: `Array` (default: `null`) - - `mixins`: `Array` (default: `null`) + - `implements`: `Array` (default: `[]`) + - `mixins`: `Array` (default: `[]`) + +--- + +### DeclaredPredicate +```javascript +t.declaredPredicate(value) +``` + +See also `t.isDeclaredPredicate(node, opts)` and `t.assertDeclaredPredicate(node, opts)`. + +Aliases: `Flow`, `FlowPredicate` + + - `value`: `Flow` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = t.anyTypeAnnotation(); +const ast = t.declaredPredicate(value); + +const { code } = generate(ast); +console.log(code); // %checks(any) +``` --- -### declareExportAllDeclaration +### DeclareExportAllDeclaration ```javascript t.declareExportAllDeclaration(source) ``` See also `t.isDeclareExportAllDeclaration(node, opts)` and `t.assertDeclareExportAllDeclaration(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `source`: `StringLiteral` (required) - - `exportKind`: `["type","value"]` (default: `null`) + - `exportKind`: `"type" | "value"` (default: `null`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const source = t.stringLiteral("foo"); +const ast = t.declareExportAllDeclaration(source); + +const { code } = generate(ast); +console.log(code); // declare export * from "foo"; +``` --- -### declareExportDeclaration +### DeclareExportDeclaration ```javascript -t.declareExportDeclaration(declaration, specifiers, source) +t.declareExportDeclaration() ``` See also `t.isDeclareExportDeclaration(node, opts)` and `t.assertDeclareExportDeclaration(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `declaration`: `Flow` (default: `null`) - - `specifiers`: `Array` (default: `null`) + - `specifiers`: `Array` (default: `[]`) - `source`: `StringLiteral` (default: `null`) - - `default`: `boolean` (default: `null`) + - `default`: `boolean` (default: `false`) --- -### declareFunction +### DeclareFunction ```javascript t.declareFunction(id) ``` See also `t.isDeclareFunction(node, opts)` and `t.assertDeclareFunction(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - `predicate`: `DeclaredPredicate` (default: `null`) --- -### declareInterface +### DeclareInterface ```javascript t.declareInterface(id, typeParameters, extends, body) ``` See also `t.isDeclareInterface(node, opts)` and `t.assertDeclareInterface(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) - - `extends`: `Array` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) + - `extends`: `Array` (required) - `body`: `ObjectTypeAnnotation` (required) - - `implements`: `Array` (default: `null`) - - `mixins`: `Array` (default: `null`) + - `implements`: `Array` (default: `[]`) + - `mixins`: `Array` (default: `[]`) --- -### declareModule +### DeclareModule ```javascript -t.declareModule(id, body, kind) +t.declareModule(id, body) ``` See also `t.isDeclareModule(node, opts)` and `t.assertDeclareModule(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier | StringLiteral` (required) - `body`: `BlockStatement` (required) - `kind`: `"CommonJS" | "ES"` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const body = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.declareModule(id, body); + +const { code } = generate(ast); +console.log(code); +// declare module foo { +// return this; +// } +``` + --- -### declareModuleExports +### DeclareModuleExports ```javascript t.declareModuleExports(typeAnnotation) ``` See also `t.isDeclareModuleExports(node, opts)` and `t.assertDeclareModuleExports(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `typeAnnotation`: `TypeAnnotation` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.typeAnnotation(t.anyTypeAnnotation()); +const ast = t.declareModuleExports(typeAnnotation); + +const { code } = generate(ast); +console.log(code); // declare module.exports: any +``` + --- -### declareOpaqueType +### DeclareOpaqueType ```javascript -t.declareOpaqueType(id, typeParameters, supertype) +t.declareOpaqueType(id) ``` See also `t.isDeclareOpaqueType(node, opts)` and `t.assertDeclareOpaqueType(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - `typeParameters`: `TypeParameterDeclaration` (default: `null`) - `supertype`: `FlowType` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.declareOpaqueType(id); + +const { code } = generate(ast); +console.log(code); // declare opaque type foo; +``` + --- -### declareTypeAlias +### DeclareTypeAlias ```javascript t.declareTypeAlias(id, typeParameters, right) ``` See also `t.isDeclareTypeAlias(node, opts)` and `t.assertDeclareTypeAlias(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) - `right`: `FlowType` (required) --- -### declareVariable +### DeclareVariable ```javascript t.declareVariable(id) ``` See also `t.isDeclareVariable(node, opts)` and `t.assertDeclareVariable(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) ---- - -### declaredPredicate ```javascript -t.declaredPredicate(value) -``` - -See also `t.isDeclaredPredicate(node, opts)` and `t.assertDeclaredPredicate(node, opts)`. +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -Aliases: `Flow`, `FlowPredicate` +const id = t.identifier("foo"); +const ast = t.declareVariable(id); - - `value`: `Flow` (required) +const { code } = generate(ast); +console.log(code); // declare var foo; +``` --- -### decorator +### Decorator ```javascript t.decorator(expression) ``` See also `t.isDecorator(node, opts)` and `t.assertDecorator(node, opts)`. +Aliases: none + - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.decorator(expression); + +const { code } = generate(ast); +console.log(code); // @foo +``` + --- -### directive +### Directive ```javascript t.directive(value) ``` See also `t.isDirective(node, opts)` and `t.assertDirective(node, opts)`. +Aliases: none + - `value`: `DirectiveLiteral` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = t.directiveLiteral("foo"); +const ast = t.directive(value); + +const { code } = generate(ast); +console.log(code); // "foo"; +``` + --- -### directiveLiteral +### DirectiveLiteral ```javascript t.directiveLiteral(value) ``` See also `t.isDirectiveLiteral(node, opts)` and `t.assertDirectiveLiteral(node, opts)`. +Aliases: none + - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.directiveLiteral(value); + +const { code } = generate(ast); +console.log(code); // "0" +``` + --- -### doExpression +### DoExpression ```javascript t.doExpression(body) ``` @@ -655,23 +1116,51 @@ Aliases: `Expression` - `body`: `BlockStatement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const body = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.doExpression(body); + +const { code } = generate(ast); +console.log(code); +// do { +// return this; +// } +``` + --- -### doWhileStatement +### DoWhileStatement ```javascript t.doWhileStatement(test, body) ``` See also `t.isDoWhileStatement(node, opts)` and `t.assertDoWhileStatement(node, opts)`. -Aliases: `Statement`, `BlockParent`, `Loop`, `While`, `Scopable` +Aliases: `BlockParent`, `Loop`, `Scopable`, `Statement`, `While` - `test`: `Expression` (required) - `body`: `Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const test = t.identifier("foo"); +const body = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("bar"), t.identifier("baz")), +]); +const ast = t.doWhileStatement(test, body); + +const { code } = generate(ast); +console.log(code); // do const bar = baz; while (foo); +``` + --- -### emptyStatement +### EmptyStatement ```javascript t.emptyStatement() ``` @@ -680,22 +1169,40 @@ See also `t.isEmptyStatement(node, opts)` and `t.assertEmptyStatement(node, opts Aliases: `Statement` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.emptyStatement(); + +const { code } = generate(ast); +console.log(code); // ; +``` --- -### emptyTypeAnnotation +### EmptyTypeAnnotation ```javascript t.emptyTypeAnnotation() ``` See also `t.isEmptyTypeAnnotation(node, opts)` and `t.assertEmptyTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.emptyTypeAnnotation(); +const { code } = generate(ast); +console.log(code); // empty +``` --- -### existsTypeAnnotation +### ExistsTypeAnnotation ```javascript t.existsTypeAnnotation() ``` @@ -704,36 +1211,78 @@ See also `t.isExistsTypeAnnotation(node, opts)` and `t.assertExistsTypeAnnotatio Aliases: `Flow`, `FlowType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.existsTypeAnnotation(); + +const { code } = generate(ast); +console.log(code); // * +``` --- -### exportAllDeclaration +### ExportAllDeclaration ```javascript t.exportAllDeclaration(source) ``` See also `t.isExportAllDeclaration(node, opts)` and `t.assertExportAllDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration`, `ModuleDeclaration`, `ExportDeclaration` +Aliases: `Declaration`, `ExportDeclaration`, `ModuleDeclaration`, `Statement` - `source`: `StringLiteral` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const source = t.stringLiteral("foo"); +const ast = t.exportAllDeclaration(source); + +const { code } = generate(ast); +console.log(code); // export * from "foo"; +``` + --- -### exportDefaultDeclaration +### ExportDefaultDeclaration ```javascript t.exportDefaultDeclaration(declaration) ``` See also `t.isExportDefaultDeclaration(node, opts)` and `t.assertExportDefaultDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration`, `ModuleDeclaration`, `ExportDeclaration` +Aliases: `Declaration`, `ExportDeclaration`, `ModuleDeclaration`, `Statement` - `declaration`: `FunctionDeclaration | TSDeclareFunction | ClassDeclaration | Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const declaration = t.functionDeclaration( + t.identifier("foo"), + [ + t.identifier("bar"), + t.assignmentPattern(t.identifier("baz"), t.stringLiteral("qux")), + t.assignmentPattern(t.identifier("quux"), t.numericLiteral(1337)), + ], + t.blockStatement([t.returnStatement(t.thisExpression())]) +); +const ast = t.exportDefaultDeclaration(declaration); + +const { code } = generate(ast); +console.log(code); +// export default function foo(bar, baz = "qux", quux = 1337) { +// return this; +// } +``` + --- -### exportDefaultSpecifier +### ExportDefaultSpecifier ```javascript t.exportDefaultSpecifier(exported) ``` @@ -744,24 +1293,36 @@ Aliases: `ModuleSpecifier` - `exported`: `Identifier` (required) ---- - -### exportNamedDeclaration ```javascript -t.exportNamedDeclaration(declaration, specifiers, source) -``` +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const exported = t.identifier("foo"); +const ast = t.exportDefaultSpecifier(exported); + +const { code } = generate(ast); +console.log(code); // foo +``` + +--- + +### ExportNamedDeclaration +```javascript +t.exportNamedDeclaration(declaration, specifiers) +``` See also `t.isExportNamedDeclaration(node, opts)` and `t.assertExportNamedDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration`, `ModuleDeclaration`, `ExportDeclaration` +Aliases: `Declaration`, `ExportDeclaration`, `ModuleDeclaration`, `Statement` - - `declaration`: `Declaration` (default: `null`) + - `declaration`: `Declaration` (required) - `specifiers`: `Array` (required) - `source`: `StringLiteral` (default: `null`) + - `exportKind`: `"type" | "value"` (default: `null`) --- -### exportNamespaceSpecifier +### ExportNamespaceSpecifier ```javascript t.exportNamespaceSpecifier(exported) ``` @@ -772,9 +1333,20 @@ Aliases: `ModuleSpecifier` - `exported`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const exported = t.identifier("foo"); +const ast = t.exportNamespaceSpecifier(exported); + +const { code } = generate(ast); +console.log(code); // * as foo +``` + --- -### exportSpecifier +### ExportSpecifier ```javascript t.exportSpecifier(local, exported) ``` @@ -786,112 +1358,215 @@ Aliases: `ModuleSpecifier` - `local`: `Identifier` (required) - `exported`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const local = t.identifier("foo"); +const exported = t.identifier("bar"); +const ast = t.exportSpecifier(local, exported); + +const { code } = generate(ast); +console.log(code); // foo as bar +``` + --- -### expressionStatement +### ExpressionStatement ```javascript t.expressionStatement(expression) ``` See also `t.isExpressionStatement(node, opts)` and `t.assertExpressionStatement(node, opts)`. -Aliases: `Statement`, `ExpressionWrapper` +Aliases: `ExpressionWrapper`, `Statement` - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.expressionStatement(expression); + +const { code } = generate(ast); +console.log(code); // foo; +``` + --- -### file +### File ```javascript t.file(program, comments, tokens) ``` See also `t.isFile(node, opts)` and `t.assertFile(node, opts)`. +Aliases: none + - `program`: `Program` (required) - - `comments` (required) - - `tokens` (required) + - `comments`: `any` (required) + - `tokens`: `any` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const program = t.program([ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), + ]), +]); +const comments = "grault"; +const tokens = "garply"; +const ast = t.file(program, comments, tokens); + +const { code } = generate(ast); +console.log(code); +// const foo = bar; +// const baz = qux; +// const quux = corge; +``` --- -### forInStatement +### ForInStatement ```javascript t.forInStatement(left, right, body) ``` See also `t.isForInStatement(node, opts)` and `t.assertForInStatement(node, opts)`. -Aliases: `Scopable`, `Statement`, `For`, `BlockParent`, `Loop`, `ForXStatement` +Aliases: `BlockParent`, `For`, `ForXStatement`, `Loop`, `Scopable`, `Statement` - `left`: `VariableDeclaration | LVal` (required) - `right`: `Expression` (required) - `body`: `Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const left = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), +]); +const right = t.identifier("baz"); +const body = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("qux"), t.identifier("quux")), +]); +const ast = t.forInStatement(left, right, body); + +const { code } = generate(ast); +console.log(code); // for (const foo = bar in baz) const qux = quux; +``` + --- -### forOfStatement +### ForOfStatement ```javascript t.forOfStatement(left, right, body) ``` See also `t.isForOfStatement(node, opts)` and `t.assertForOfStatement(node, opts)`. -Aliases: `Scopable`, `Statement`, `For`, `BlockParent`, `Loop`, `ForXStatement` +Aliases: `BlockParent`, `For`, `ForXStatement`, `Loop`, `Scopable`, `Statement` - `left`: `VariableDeclaration | LVal` (required) - `right`: `Expression` (required) - `body`: `Statement` (required) - `await`: `boolean` (default: `false`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const left = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), +]); +const right = t.identifier("baz"); +const body = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("qux"), t.identifier("quux")), +]); +const ast = t.forOfStatement(left, right, body); + +const { code } = generate(ast); +console.log(code); // for (const foo = bar of baz) const qux = quux; +``` + --- -### forStatement +### ForStatement ```javascript t.forStatement(init, test, update, body) ``` See also `t.isForStatement(node, opts)` and `t.assertForStatement(node, opts)`. -Aliases: `Scopable`, `Statement`, `For`, `BlockParent`, `Loop` +Aliases: `BlockParent`, `For`, `Loop`, `Scopable`, `Statement` - - `init`: `VariableDeclaration | Expression` (default: `null`) - - `test`: `Expression` (default: `null`) - - `update`: `Expression` (default: `null`) + - `init`: `VariableDeclaration | Expression` (required) + - `test`: `Expression` (required) + - `update`: `Expression` (required) - `body`: `Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const init = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), +]); +const test = t.identifier("baz"); +const update = t.identifier("qux"); +const body = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), +]); +const ast = t.forStatement(init, test, update, body); + +const { code } = generate(ast); +console.log(code); // for (const foo = bar; baz; qux) const quux = corge; +``` + --- -### functionDeclaration +### FunctionDeclaration ```javascript -t.functionDeclaration(id, params, body, generator, async) +t.functionDeclaration(id, params, body) ``` See also `t.isFunctionDeclaration(node, opts)` and `t.assertFunctionDeclaration(node, opts)`. -Aliases: `Scopable`, `Function`, `BlockParent`, `FunctionParent`, `Statement`, `Pureish`, `Declaration` +Aliases: `BlockParent`, `Declaration`, `Function`, `FunctionParent`, `Pureish`, `Scopable`, `Statement` - - `id`: `Identifier` (default: `null`) - - `params`: `Array` (required) + - `id`: `Identifier` (required) + - `params`: `Array` (required) - `body`: `BlockStatement` (required) - `generator`: `boolean` (default: `false`) - `async`: `boolean` (default: `false`) - - `declare`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) - `returnType`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) --- -### functionExpression +### FunctionExpression ```javascript -t.functionExpression(id, params, body, generator, async) +t.functionExpression(id, params, body) ``` See also `t.isFunctionExpression(node, opts)` and `t.assertFunctionExpression(node, opts)`. -Aliases: `Scopable`, `Function`, `BlockParent`, `FunctionParent`, `Expression`, `Pureish` +Aliases: `BlockParent`, `Expression`, `Function`, `FunctionParent`, `Pureish`, `Scopable` - - `id`: `Identifier` (default: `null`) - - `params`: `Array` (required) + - `id`: `Identifier` (required) + - `params`: `Array` (required) - `body`: `BlockStatement` (required) - `generator`: `boolean` (default: `false`) - `async`: `boolean` (default: `false`) @@ -900,7 +1575,7 @@ Aliases: `Scopable`, `Function`, `BlockParent`, `FunctionParent`, `Expression`, --- -### functionTypeAnnotation +### FunctionTypeAnnotation ```javascript t.functionTypeAnnotation(typeParameters, params, rest, returnType) ``` @@ -909,14 +1584,14 @@ See also `t.isFunctionTypeAnnotation(node, opts)` and `t.assertFunctionTypeAnnot Aliases: `Flow`, `FlowType` - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) - `params`: `Array` (required) - - `rest`: `FunctionTypeParam` (default: `null`) + - `rest`: `FunctionTypeParam` (required) - `returnType`: `FlowType` (required) --- -### functionTypeParam +### FunctionTypeParam ```javascript t.functionTypeParam(name, typeAnnotation) ``` @@ -925,58 +1600,106 @@ See also `t.isFunctionTypeParam(node, opts)` and `t.assertFunctionTypeParam(node Aliases: `Flow` - - `name`: `Identifier` (default: `null`) + - `name`: `Identifier` (required) - `typeAnnotation`: `FlowType` (required) - - `optional`: `boolean` (default: `null`) + - `optional`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const name = t.identifier("foo"); +const typeAnnotation = t.anyTypeAnnotation(); +const ast = t.functionTypeParam(name, typeAnnotation); + +const { code } = generate(ast); +console.log(code); // foo: any +``` --- -### genericTypeAnnotation +### GenericTypeAnnotation ```javascript -t.genericTypeAnnotation(id, typeParameters) +t.genericTypeAnnotation(id) ``` See also `t.isGenericTypeAnnotation(node, opts)` and `t.assertGenericTypeAnnotation(node, opts)`. Aliases: `Flow`, `FlowType` - - `id`: `Identifier` (required) + - `id`: `Identifier | QualifiedTypeIdentifier` (required) - `typeParameters`: `TypeParameterInstantiation` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.genericTypeAnnotation(id); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### identifier +### Identifier ```javascript t.identifier(name) ``` See also `t.isIdentifier(node, opts)` and `t.assertIdentifier(node, opts)`. -Aliases: `Expression`, `PatternLike`, `LVal`, `TSEntityName` +Aliases: `Expression`, `LVal`, `PatternLike`, `TSEntityName` - `name`: `string` (required) - - `decorators`: `Array` (default: `null`) - - `optional`: `boolean` (default: `null`) + - `decorators`: `Array` (default: `[]`) + - `optional`: `boolean` (default: `false`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const name = "foo"; +const ast = t.identifier(name); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### ifStatement +### IfStatement ```javascript -t.ifStatement(test, consequent, alternate) +t.ifStatement(test, consequent) ``` See also `t.isIfStatement(node, opts)` and `t.assertIfStatement(node, opts)`. -Aliases: `Statement`, `Conditional` +Aliases: `Conditional`, `Statement` - `test`: `Expression` (required) - `consequent`: `Statement` (required) - `alternate`: `Statement` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const test = t.identifier("foo"); +const consequent = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("bar"), t.identifier("baz")), +]); +const ast = t.ifStatement(test, consequent); + +const { code } = generate(ast); +console.log(code); // if (foo) const bar = baz; +``` + --- -### import +### Import ```javascript t.import() ``` @@ -985,24 +1708,34 @@ See also `t.isImport(node, opts)` and `t.assertImport(node, opts)`. Aliases: `Expression` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.import(); + +const { code } = generate(ast); +console.log(code); // import +``` --- -### importDeclaration +### ImportDeclaration ```javascript t.importDeclaration(specifiers, source) ``` See also `t.isImportDeclaration(node, opts)` and `t.assertImportDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration`, `ModuleDeclaration` +Aliases: `Declaration`, `ModuleDeclaration`, `Statement` - `specifiers`: `Array` (required) - `source`: `StringLiteral` (required) + - `importKind`: `"type" | "typeof" | "value"` (default: `null`) --- -### importDefaultSpecifier +### ImportDefaultSpecifier ```javascript t.importDefaultSpecifier(local) ``` @@ -1013,9 +1746,20 @@ Aliases: `ModuleSpecifier` - `local`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const local = t.identifier("foo"); +const ast = t.importDefaultSpecifier(local); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### importNamespaceSpecifier +### ImportNamespaceSpecifier ```javascript t.importNamespaceSpecifier(local) ``` @@ -1026,9 +1770,20 @@ Aliases: `ModuleSpecifier` - `local`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const local = t.identifier("foo"); +const ast = t.importNamespaceSpecifier(local); + +const { code } = generate(ast); +console.log(code); // * as foo +``` + --- -### importSpecifier +### ImportSpecifier ```javascript t.importSpecifier(local, imported) ``` @@ -1039,11 +1794,23 @@ Aliases: `ModuleSpecifier` - `local`: `Identifier` (required) - `imported`: `Identifier` (required) - - `importKind`: `null | "type" | "typeof"` (default: `null`) + - `importKind`: `"type" | "typeof"` (default: `null`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const local = t.identifier("foo"); +const imported = t.identifier("bar"); +const ast = t.importSpecifier(local, imported); + +const { code } = generate(ast); +console.log(code); // bar as foo +``` --- -### inferredPredicate +### InferredPredicate ```javascript t.inferredPredicate() ``` @@ -1052,42 +1819,62 @@ See also `t.isInferredPredicate(node, opts)` and `t.assertInferredPredicate(node Aliases: `Flow`, `FlowPredicate` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.inferredPredicate(); + +const { code } = generate(ast); +console.log(code); // %checks +``` --- -### interfaceDeclaration +### InterfaceDeclaration ```javascript t.interfaceDeclaration(id, typeParameters, extends, body) ``` See also `t.isInterfaceDeclaration(node, opts)` and `t.assertInterfaceDeclaration(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) - - `extends`: `Array` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) + - `extends`: `Array` (required) - `body`: `ObjectTypeAnnotation` (required) - - `implements`: `Array` (default: `null`) - - `mixins`: `Array` (default: `null`) + - `implements`: `Array` (default: `[]`) + - `mixins`: `Array` (default: `[]`) --- -### interfaceExtends +### InterfaceExtends ```javascript -t.interfaceExtends(id, typeParameters) +t.interfaceExtends(id) ``` See also `t.isInterfaceExtends(node, opts)` and `t.assertInterfaceExtends(node, opts)`. Aliases: `Flow` - - `id`: `Identifier` (required) + - `id`: `Identifier | QualifiedTypeIdentifier` (required) - `typeParameters`: `TypeParameterInstantiation` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.interfaceExtends(id); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### interfaceTypeAnnotation +### InterfaceTypeAnnotation ```javascript t.interfaceTypeAnnotation(extends, body) ``` @@ -1096,23 +1883,57 @@ See also `t.isInterfaceTypeAnnotation(node, opts)` and `t.assertInterfaceTypeAnn Aliases: `Flow`, `FlowType` - - `extends`: `Array` (default: `null`) + - `extends`: `Array` (required) - `body`: `ObjectTypeAnnotation` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const _extends = [ + t.interfaceExtends(t.identifier("foo")), + t.interfaceExtends(t.identifier("bar")), + t.interfaceExtends(t.identifier("baz")), +]; +const body = t.objectTypeAnnotation([ + t.objectTypeProperty(t.identifier("qux"), t.anyTypeAnnotation()), +]); +const ast = t.interfaceTypeAnnotation(_extends, body); + +const { code } = generate(ast); +console.log(code); +// interface extends foo, bar, baz { +// qux: any +// } +``` + --- -### interpreterDirective +### InterpreterDirective ```javascript t.interpreterDirective(value) ``` See also `t.isInterpreterDirective(node, opts)` and `t.assertInterpreterDirective(node, opts)`. +Aliases: none + - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.interpreterDirective(value); + +const { code } = generate(ast); +console.log(code); // #!0 +``` + --- -### intersectionTypeAnnotation +### IntersectionTypeAnnotation ```javascript t.intersectionTypeAnnotation(types) ``` @@ -1123,64 +1944,110 @@ Aliases: `Flow`, `FlowType` - `types`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const types = [ + t.anyTypeAnnotation(), + t.stringTypeAnnotation(), + t.numberTypeAnnotation(), +]; +const ast = t.intersectionTypeAnnotation(types); + +const { code } = generate(ast); +console.log(code); // any & string & number +``` + --- -### jSXAttribute +### JSXAttribute ```javascript -t.jsxAttribute(name, value) +t.jsxAttribute(name) ``` See also `t.isJSXAttribute(node, opts)` and `t.assertJSXAttribute(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - `name`: `JSXIdentifier | JSXNamespacedName` (required) - `value`: `JSXElement | JSXFragment | StringLiteral | JSXExpressionContainer` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const name = t.jsxIdentifier("foo"); +const ast = t.jsxAttribute(name); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### jSXClosingElement +### JSXClosingElement ```javascript t.jsxClosingElement(name) ``` See also `t.isJSXClosingElement(node, opts)` and `t.assertJSXClosingElement(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - `name`: `JSXIdentifier | JSXMemberExpression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const name = t.jsxIdentifier("foo"); +const ast = t.jsxClosingElement(name); + +const { code } = generate(ast); +console.log(code); // +``` + --- -### jSXClosingFragment +### JSXClosingFragment ```javascript t.jsxClosingFragment() ``` See also `t.isJSXClosingFragment(node, opts)` and `t.assertJSXClosingFragment(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.jsxClosingFragment(); +const { code } = generate(ast); +console.log(code); // +``` --- -### jSXElement +### JSXElement ```javascript t.jsxElement(openingElement, closingElement, children, selfClosing) ``` See also `t.isJSXElement(node, opts)` and `t.assertJSXElement(node, opts)`. -Aliases: `JSX`, `Immutable`, `Expression` +Aliases: `Expression`, `Immutable`, `JSX` - `openingElement`: `JSXOpeningElement` (required) - - `closingElement`: `JSXClosingElement` (default: `null`) + - `closingElement`: `JSXClosingElement` (required) - `children`: `Array` (required) - - `selfClosing` (required) + - `selfClosing`: `any` (required) --- -### jSXEmptyExpression +### JSXEmptyExpression ```javascript t.jsxEmptyExpression() ``` @@ -1189,30 +2056,50 @@ See also `t.isJSXEmptyExpression(node, opts)` and `t.assertJSXEmptyExpression(no Aliases: `JSX` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.jsxEmptyExpression(); + +const { code } = generate(ast); +console.log(code); // +``` --- -### jSXExpressionContainer +### JSXExpressionContainer ```javascript t.jsxExpressionContainer(expression) ``` See also `t.isJSXExpressionContainer(node, opts)` and `t.assertJSXExpressionContainer(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - - `expression`: `Expression` (required) + - `expression`: `Expression | JSXEmptyExpression` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.jsxExpressionContainer(expression); + +const { code } = generate(ast); +console.log(code); // {foo} +``` --- -### jSXFragment +### JSXFragment ```javascript t.jsxFragment(openingFragment, closingFragment, children) ``` See also `t.isJSXFragment(node, opts)` and `t.assertJSXFragment(node, opts)`. -Aliases: `JSX`, `Immutable`, `Expression` +Aliases: `Expression`, `Immutable`, `JSX` - `openingFragment`: `JSXOpeningFragment` (required) - `closingFragment`: `JSXClosingFragment` (required) @@ -1220,7 +2107,7 @@ Aliases: `JSX`, `Immutable`, `Expression` --- -### jSXIdentifier +### JSXIdentifier ```javascript t.jsxIdentifier(name) ``` @@ -1231,9 +2118,20 @@ Aliases: `JSX` - `name`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const name = "foo"; +const ast = t.jsxIdentifier(name); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### jSXMemberExpression +### JSXMemberExpression ```javascript t.jsxMemberExpression(object, property) ``` @@ -1245,9 +2143,24 @@ Aliases: `JSX` - `object`: `JSXMemberExpression | JSXIdentifier` (required) - `property`: `JSXIdentifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const object = t.jsxMemberExpression( + t.jsxIdentifier("foo"), + t.jsxIdentifier("bar") +); +const property = t.jsxIdentifier("baz"); +const ast = t.jsxMemberExpression(object, property); + +const { code } = generate(ast); +console.log(code); // foo.bar.baz +``` + --- -### jSXNamespacedName +### JSXNamespacedName ```javascript t.jsxNamespacedName(namespace, name) ``` @@ -1259,36 +2172,58 @@ Aliases: `JSX` - `namespace`: `JSXIdentifier` (required) - `name`: `JSXIdentifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const namespace = t.jsxIdentifier("foo"); +const name = t.jsxIdentifier("bar"); +const ast = t.jsxNamespacedName(namespace, name); + +const { code } = generate(ast); +console.log(code); // foo:bar +``` + --- -### jSXOpeningElement +### JSXOpeningElement ```javascript -t.jsxOpeningElement(name, attributes, selfClosing) +t.jsxOpeningElement(name, attributes) ``` See also `t.isJSXOpeningElement(node, opts)` and `t.assertJSXOpeningElement(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - `name`: `JSXIdentifier | JSXMemberExpression` (required) - `attributes`: `Array` (required) - `selfClosing`: `boolean` (default: `false`) + - `typeParameters`: `TypeParameterInstantiation | TSTypeParameterInstantiation` (default: `null`) --- -### jSXOpeningFragment +### JSXOpeningFragment ```javascript t.jsxOpeningFragment() ``` See also `t.isJSXOpeningFragment(node, opts)` and `t.assertJSXOpeningFragment(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.jsxOpeningFragment(); + +const { code } = generate(ast); +console.log(code); // <> +``` --- -### jSXSpreadAttribute +### JSXSpreadAttribute ```javascript t.jsxSpreadAttribute(argument) ``` @@ -1299,35 +2234,68 @@ Aliases: `JSX` - `argument`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.identifier("foo"); +const ast = t.jsxSpreadAttribute(argument); + +const { code } = generate(ast); +console.log(code); // {...foo} +``` + --- -### jSXSpreadChild +### JSXSpreadChild ```javascript t.jsxSpreadChild(expression) ``` See also `t.isJSXSpreadChild(node, opts)` and `t.assertJSXSpreadChild(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.jsxSpreadChild(expression); + +const { code } = generate(ast); +console.log(code); // {...foo} +``` + --- -### jSXText +### JSXText ```javascript t.jsxText(value) ``` See also `t.isJSXText(node, opts)` and `t.assertJSXText(node, opts)`. -Aliases: `JSX`, `Immutable` +Aliases: `Immutable`, `JSX` - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.jsxText(value); + +const { code } = generate(ast); +console.log(code); // 0 +``` + --- -### labeledStatement +### LabeledStatement ```javascript t.labeledStatement(label, body) ``` @@ -1339,9 +2307,23 @@ Aliases: `Statement` - `label`: `Identifier` (required) - `body`: `Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const label = t.identifier("foo"); +const body = t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("bar"), t.identifier("baz")), +]); +const ast = t.labeledStatement(label, body); + +const { code } = generate(ast); +console.log(code); // foo: const bar = baz; +``` + --- -### logicalExpression +### LogicalExpression ```javascript t.logicalExpression(operator, left, right) ``` @@ -1356,9 +2338,9 @@ Aliases: `Binary`, `Expression` --- -### memberExpression +### MemberExpression ```javascript -t.memberExpression(object, property, computed, optional) +t.memberExpression(object, property) ``` See also `t.isMemberExpression(node, opts)` and `t.assertMemberExpression(node, opts)`. @@ -1370,9 +2352,21 @@ Aliases: `Expression`, `LVal` - `computed`: `boolean` (default: `false`) - `optional`: `true | false` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const object = t.thisExpression(); +const property = t.identifier("foo"); +const ast = t.memberExpression(object, property); + +const { code } = generate(ast); +console.log(code); // this.foo +``` + --- -### metaProperty +### MetaProperty ```javascript t.metaProperty(meta, property) ``` @@ -1384,21 +2378,42 @@ Aliases: `Expression` - `meta`: `Identifier` (required) - `property`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const meta = t.identifier("foo"); +const property = t.identifier("bar"); +const ast = t.metaProperty(meta, property); + +const { code } = generate(ast); +console.log(code); // foo.bar +``` + --- -### mixedTypeAnnotation +### MixedTypeAnnotation ```javascript t.mixedTypeAnnotation() ``` See also `t.isMixedTypeAnnotation(node, opts)` and `t.assertMixedTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.mixedTypeAnnotation(); +const { code } = generate(ast); +console.log(code); // mixed +``` --- -### newExpression +### NewExpression ```javascript t.newExpression(callee, arguments) ``` @@ -1408,61 +2423,101 @@ See also `t.isNewExpression(node, opts)` and `t.assertNewExpression(node, opts)` Aliases: `Expression` - `callee`: `Expression` (required) - - `arguments`: `Array` (required) + - `arguments`: `Array` (required) - `optional`: `true | false` (default: `null`) - `typeArguments`: `TypeParameterInstantiation` (default: `null`) - `typeParameters`: `TSTypeParameterInstantiation` (default: `null`) --- -### noop +### Noop ```javascript t.noop() ``` See also `t.isNoop(node, opts)` and `t.assertNoop(node, opts)`. +Aliases: none + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.noop(); + +const { code } = generate(ast); +console.log(code); // +``` --- -### nullLiteral +### NullableTypeAnnotation ```javascript -t.nullLiteral() +t.nullableTypeAnnotation(typeAnnotation) ``` -See also `t.isNullLiteral(node, opts)` and `t.assertNullLiteral(node, opts)`. +See also `t.isNullableTypeAnnotation(node, opts)` and `t.assertNullableTypeAnnotation(node, opts)`. + +Aliases: `Flow`, `FlowType` + + - `typeAnnotation`: `FlowType` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -Aliases: `Expression`, `Pureish`, `Literal`, `Immutable` +const typeAnnotation = t.anyTypeAnnotation(); +const ast = t.nullableTypeAnnotation(typeAnnotation); +const { code } = generate(ast); +console.log(code); // ?any +``` --- -### nullLiteralTypeAnnotation +### NullLiteral ```javascript -t.nullLiteralTypeAnnotation() +t.nullLiteral() ``` -See also `t.isNullLiteralTypeAnnotation(node, opts)` and `t.assertNullLiteralTypeAnnotation(node, opts)`. +See also `t.isNullLiteral(node, opts)` and `t.assertNullLiteral(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Expression`, `Immutable`, `Literal`, `Pureish` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.nullLiteral(); + +const { code } = generate(ast); +console.log(code); // null +``` --- -### nullableTypeAnnotation +### NullLiteralTypeAnnotation ```javascript -t.nullableTypeAnnotation(typeAnnotation) +t.nullLiteralTypeAnnotation() ``` -See also `t.isNullableTypeAnnotation(node, opts)` and `t.assertNullableTypeAnnotation(node, opts)`. +See also `t.isNullLiteralTypeAnnotation(node, opts)` and `t.assertNullLiteralTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` - - `typeAnnotation`: `FlowType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.nullLiteralTypeAnnotation(); + +const { code } = generate(ast); +console.log(code); // null +``` --- -### numberLiteralTypeAnnotation +### NumberLiteralTypeAnnotation ```javascript t.numberLiteralTypeAnnotation(value) ``` @@ -1473,34 +2528,65 @@ Aliases: `Flow`, `FlowType` - `value`: `number` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = 0; +const ast = t.numberLiteralTypeAnnotation(value); + +const { code } = generate(ast); +console.log(code); // 0 +``` + --- -### numberTypeAnnotation +### NumberTypeAnnotation ```javascript t.numberTypeAnnotation() ``` See also `t.isNumberTypeAnnotation(node, opts)` and `t.assertNumberTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.numberTypeAnnotation(); +const { code } = generate(ast); +console.log(code); // number +``` --- -### numericLiteral +### NumericLiteral ```javascript t.numericLiteral(value) ``` See also `t.isNumericLiteral(node, opts)` and `t.assertNumericLiteral(node, opts)`. -Aliases: `Expression`, `Pureish`, `Literal`, `Immutable` +Aliases: `Expression`, `Immutable`, `Literal`, `Pureish` - `value`: `number` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = 0; +const ast = t.numericLiteral(value); + +const { code } = generate(ast); +console.log(code); // 0 +``` + --- -### objectExpression +### ObjectExpression ```javascript t.objectExpression(properties) ``` @@ -1513,63 +2599,75 @@ Aliases: `Expression` --- -### objectMethod +### ObjectMethod ```javascript -t.objectMethod(kind, key, params, body, computed) +t.objectMethod(kind, key, params, body) ``` See also `t.isObjectMethod(node, opts)` and `t.assertObjectMethod(node, opts)`. -Aliases: `UserWhitespacable`, `Function`, `Scopable`, `BlockParent`, `FunctionParent`, `Method`, `ObjectMember` +Aliases: `BlockParent`, `Function`, `FunctionParent`, `Method`, `ObjectMember`, `Scopable`, `UserWhitespacable` - - `kind`: `"method" | "get" | "set"` (default: `'method'`) + - `kind`: `"method" | "get" | "set"` (required) - `key`: if computed then `Expression` else `Identifier | Literal` (required) - - `params`: `Array` (required) + - `params`: `Array` (required) - `body`: `BlockStatement` (required) - `computed`: `boolean` (default: `false`) - `async`: `boolean` (default: `false`) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `generator`: `boolean` (default: `false`) - `returnType`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) - `typeParameters`: `TypeParameterDeclaration | TSTypeParameterDeclaration | Noop` (default: `null`) --- -### objectPattern +### ObjectPattern ```javascript t.objectPattern(properties) ``` See also `t.isObjectPattern(node, opts)` and `t.assertObjectPattern(node, opts)`. -Aliases: `Pattern`, `PatternLike`, `LVal` +Aliases: `LVal`, `Pattern`, `PatternLike` - `properties`: `Array` (required) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) --- -### objectProperty +### ObjectProperty ```javascript -t.objectProperty(key, value, computed, shorthand, decorators) +t.objectProperty(key, value) ``` See also `t.isObjectProperty(node, opts)` and `t.assertObjectProperty(node, opts)`. -Aliases: `UserWhitespacable`, `Property`, `ObjectMember` +Aliases: `ObjectMember`, `Property`, `UserWhitespacable` - `key`: if computed then `Expression` else `Identifier | Literal` (required) - `value`: `Expression | PatternLike` (required) - `computed`: `boolean` (default: `false`) - `shorthand`: `boolean` (default: `false`) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const key = t.identifier("foo"); +const value = t.identifier("bar"); +const ast = t.objectProperty(key, value); + +const { code } = generate(ast); +console.log(code); // foo: bar +``` --- -### objectTypeAnnotation +### ObjectTypeAnnotation ```javascript -t.objectTypeAnnotation(properties, indexers, callProperties, internalSlots, exact) +t.objectTypeAnnotation(properties) ``` See also `t.isObjectTypeAnnotation(node, opts)` and `t.assertObjectTypeAnnotation(node, opts)`. @@ -1577,14 +2675,15 @@ See also `t.isObjectTypeAnnotation(node, opts)` and `t.assertObjectTypeAnnotatio Aliases: `Flow`, `FlowType` - `properties`: `Array` (required) - - `indexers`: `Array` (default: `null`) - - `callProperties`: `Array` (default: `null`) - - `internalSlots`: `Array` (default: `null`) + - `indexers`: `Array` (default: `[]`) + - `callProperties`: `Array` (default: `[]`) + - `internalSlots`: `Array` (default: `[]`) - `exact`: `boolean` (default: `false`) + - `inexact`: `boolean` (default: `false`) --- -### objectTypeCallProperty +### ObjectTypeCallProperty ```javascript t.objectTypeCallProperty(value) ``` @@ -1594,28 +2693,52 @@ See also `t.isObjectTypeCallProperty(node, opts)` and `t.assertObjectTypeCallPro Aliases: `Flow`, `UserWhitespacable` - `value`: `FlowType` (required) - - `static`: `boolean` (default: `null`) + - `static`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = t.anyTypeAnnotation(); +const ast = t.objectTypeCallProperty(value); + +const { code } = generate(ast); +console.log(code); // any +``` --- -### objectTypeIndexer +### ObjectTypeIndexer ```javascript -t.objectTypeIndexer(id, key, value, variance) +t.objectTypeIndexer(id, key, value) ``` See also `t.isObjectTypeIndexer(node, opts)` and `t.assertObjectTypeIndexer(node, opts)`. Aliases: `Flow`, `UserWhitespacable` - - `id`: `Identifier` (default: `null`) + - `id`: `Identifier` (required) - `key`: `FlowType` (required) - `value`: `FlowType` (required) - `variance`: `Variance` (default: `null`) - - `static`: `boolean` (default: `null`) + - `static`: `boolean` (default: `false`) ---- +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const key = t.anyTypeAnnotation(); +const value = t.anyTypeAnnotation(); +const ast = t.objectTypeIndexer(id, key, value); + +const { code } = generate(ast); +console.log(code); // [foo: any]: any +``` -### objectTypeInternalSlot +--- + +### ObjectTypeInternalSlot ```javascript t.objectTypeInternalSlot(id, value, optional, static, method) ``` @@ -1630,11 +2753,26 @@ Aliases: `Flow`, `UserWhitespacable` - `static`: `boolean` (required) - `method`: `boolean` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const value = t.anyTypeAnnotation(); +const optional = true; +const static = true; +const method = true; +const ast = t.objectTypeInternalSlot(id, value, optional, static, method); + +const { code } = generate(ast); +console.log(code); // static [[foo]]?any +``` + --- -### objectTypeProperty +### ObjectTypeProperty ```javascript -t.objectTypeProperty(key, value, variance) +t.objectTypeProperty(key, value) ``` See also `t.isObjectTypeProperty(node, opts)` and `t.assertObjectTypeProperty(node, opts)`. @@ -1645,13 +2783,25 @@ Aliases: `Flow`, `UserWhitespacable` - `value`: `FlowType` (required) - `variance`: `Variance` (default: `null`) - `kind`: `"init" | "get" | "set"` (default: `null`) - - `optional`: `boolean` (default: `null`) - - `proto`: `boolean` (default: `null`) - - `static`: `boolean` (default: `null`) + - `optional`: `boolean` (default: `false`) + - `proto`: `boolean` (default: `false`) + - `static`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const key = t.identifier("foo"); +const value = t.anyTypeAnnotation(); +const ast = t.objectTypeProperty(key, value); + +const { code } = generate(ast); +console.log(code); // foo: any +``` --- -### objectTypeSpreadProperty +### ObjectTypeSpreadProperty ```javascript t.objectTypeSpreadProperty(argument) ``` @@ -1662,25 +2812,36 @@ Aliases: `Flow`, `UserWhitespacable` - `argument`: `FlowType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.anyTypeAnnotation(); +const ast = t.objectTypeSpreadProperty(argument); + +const { code } = generate(ast); +console.log(code); // ...any +``` + --- -### opaqueType +### OpaqueType ```javascript t.opaqueType(id, typeParameters, supertype, impltype) ``` See also `t.isOpaqueType(node, opts)` and `t.assertOpaqueType(node, opts)`. -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) - - `supertype`: `FlowType` (default: `null`) + - `typeParameters`: `TypeParameterDeclaration` (required) + - `supertype`: `FlowType` (required) - `impltype`: `FlowType` (required) --- -### optionalCallExpression +### OptionalCallExpression ```javascript t.optionalCallExpression(callee, arguments, optional) ``` @@ -1697,7 +2858,7 @@ Aliases: `Expression` --- -### optionalMemberExpression +### OptionalMemberExpression ```javascript t.optionalMemberExpression(object, property, computed, optional) ``` @@ -1708,12 +2869,12 @@ Aliases: `Expression` - `object`: `Expression` (required) - `property`: `any` (required) - - `computed`: `boolean` (default: `false`) + - `computed`: `boolean` (required) - `optional`: `boolean` (required) --- -### parenthesizedExpression +### ParenthesizedExpression ```javascript t.parenthesizedExpression(expression) ``` @@ -1724,9 +2885,115 @@ Aliases: `Expression`, `ExpressionWrapper` - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.parenthesizedExpression(expression); + +const { code } = generate(ast); +console.log(code); // (foo) +``` + +--- + +### PipelineBareFunction +```javascript +t.pipelineBareFunction(callee) +``` + +See also `t.isPipelineBareFunction(node, opts)` and `t.assertPipelineBareFunction(node, opts)`. + +Aliases: none + + - `callee`: `Expression` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const callee = t.identifier("foo"); +const ast = t.pipelineBareFunction(callee); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### privateName +### PipelinePrimaryTopicReference +```javascript +t.pipelinePrimaryTopicReference() +``` + +See also `t.isPipelinePrimaryTopicReference(node, opts)` and `t.assertPipelinePrimaryTopicReference(node, opts)`. + +Aliases: `Expression` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.pipelinePrimaryTopicReference(); + +const { code } = generate(ast); +console.log(code); // # +``` + +--- + +### PipelineTopicExpression +```javascript +t.pipelineTopicExpression(expression) +``` + +See also `t.isPipelineTopicExpression(node, opts)` and `t.assertPipelineTopicExpression(node, opts)`. + +Aliases: none + + - `expression`: `Expression` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.pipelineTopicExpression(expression); + +const { code } = generate(ast); +console.log(code); // foo +``` + +--- + +### Placeholder +```javascript +t.placeholder(expectedNode, name) +``` + +See also `t.isPlaceholder(node, opts)` and `t.assertPlaceholder(node, opts)`. + +Aliases: none + + - `expectedNode`: `"Identifier" | "StringLiteral" | "Expression" | "Statement" | "Declaration" | "BlockStatement" | "ClassBody" | "Pattern"` (required) + - `name`: `Identifier` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expectedNode = "Identifier"; +const name = t.identifier("foo"); +const ast = t.placeholder(expectedNode, name); + +const { code } = generate(ast); +console.log(code); // %%foo%% +``` + +--- + +### PrivateName ```javascript t.privateName(id) ``` @@ -1737,26 +3004,61 @@ Aliases: `Private` - `id`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.privateName(id); + +const { code } = generate(ast); +console.log(code); // #foo +``` + --- -### program +### Program ```javascript -t.program(body, directives, sourceType, interpreter) +t.program(body) ``` See also `t.isProgram(node, opts)` and `t.assertProgram(node, opts)`. -Aliases: `Scopable`, `BlockParent`, `Block` +Aliases: `Block`, `BlockParent`, `Scopable` - `body`: `Array` (required) - `directives`: `Array` (default: `[]`) - `sourceType`: `"script" | "module"` (default: `'script'`) - `interpreter`: `InterpreterDirective` (default: `null`) - - `sourceFile`: `string` (default: `null`) + - `sourceFile`: `string` (default: `""`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const body = [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), + ]), +]; +const ast = t.program(body); + +const { code } = generate(ast); +console.log(code); +// const foo = bar; +// const baz = qux; +// const quux = corge; +``` --- -### qualifiedTypeIdentifier +### QualifiedTypeIdentifier ```javascript t.qualifiedTypeIdentifier(id, qualification) ``` @@ -1768,11 +3070,23 @@ Aliases: `Flow` - `id`: `Identifier` (required) - `qualification`: `Identifier | QualifiedTypeIdentifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const qualification = t.identifier("bar"); +const ast = t.qualifiedTypeIdentifier(id, qualification); + +const { code } = generate(ast); +console.log(code); // bar.foo +``` + --- -### regExpLiteral +### RegExpLiteral ```javascript -t.regExpLiteral(pattern, flags) +t.regExpLiteral(pattern) ``` See also `t.isRegExpLiteral(node, opts)` and `t.assertRegExpLiteral(node, opts)`. @@ -1780,11 +3094,22 @@ See also `t.isRegExpLiteral(node, opts)` and `t.assertRegExpLiteral(node, opts)` Aliases: `Expression`, `Literal` - `pattern`: `string` (required) - - `flags`: `string` (default: `''`) + - `flags`: `string` (default: `""`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const pattern = "w+"; +const ast = t.regExpLiteral(pattern); + +const { code } = generate(ast); +console.log(code); // /w+/ +``` --- -### restElement +### RestElement ```javascript t.restElement(argument) ``` @@ -1794,25 +3119,46 @@ See also `t.isRestElement(node, opts)` and `t.assertRestElement(node, opts)`. Aliases: `LVal`, `PatternLike` - `argument`: `LVal` (required) - - `decorators`: `Array` (default: `null`) + - `decorators`: `Array` (default: `[]`) - `typeAnnotation`: `TypeAnnotation | TSTypeAnnotation | Noop` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.identifier("foo"); +const ast = t.restElement(argument); + +const { code } = generate(ast); +console.log(code); // ...foo +``` + --- -### returnStatement +### ReturnStatement ```javascript -t.returnStatement(argument) +t.returnStatement() ``` See also `t.isReturnStatement(node, opts)` and `t.assertReturnStatement(node, opts)`. -Aliases: `Statement`, `Terminatorless`, `CompletionStatement` +Aliases: `CompletionStatement`, `Statement`, `Terminatorless` - `argument`: `Expression` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.returnStatement(); + +const { code } = generate(ast); +console.log(code); // return; +``` + --- -### sequenceExpression +### SequenceExpression ```javascript t.sequenceExpression(expressions) ``` @@ -1823,9 +3169,24 @@ Aliases: `Expression` - `expressions`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expressions = [ + t.identifier("foo"), + t.identifier("bar"), + t.identifier("baz"), +]; +const ast = t.sequenceExpression(expressions); + +const { code } = generate(ast); +console.log(code); // foo, bar, baz +``` + --- -### spreadElement +### SpreadElement ```javascript t.spreadElement(argument) ``` @@ -1836,22 +3197,44 @@ Aliases: `UnaryLike` - `argument`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.identifier("foo"); +const ast = t.spreadElement(argument); + +const { code } = generate(ast); +console.log(code); // ...foo +``` + --- -### stringLiteral +### StringLiteral ```javascript t.stringLiteral(value) ``` See also `t.isStringLiteral(node, opts)` and `t.assertStringLiteral(node, opts)`. -Aliases: `Expression`, `Pureish`, `Literal`, `Immutable` +Aliases: `Expression`, `Immutable`, `Literal`, `Pureish` - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.stringLiteral(value); + +const { code } = generate(ast); +console.log(code); // "0" +``` + --- -### stringLiteralTypeAnnotation +### StringLiteralTypeAnnotation ```javascript t.stringLiteralTypeAnnotation(value) ``` @@ -1862,21 +3245,41 @@ Aliases: `Flow`, `FlowType` - `value`: `string` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const value = "0"; +const ast = t.stringLiteralTypeAnnotation(value); + +const { code } = generate(ast); +console.log(code); // "0" +``` + --- -### stringTypeAnnotation +### StringTypeAnnotation ```javascript t.stringTypeAnnotation() ``` See also `t.isStringTypeAnnotation(node, opts)` and `t.assertStringTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.stringTypeAnnotation(); + +const { code } = generate(ast); +console.log(code); // string +``` --- -### super +### Super ```javascript t.super() ``` @@ -1885,36 +3288,300 @@ See also `t.isSuper(node, opts)` and `t.assertSuper(node, opts)`. Aliases: `Expression` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.super(); + +const { code } = generate(ast); +console.log(code); // super +``` --- -### switchCase +### SwitchCase ```javascript t.switchCase(test, consequent) ``` See also `t.isSwitchCase(node, opts)` and `t.assertSwitchCase(node, opts)`. - - `test`: `Expression` (default: `null`) +Aliases: none + + - `test`: `Expression` (required) - `consequent`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const test = t.identifier("foo"); +const consequent = [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("bar"), t.identifier("baz")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("qux"), t.identifier("quux")), + ]), + t.breakStatement(), +]; +const ast = t.switchCase(test, consequent); + +const { code } = generate(ast); +console.log(code); +// case foo: +// const bar = baz; +// const qux = quux; +// break; +``` + --- -### switchStatement +### SwitchStatement ```javascript t.switchStatement(discriminant, cases) ``` See also `t.isSwitchStatement(node, opts)` and `t.assertSwitchStatement(node, opts)`. -Aliases: `Statement`, `BlockParent`, `Scopable` +Aliases: `BlockParent`, `Scopable`, `Statement` - `discriminant`: `Expression` (required) - `cases`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const discriminant = t.identifier("foo"); +const cases = [ + t.switchCase(t.stringLiteral("bar"), [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), + ]), + t.breakStatement(), + ]), + t.switchCase(t.stringLiteral("grault"), [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("garply"), t.identifier("waldo")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("fred"), t.identifier("plugh")), + ]), + t.breakStatement(), + ]), + t.switchCase(t.stringLiteral("xyzzy"), [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("thud"), t.identifier("barFoo")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("bazFoo"), t.identifier("quxFoo")), + ]), + t.breakStatement(), + ]), +]; +const ast = t.switchStatement(discriminant, cases); + +const { code } = generate(ast); +console.log(code); +// switch (foo) { +// case "bar": +// const baz = qux; +// const quux = corge; +// break; +// +// case "grault": +// const garply = waldo; +// const fred = plugh; +// break; +// +// case "xyzzy": +// const thud = barFoo; +// const bazFoo = quxFoo; +// break; +// } +``` + +--- + +### TaggedTemplateExpression +```javascript +t.taggedTemplateExpression(tag, quasi) +``` + +See also `t.isTaggedTemplateExpression(node, opts)` and `t.assertTaggedTemplateExpression(node, opts)`. + +Aliases: `Expression` + + - `tag`: `Expression` (required) + - `quasi`: `TemplateLiteral` (required) + - `typeParameters`: `TypeParameterInstantiation | TSTypeParameterInstantiation` (default: `null`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const tag = t.identifier("foo"); +const quasi = t.templateLiteral( + [t.templateElement({ raw: "bar" }, true)], + [t.identifier("baz"), t.identifier("qux"), t.identifier("quux")] +); +const ast = t.taggedTemplateExpression(tag, quasi); + +const { code } = generate(ast); +console.log(code); // foo`bar` +``` + +--- + +### TemplateElement +```javascript +t.templateElement(value) +``` + +See also `t.isTemplateElement(node, opts)` and `t.assertTemplateElement(node, opts)`. + +Aliases: none + + - `value`: `any` (required) + - `tail`: `boolean` (default: `false`) + --- -### tSAnyKeyword +### TemplateLiteral +```javascript +t.templateLiteral(quasis, expressions) +``` + +See also `t.isTemplateLiteral(node, opts)` and `t.assertTemplateLiteral(node, opts)`. + +Aliases: `Expression`, `Literal` + + - `quasis`: `Array` (required) + - `expressions`: `Array` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const quasis = [ + t.templateElement({ raw: "foo" }, true), + t.templateElement({ raw: "bar" }, true), + t.templateElement({ raw: "baz" }, true), +]; +const expressions = [ + t.identifier("qux"), + t.identifier("quux"), + t.identifier("corge"), +]; +const ast = t.templateLiteral(quasis, expressions); + +const { code } = generate(ast); +console.log(code); // `foo${qux}bar${quux}baz` +``` + +--- + +### ThisExpression +```javascript +t.thisExpression() +``` + +See also `t.isThisExpression(node, opts)` and `t.assertThisExpression(node, opts)`. + +Aliases: `Expression` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.thisExpression(); + +const { code } = generate(ast); +console.log(code); // this +``` + +--- + +### ThisTypeAnnotation +```javascript +t.thisTypeAnnotation() +``` + +See also `t.isThisTypeAnnotation(node, opts)` and `t.assertThisTypeAnnotation(node, opts)`. + +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.thisTypeAnnotation(); + +const { code } = generate(ast); +console.log(code); // this +``` + +--- + +### ThrowStatement +```javascript +t.throwStatement(argument) +``` + +See also `t.isThrowStatement(node, opts)` and `t.assertThrowStatement(node, opts)`. + +Aliases: `CompletionStatement`, `Statement`, `Terminatorless` + + - `argument`: `Expression` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.identifier("foo"); +const ast = t.throwStatement(argument); + +const { code } = generate(ast); +console.log(code); // throw foo; +``` + +--- + +### TryStatement +```javascript +t.tryStatement(block) +``` + +See also `t.isTryStatement(node, opts)` and `t.assertTryStatement(node, opts)`. + +Aliases: `Statement` + + - `block`: `BlockStatement` (required) + - `handler`: `CatchClause` (default: `null`) + - `finalizer`: `BlockStatement` (default: `null`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const block = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.tryStatement(block); + +const { code } = generate(ast); +console.log(code); +// try { +// return this; +// } +``` + +--- + +### TSAnyKeyword ```javascript t.tsAnyKeyword() ``` @@ -1923,10 +3590,19 @@ See also `t.isTSAnyKeyword(node, opts)` and `t.assertTSAnyKeyword(node, opts)`. Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsAnyKeyword(); + +const { code } = generate(ast); +console.log(code); // any +``` --- -### tSArrayType +### TSArrayType ```javascript t.tsArrayType(elementType) ``` @@ -1937,9 +3613,20 @@ Aliases: `TSType` - `elementType`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const elementType = t.tsAnyKeyword(); +const ast = t.tsArrayType(elementType); + +const { code } = generate(ast); +console.log(code); // any[] +``` + --- -### tSAsExpression +### TSAsExpression ```javascript t.tsAsExpression(expression, typeAnnotation) ``` @@ -1951,9 +3638,21 @@ Aliases: `Expression` - `expression`: `Expression` (required) - `typeAnnotation`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const typeAnnotation = t.tsAnyKeyword(); +const ast = t.tsAsExpression(expression, typeAnnotation); + +const { code } = generate(ast); +console.log(code); // foo as any +``` + --- -### tSBooleanKeyword +### TSBooleanKeyword ```javascript t.tsBooleanKeyword() ``` @@ -1962,25 +3661,34 @@ See also `t.isTSBooleanKeyword(node, opts)` and `t.assertTSBooleanKeyword(node, Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsBooleanKeyword(); + +const { code } = generate(ast); +console.log(code); // boolean +``` --- -### tSCallSignatureDeclaration +### TSCallSignatureDeclaration ```javascript -t.tsCallSignatureDeclaration(typeParameters, parameters, typeAnnotation) +t.tsCallSignatureDeclaration(typeParameters, parameters) ``` See also `t.isTSCallSignatureDeclaration(node, opts)` and `t.assertTSCallSignatureDeclaration(node, opts)`. Aliases: `TSTypeElement` - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) - - `parameters`: `Array` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `parameters`: `Array` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) --- -### tSConditionalType +### TSConditionalType ```javascript t.tsConditionalType(checkType, extendsType, trueType, falseType) ``` @@ -1994,111 +3702,161 @@ Aliases: `TSType` - `trueType`: `TSType` (required) - `falseType`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const checkType = t.tsAnyKeyword(); +const extendsType = t.tsAnyKeyword(); +const trueType = t.tsAnyKeyword(); +const falseType = t.tsAnyKeyword(); +const ast = t.tsConditionalType(checkType, extendsType, trueType, falseType); + +const { code } = generate(ast); +console.log(code); // any extends any ? any : any +``` + --- -### tSConstructSignatureDeclaration +### TSConstructorType ```javascript -t.tsConstructSignatureDeclaration(typeParameters, parameters, typeAnnotation) +t.tsConstructorType(typeParameters, parameters) ``` -See also `t.isTSConstructSignatureDeclaration(node, opts)` and `t.assertTSConstructSignatureDeclaration(node, opts)`. +See also `t.isTSConstructorType(node, opts)` and `t.assertTSConstructorType(node, opts)`. -Aliases: `TSTypeElement` +Aliases: `TSType` - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) - - `parameters`: `Array` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `parameters`: `Array` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) --- -### tSConstructorType +### TSConstructSignatureDeclaration ```javascript -t.tsConstructorType(typeParameters, typeAnnotation) +t.tsConstructSignatureDeclaration(typeParameters, parameters) ``` -See also `t.isTSConstructorType(node, opts)` and `t.assertTSConstructorType(node, opts)`. +See also `t.isTSConstructSignatureDeclaration(node, opts)` and `t.assertTSConstructSignatureDeclaration(node, opts)`. -Aliases: `TSType` +Aliases: `TSTypeElement` - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `parameters`: `Array` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) - - `parameters`: `Array` (default: `null`) --- -### tSDeclareFunction +### TSDeclareFunction ```javascript -t.tsDeclareFunction(id, typeParameters, params, returnType) +t.tsDeclareFunction(id, typeParameters, params) ``` See also `t.isTSDeclareFunction(node, opts)` and `t.assertTSDeclareFunction(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - - `id`: `Identifier` (default: `null`) - - `typeParameters`: `TSTypeParameterDeclaration | Noop` (default: `null`) - - `params`: `Array` (required) + - `id`: `Identifier` (required) + - `typeParameters`: `TSTypeParameterDeclaration | Noop` (required) + - `params`: `Array` (required) - `returnType`: `TSTypeAnnotation | Noop` (default: `null`) - `async`: `boolean` (default: `false`) - - `declare`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) - `generator`: `boolean` (default: `false`) --- -### tSDeclareMethod +### TSDeclareMethod ```javascript -t.tsDeclareMethod(decorators, key, typeParameters, params, returnType) +t.tsDeclareMethod(decorators, key, typeParameters, params) ``` See also `t.isTSDeclareMethod(node, opts)` and `t.assertTSDeclareMethod(node, opts)`. - - `decorators`: `Array` (default: `null`) +Aliases: none + + - `decorators`: `Array` (required) - `key`: `Identifier | StringLiteral | NumericLiteral | Expression` (required) - - `typeParameters`: `TSTypeParameterDeclaration | Noop` (default: `null`) - - `params`: `Array` (required) + - `typeParameters`: `TSTypeParameterDeclaration | Noop` (required) + - `params`: `Array` (required) - `returnType`: `TSTypeAnnotation | Noop` (default: `null`) - - `abstract`: `boolean` (default: `null`) + - `abstract`: `boolean` (default: `false`) - `access`: `"public" | "private" | "protected"` (default: `null`) - `accessibility`: `"public" | "private" | "protected"` (default: `null`) - `async`: `boolean` (default: `false`) - `computed`: `boolean` (default: `false`) - `generator`: `boolean` (default: `false`) - `kind`: `"get" | "set" | "method" | "constructor"` (default: `'method'`) - - `optional`: `boolean` (default: `null`) - - `static`: `boolean` (default: `null`) + - `optional`: `boolean` (default: `false`) + - `static`: `boolean` (default: `false`) --- -### tSEnumDeclaration +### TSEnumDeclaration ```javascript t.tsEnumDeclaration(id, members) ``` See also `t.isTSEnumDeclaration(node, opts)` and `t.assertTSEnumDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - `id`: `Identifier` (required) - `members`: `Array` (required) - - `const`: `boolean` (default: `null`) - - `declare`: `boolean` (default: `null`) + - `const`: `boolean` (default: `false`) + - `declare`: `boolean` (default: `false`) - `initializer`: `Expression` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const members = [ + t.tsEnumMember(t.identifier("bar")), + t.tsEnumMember(t.identifier("baz")), + t.tsEnumMember(t.identifier("qux")), +]; +const ast = t.tsEnumDeclaration(id, members); + +const { code } = generate(ast); +console.log(code); +// enum foo { +// bar, +// baz, +// qux, +// } +``` + --- -### tSEnumMember +### TSEnumMember ```javascript -t.tsEnumMember(id, initializer) +t.tsEnumMember(id) ``` See also `t.isTSEnumMember(node, opts)` and `t.assertTSEnumMember(node, opts)`. +Aliases: none + - `id`: `Identifier | StringLiteral` (required) - `initializer`: `Expression` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.tsEnumMember(id); + +const { code } = generate(ast); +console.log(code); // foo, +``` + --- -### tSExportAssignment +### TSExportAssignment ```javascript t.tsExportAssignment(expression) ``` @@ -2109,11 +3867,22 @@ Aliases: `Statement` - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.tsExportAssignment(expression); + +const { code } = generate(ast); +console.log(code); // export = foo; +``` + --- -### tSExpressionWithTypeArguments +### TSExpressionWithTypeArguments ```javascript -t.tsExpressionWithTypeArguments(expression, typeParameters) +t.tsExpressionWithTypeArguments(expression) ``` See also `t.isTSExpressionWithTypeArguments(node, opts)` and `t.assertTSExpressionWithTypeArguments(node, opts)`. @@ -2123,35 +3892,59 @@ Aliases: `TSType` - `expression`: `TSEntityName` (required) - `typeParameters`: `TSTypeParameterInstantiation` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.tsExpressionWithTypeArguments(expression); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### tSExternalModuleReference +### TSExternalModuleReference ```javascript t.tsExternalModuleReference(expression) ``` See also `t.isTSExternalModuleReference(node, opts)` and `t.assertTSExternalModuleReference(node, opts)`. +Aliases: none + - `expression`: `StringLiteral` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.stringLiteral("foo"); +const ast = t.tsExternalModuleReference(expression); + +const { code } = generate(ast); +console.log(code); // require("foo") +``` + --- -### tSFunctionType +### TSFunctionType ```javascript -t.tsFunctionType(typeParameters, typeAnnotation) +t.tsFunctionType(typeParameters, parameters) ``` See also `t.isTSFunctionType(node, opts)` and `t.assertTSFunctionType(node, opts)`. Aliases: `TSType` - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `parameters`: `Array` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) - - `parameters`: `Array` (default: `null`) --- -### tSImportEqualsDeclaration +### TSImportEqualsDeclaration ```javascript t.tsImportEqualsDeclaration(id, moduleReference) ``` @@ -2162,26 +3955,49 @@ Aliases: `Statement` - `id`: `Identifier` (required) - `moduleReference`: `TSEntityName | TSExternalModuleReference` (required) - - `isExport`: `boolean` (default: `null`) + - `isExport`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const moduleReference = t.identifier("bar"); +const ast = t.tsImportEqualsDeclaration(id, moduleReference); + +const { code } = generate(ast); +console.log(code); // import foo = bar; +``` --- -### tSIndexSignature +### TSImportType ```javascript -t.tsIndexSignature(parameters, typeAnnotation) +t.tsImportType(argument) ``` -See also `t.isTSIndexSignature(node, opts)` and `t.assertTSIndexSignature(node, opts)`. +See also `t.isTSImportType(node, opts)` and `t.assertTSImportType(node, opts)`. -Aliases: `TSTypeElement` +Aliases: `TSType` - - `parameters`: `Array` (required) - - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) - - `readonly`: `boolean` (default: `null`) + - `argument`: `StringLiteral` (required) + - `qualifier`: `TSEntityName` (default: `null`) + - `typeParameters`: `TSTypeParameterInstantiation` (default: `null`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const argument = t.stringLiteral("foo"); +const ast = t.tsImportType(argument); + +const { code } = generate(ast); +console.log(code); // import("foo") +``` --- -### tSIndexedAccessType +### TSIndexedAccessType ```javascript t.tsIndexedAccessType(objectType, indexType) ``` @@ -2193,9 +4009,51 @@ Aliases: `TSType` - `objectType`: `TSType` (required) - `indexType`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const objectType = t.tsAnyKeyword(); +const indexType = t.tsAnyKeyword(); +const ast = t.tsIndexedAccessType(objectType, indexType); + +const { code } = generate(ast); +console.log(code); // any[any] +``` + --- -### tSInferType +### TSIndexSignature +```javascript +t.tsIndexSignature(parameters) +``` + +See also `t.isTSIndexSignature(node, opts)` and `t.assertTSIndexSignature(node, opts)`. + +Aliases: `TSTypeElement` + + - `parameters`: `Array` (required) + - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) + - `readonly`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const parameters = [ + t.identifier("foo"), + t.identifier("bar"), + t.identifier("baz"), +]; +const ast = t.tsIndexSignature(parameters); + +const { code } = generate(ast); +console.log(code); // [foo, bar, baz]; +``` + +--- + +### TSInferType ```javascript t.tsInferType(typeParameter) ``` @@ -2208,35 +4066,57 @@ Aliases: `TSType` --- -### tSInterfaceBody +### TSInterfaceBody ```javascript t.tsInterfaceBody(body) ``` See also `t.isTSInterfaceBody(node, opts)` and `t.assertTSInterfaceBody(node, opts)`. +Aliases: none + - `body`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const body = [ + t.tsIndexSignature([t.identifier("foo")]), + t.tsIndexSignature([t.identifier("bar")]), + t.tsIndexSignature([t.identifier("baz")]), +]; +const ast = t.tsInterfaceBody(body); + +const { code } = generate(ast); +console.log(code); +// { +// [foo]; +// [bar]; +// [baz]; +// } +``` + --- -### tSInterfaceDeclaration +### TSInterfaceDeclaration ```javascript t.tsInterfaceDeclaration(id, typeParameters, extends, body) ``` See also `t.isTSInterfaceDeclaration(node, opts)` and `t.assertTSInterfaceDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) - - `extends`: `Array` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `extends`: `Array` (required) - `body`: `TSInterfaceBody` (required) - - `declare`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) --- -### tSIntersectionType +### TSIntersectionType ```javascript t.tsIntersectionType(types) ``` @@ -2247,9 +4127,20 @@ Aliases: `TSType` - `types`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const types = [t.tsAnyKeyword(), t.tsStringKeyword(), t.tsNumberKeyword()]; +const ast = t.tsIntersectionType(types); + +const { code } = generate(ast); +console.log(code); // any & string & number +``` + --- -### tSLiteralType +### TSLiteralType ```javascript t.tsLiteralType(literal) ``` @@ -2260,11 +4151,22 @@ Aliases: `TSType` - `literal`: `NumericLiteral | StringLiteral | BooleanLiteral` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const literal = t.numericLiteral(1337); +const ast = t.tsLiteralType(literal); + +const { code } = generate(ast); +console.log(code); // 1337 +``` + --- -### tSMappedType +### TSMappedType ```javascript -t.tsMappedType(typeParameter, typeAnnotation) +t.tsMappedType(typeParameter) ``` See also `t.isTSMappedType(node, opts)` and `t.assertTSMappedType(node, opts)`. @@ -2273,14 +4175,14 @@ Aliases: `TSType` - `typeParameter`: `TSTypeParameter` (required) - `typeAnnotation`: `TSType` (default: `null`) - - `optional`: `boolean` (default: `null`) - - `readonly`: `boolean` (default: `null`) + - `optional`: `boolean` (default: `false`) + - `readonly`: `boolean` (default: `false`) --- -### tSMethodSignature +### TSMethodSignature ```javascript -t.tsMethodSignature(key, typeParameters, parameters, typeAnnotation) +t.tsMethodSignature(key, typeParameters, parameters) ``` See also `t.isTSMethodSignature(node, opts)` and `t.assertTSMethodSignature(node, opts)`. @@ -2288,42 +4190,85 @@ See also `t.isTSMethodSignature(node, opts)` and `t.assertTSMethodSignature(node Aliases: `TSTypeElement` - `key`: `Expression` (required) - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) - - `parameters`: `Array` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) + - `parameters`: `Array` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) - - `computed`: `boolean` (default: `null`) - - `optional`: `boolean` (default: `null`) + - `computed`: `boolean` (default: `false`) + - `optional`: `boolean` (default: `false`) --- -### tSModuleBlock +### TSModuleBlock ```javascript t.tsModuleBlock(body) ``` See also `t.isTSModuleBlock(node, opts)` and `t.assertTSModuleBlock(node, opts)`. +Aliases: `Block`, `BlockParent`, `Scopable` + - `body`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const body = [ + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + ]), + t.variableDeclaration("const", [ + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), + ]), +]; +const ast = t.tsModuleBlock(body); + +const { code } = generate(ast); +console.log(code); +// { +// const foo = bar; +// const baz = qux; +// const quux = corge; +// } +``` + --- -### tSModuleDeclaration +### TSModuleDeclaration ```javascript t.tsModuleDeclaration(id, body) ``` See also `t.isTSModuleDeclaration(node, opts)` and `t.assertTSModuleDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - `id`: `Identifier | StringLiteral` (required) - `body`: `TSModuleBlock | TSModuleDeclaration` (required) - - `declare`: `boolean` (default: `null`) - - `global`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) + - `global`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const body = t.tsModuleBlock([t.returnStatement(t.thisExpression())]); +const ast = t.tsModuleDeclaration(id, body); + +const { code } = generate(ast); +console.log(code); +// namespace foo { +// return this; +// } +``` --- -### tSNamespaceExportDeclaration +### TSNamespaceExportDeclaration ```javascript t.tsNamespaceExportDeclaration(id) ``` @@ -2334,9 +4279,20 @@ Aliases: `Statement` - `id`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.tsNamespaceExportDeclaration(id); + +const { code } = generate(ast); +console.log(code); // export as namespace foo +``` + --- -### tSNeverKeyword +### TSNeverKeyword ```javascript t.tsNeverKeyword() ``` @@ -2345,10 +4301,19 @@ See also `t.isTSNeverKeyword(node, opts)` and `t.assertTSNeverKeyword(node, opts Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsNeverKeyword(); + +const { code } = generate(ast); +console.log(code); // never +``` --- -### tSNonNullExpression +### TSNonNullExpression ```javascript t.tsNonNullExpression(expression) ``` @@ -2359,9 +4324,20 @@ Aliases: `Expression` - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const expression = t.identifier("foo"); +const ast = t.tsNonNullExpression(expression); + +const { code } = generate(ast); +console.log(code); // foo! +``` + --- -### tSNullKeyword +### TSNullKeyword ```javascript t.tsNullKeyword() ``` @@ -2370,10 +4346,19 @@ See also `t.isTSNullKeyword(node, opts)` and `t.assertTSNullKeyword(node, opts)` Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsNullKeyword(); + +const { code } = generate(ast); +console.log(code); // null +``` --- -### tSNumberKeyword +### TSNumberKeyword ```javascript t.tsNumberKeyword() ``` @@ -2382,10 +4367,19 @@ See also `t.isTSNumberKeyword(node, opts)` and `t.assertTSNumberKeyword(node, op Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsNumberKeyword(); + +const { code } = generate(ast); +console.log(code); // number +``` --- -### tSObjectKeyword +### TSObjectKeyword ```javascript t.tsObjectKeyword() ``` @@ -2394,10 +4388,43 @@ See also `t.isTSObjectKeyword(node, opts)` and `t.assertTSObjectKeyword(node, op Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsObjectKeyword(); + +const { code } = generate(ast); +console.log(code); // object +``` --- -### tSParameterProperty +### TSOptionalType +```javascript +t.tsOptionalType(typeAnnotation) +``` + +See also `t.isTSOptionalType(node, opts)` and `t.assertTSOptionalType(node, opts)`. + +Aliases: `TSType` + + - `typeAnnotation`: `TSType` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.tsAnyKeyword(); +const ast = t.tsOptionalType(typeAnnotation); + +const { code } = generate(ast); +console.log(code); // any? +``` + +--- + +### TSParameterProperty ```javascript t.tsParameterProperty(parameter) ``` @@ -2408,11 +4435,22 @@ Aliases: `LVal` - `parameter`: `Identifier | AssignmentPattern` (required) - `accessibility`: `"public" | "private" | "protected"` (default: `null`) - - `readonly`: `boolean` (default: `null`) + - `readonly`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const parameter = t.identifier("foo"); +const ast = t.tsParameterProperty(parameter); + +const { code } = generate(ast); +console.log(code); // foo +``` --- -### tSParenthesizedType +### TSParenthesizedType ```javascript t.tsParenthesizedType(typeAnnotation) ``` @@ -2423,11 +4461,22 @@ Aliases: `TSType` - `typeAnnotation`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.tsAnyKeyword(); +const ast = t.tsParenthesizedType(typeAnnotation); + +const { code } = generate(ast); +console.log(code); // (any) +``` + --- -### tSPropertySignature +### TSPropertySignature ```javascript -t.tsPropertySignature(key, typeAnnotation, initializer) +t.tsPropertySignature(key) ``` See also `t.isTSPropertySignature(node, opts)` and `t.assertTSPropertySignature(node, opts)`. @@ -2437,13 +4486,24 @@ Aliases: `TSTypeElement` - `key`: `Expression` (required) - `typeAnnotation`: `TSTypeAnnotation` (default: `null`) - `initializer`: `Expression` (default: `null`) - - `computed`: `boolean` (default: `null`) - - `optional`: `boolean` (default: `null`) - - `readonly`: `boolean` (default: `null`) + - `computed`: `boolean` (default: `false`) + - `optional`: `boolean` (default: `false`) + - `readonly`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const key = t.identifier("foo"); +const ast = t.tsPropertySignature(key); + +const { code } = generate(ast); +console.log(code); // foo; +``` --- -### tSQualifiedName +### TSQualifiedName ```javascript t.tsQualifiedName(left, right) ``` @@ -2455,9 +4515,45 @@ Aliases: `TSEntityName` - `left`: `TSEntityName` (required) - `right`: `Identifier` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const left = t.identifier("foo"); +const right = t.identifier("bar"); +const ast = t.tsQualifiedName(left, right); + +const { code } = generate(ast); +console.log(code); // foo.bar +``` + +--- + +### TSRestType +```javascript +t.tsRestType(typeAnnotation) +``` + +See also `t.isTSRestType(node, opts)` and `t.assertTSRestType(node, opts)`. + +Aliases: `TSType` + + - `typeAnnotation`: `TSType` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.tsAnyKeyword(); +const ast = t.tsRestType(typeAnnotation); + +const { code } = generate(ast); +console.log(code); // ...any +``` + --- -### tSStringKeyword +### TSStringKeyword ```javascript t.tsStringKeyword() ``` @@ -2466,10 +4562,19 @@ See also `t.isTSStringKeyword(node, opts)` and `t.assertTSStringKeyword(node, op Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsStringKeyword(); + +const { code } = generate(ast); +console.log(code); // string +``` --- -### tSSymbolKeyword +### TSSymbolKeyword ```javascript t.tsSymbolKeyword() ``` @@ -2478,10 +4583,19 @@ See also `t.isTSSymbolKeyword(node, opts)` and `t.assertTSSymbolKeyword(node, op Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsSymbolKeyword(); + +const { code } = generate(ast); +console.log(code); // symbol +``` --- -### tSThisType +### TSThisType ```javascript t.tsThisType() ``` @@ -2490,10 +4604,19 @@ See also `t.isTSThisType(node, opts)` and `t.assertTSThisType(node, opts)`. Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsThisType(); + +const { code } = generate(ast); +console.log(code); // this +``` --- -### tSTupleType +### TSTupleType ```javascript t.tsTupleType(elementTypes) ``` @@ -2504,36 +4627,64 @@ Aliases: `TSType` - `elementTypes`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const elementTypes = [ + t.tsAnyKeyword(), + t.tsStringKeyword(), + t.tsNumberKeyword(), +]; +const ast = t.tsTupleType(elementTypes); + +const { code } = generate(ast); +console.log(code); // [any, string, number] +``` + --- -### tSTypeAliasDeclaration +### TSTypeAliasDeclaration ```javascript t.tsTypeAliasDeclaration(id, typeParameters, typeAnnotation) ``` See also `t.isTSTypeAliasDeclaration(node, opts)` and `t.assertTSTypeAliasDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - `id`: `Identifier` (required) - - `typeParameters`: `TSTypeParameterDeclaration` (default: `null`) + - `typeParameters`: `TSTypeParameterDeclaration` (required) - `typeAnnotation`: `TSType` (required) - - `declare`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) --- -### tSTypeAnnotation +### TSTypeAnnotation ```javascript t.tsTypeAnnotation(typeAnnotation) ``` See also `t.isTSTypeAnnotation(node, opts)` and `t.assertTSTypeAnnotation(node, opts)`. +Aliases: none + - `typeAnnotation`: `TSType` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.tsAnyKeyword(); +const ast = t.tsTypeAnnotation(typeAnnotation); + +const { code } = generate(ast); +console.log(code); // : any +``` + --- -### tSTypeAssertion +### TSTypeAssertion ```javascript t.tsTypeAssertion(typeAnnotation, expression) ``` @@ -2545,9 +4696,21 @@ Aliases: `Expression` - `typeAnnotation`: `TSType` (required) - `expression`: `Expression` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeAnnotation = t.tsAnyKeyword(); +const expression = t.identifier("foo"); +const ast = t.tsTypeAssertion(typeAnnotation, expression); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### tSTypeLiteral +### TSTypeLiteral ```javascript t.tsTypeLiteral(members) ``` @@ -2558,9 +4721,29 @@ Aliases: `TSType` - `members`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const members = [ + t.tsIndexSignature([t.identifier("foo")]), + t.tsIndexSignature([t.identifier("bar")]), + t.tsIndexSignature([t.identifier("baz")]), +]; +const ast = t.tsTypeLiteral(members); + +const { code } = generate(ast); +console.log(code); +// { +// [foo]; +// [bar]; +// [baz]; +// } +``` + --- -### tSTypeOperator +### TSTypeOperator ```javascript t.tsTypeOperator(typeAnnotation) ``` @@ -2570,46 +4753,63 @@ See also `t.isTSTypeOperator(node, opts)` and `t.assertTSTypeOperator(node, opts Aliases: `TSType` - `typeAnnotation`: `TSType` (required) - - `operator`: `string` (default: `null`) + - `operator`: `string` (default: `""`) --- -### tSTypeParameter +### TSTypeParameter ```javascript -t.tsTypeParameter(constraint, default) +t.tsTypeParameter() ``` See also `t.isTSTypeParameter(node, opts)` and `t.assertTSTypeParameter(node, opts)`. +Aliases: none + - `constraint`: `TSType` (default: `null`) - `default`: `TSType` (default: `null`) - - `name`: `string` (default: `null`) + - `name`: `string` (default: `""`) --- -### tSTypeParameterDeclaration +### TSTypeParameterDeclaration ```javascript t.tsTypeParameterDeclaration(params) ``` See also `t.isTSTypeParameterDeclaration(node, opts)` and `t.assertTSTypeParameterDeclaration(node, opts)`. +Aliases: none + - `params`: `Array` (required) --- -### tSTypeParameterInstantiation +### TSTypeParameterInstantiation ```javascript t.tsTypeParameterInstantiation(params) ``` See also `t.isTSTypeParameterInstantiation(node, opts)` and `t.assertTSTypeParameterInstantiation(node, opts)`. +Aliases: none + - `params`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const params = [t.tsAnyKeyword(), t.tsStringKeyword(), t.tsNumberKeyword()]; +const ast = t.tsTypeParameterInstantiation(params); + +const { code } = generate(ast); +console.log(code); // +``` + --- -### tSTypePredicate +### TSTypePredicate ```javascript t.tsTypePredicate(parameterName, typeAnnotation) ``` @@ -2621,9 +4821,21 @@ Aliases: `TSType` - `parameterName`: `Identifier | TSThisType` (required) - `typeAnnotation`: `TSTypeAnnotation` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const parameterName = t.identifier("foo"); +const typeAnnotation = t.tsTypeAnnotation(t.tsAnyKeyword()); +const ast = t.tsTypePredicate(parameterName, typeAnnotation); + +const { code } = generate(ast); +console.log(code); // foo is any +``` + --- -### tSTypeQuery +### TSTypeQuery ```javascript t.tsTypeQuery(exprName) ``` @@ -2632,13 +4844,24 @@ See also `t.isTSTypeQuery(node, opts)` and `t.assertTSTypeQuery(node, opts)`. Aliases: `TSType` - - `exprName`: `TSEntityName` (required) + - `exprName`: `TSEntityName | TSImportType` (required) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const exprName = t.identifier("foo"); +const ast = t.tsTypeQuery(exprName); + +const { code } = generate(ast); +console.log(code); // typeof foo +``` --- -### tSTypeReference +### TSTypeReference ```javascript -t.tsTypeReference(typeName, typeParameters) +t.tsTypeReference(typeName) ``` See also `t.isTSTypeReference(node, opts)` and `t.assertTSTypeReference(node, opts)`. @@ -2648,9 +4871,20 @@ Aliases: `TSType` - `typeName`: `TSEntityName` (required) - `typeParameters`: `TSTypeParameterInstantiation` (default: `null`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const typeName = t.identifier("foo"); +const ast = t.tsTypeReference(typeName); + +const { code } = generate(ast); +console.log(code); // foo +``` + --- -### tSUndefinedKeyword +### TSUndefinedKeyword ```javascript t.tsUndefinedKeyword() ``` @@ -2659,10 +4893,19 @@ See also `t.isTSUndefinedKeyword(node, opts)` and `t.assertTSUndefinedKeyword(no Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.tsUndefinedKeyword(); + +const { code } = generate(ast); +console.log(code); // undefined +``` --- -### tSUnionType +### TSUnionType ```javascript t.tsUnionType(types) ``` @@ -2673,183 +4916,181 @@ Aliases: `TSType` - `types`: `Array` (required) ---- - -### tSUnknownType ```javascript -t.tsUnknownType(types) -``` +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -See also `t.isTSUnknownType(node, opts)` and `t.assertTSUnknownType(node, opts)`. - -Aliases: `TSType` +const types = [t.tsAnyKeyword(), t.tsStringKeyword(), t.tsNumberKeyword()]; +const ast = t.tsUnionType(types); - - `types`: `Array` (required) +const { code } = generate(ast); +console.log(code); // any | string | number +``` --- -### tSVoidKeyword +### TSUnknownKeyword ```javascript -t.tsVoidKeyword() +t.tsUnknownKeyword() ``` -See also `t.isTSVoidKeyword(node, opts)` and `t.assertTSVoidKeyword(node, opts)`. +See also `t.isTSUnknownKeyword(node, opts)` and `t.assertTSUnknownKeyword(node, opts)`. Aliases: `TSType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); ---- +const ast = t.tsUnknownKeyword(); -### taggedTemplateExpression -```javascript -t.taggedTemplateExpression(tag, quasi) +const { code } = generate(ast); +console.log(code); // unknown ``` -See also `t.isTaggedTemplateExpression(node, opts)` and `t.assertTaggedTemplateExpression(node, opts)`. +--- -Aliases: `Expression` +### TSVoidKeyword +```javascript +t.tsVoidKeyword() +``` - - `tag`: `Expression` (required) - - `quasi`: `TemplateLiteral` (required) +See also `t.isTSVoidKeyword(node, opts)` and `t.assertTSVoidKeyword(node, opts)`. ---- +Aliases: `TSType` -### templateElement ```javascript -t.templateElement(value, tail) -``` +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -See also `t.isTemplateElement(node, opts)` and `t.assertTemplateElement(node, opts)`. +const ast = t.tsVoidKeyword(); - - `value` (required) - - `tail`: `boolean` (default: `false`) +const { code } = generate(ast); +console.log(code); // void +``` --- -### templateLiteral +### TupleTypeAnnotation ```javascript -t.templateLiteral(quasis, expressions) +t.tupleTypeAnnotation(types) ``` -See also `t.isTemplateLiteral(node, opts)` and `t.assertTemplateLiteral(node, opts)`. - -Aliases: `Expression`, `Literal` +See also `t.isTupleTypeAnnotation(node, opts)` and `t.assertTupleTypeAnnotation(node, opts)`. - - `quasis`: `Array` (required) - - `expressions`: `Array` (required) +Aliases: `Flow`, `FlowType` ---- + - `types`: `Array` (required) -### thisExpression ```javascript -t.thisExpression() -``` +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -See also `t.isThisExpression(node, opts)` and `t.assertThisExpression(node, opts)`. - -Aliases: `Expression` +const types = [ + t.anyTypeAnnotation(), + t.stringTypeAnnotation(), + t.numberTypeAnnotation(), +]; +const ast = t.tupleTypeAnnotation(types); +const { code } = generate(ast); +console.log(code); // [any, string, number] +``` --- -### thisTypeAnnotation +### TypeAlias ```javascript -t.thisTypeAnnotation() +t.typeAlias(id, typeParameters, right) ``` -See also `t.isThisTypeAnnotation(node, opts)` and `t.assertThisTypeAnnotation(node, opts)`. +See also `t.isTypeAlias(node, opts)` and `t.assertTypeAlias(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Declaration`, `Flow`, `FlowDeclaration`, `Statement` + - `id`: `Identifier` (required) + - `typeParameters`: `TypeParameterDeclaration` (required) + - `right`: `FlowType` (required) --- -### throwStatement +### TypeAnnotation ```javascript -t.throwStatement(argument) +t.typeAnnotation(typeAnnotation) ``` -See also `t.isThrowStatement(node, opts)` and `t.assertThrowStatement(node, opts)`. - -Aliases: `Statement`, `Terminatorless`, `CompletionStatement` +See also `t.isTypeAnnotation(node, opts)` and `t.assertTypeAnnotation(node, opts)`. - - `argument`: `Expression` (required) +Aliases: `Flow` ---- + - `typeAnnotation`: `FlowType` (required) -### tryStatement ```javascript -t.tryStatement(block, handler, finalizer) -``` +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -See also `t.isTryStatement(node, opts)` and `t.assertTryStatement(node, opts)`. - -Aliases: `Statement` +const typeAnnotation = t.anyTypeAnnotation(); +const ast = t.typeAnnotation(typeAnnotation); - - `block`: `BlockStatement` (required) - - `handler`: `CatchClause` (default: `null`) - - `finalizer`: `BlockStatement` (default: `null`) +const { code } = generate(ast); +console.log(code); // : any +``` --- -### tupleTypeAnnotation +### TypeCastExpression ```javascript -t.tupleTypeAnnotation(types) +t.typeCastExpression(expression, typeAnnotation) ``` -See also `t.isTupleTypeAnnotation(node, opts)` and `t.assertTupleTypeAnnotation(node, opts)`. - -Aliases: `Flow`, `FlowType` +See also `t.isTypeCastExpression(node, opts)` and `t.assertTypeCastExpression(node, opts)`. - - `types`: `Array` (required) +Aliases: `Expression`, `ExpressionWrapper`, `Flow` ---- + - `expression`: `Expression` (required) + - `typeAnnotation`: `TypeAnnotation` (required) -### typeAlias ```javascript -t.typeAlias(id, typeParameters, right) -``` - -See also `t.isTypeAlias(node, opts)` and `t.assertTypeAlias(node, opts)`. +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration` +const expression = t.identifier("foo"); +const typeAnnotation = t.typeAnnotation(t.anyTypeAnnotation()); +const ast = t.typeCastExpression(expression, typeAnnotation); - - `id`: `Identifier` (required) - - `typeParameters`: `TypeParameterDeclaration` (default: `null`) - - `right`: `FlowType` (required) +const { code } = generate(ast); +console.log(code); // (foo: any) +``` --- -### typeAnnotation +### TypeofTypeAnnotation ```javascript -t.typeAnnotation(typeAnnotation) +t.typeofTypeAnnotation(argument) ``` -See also `t.isTypeAnnotation(node, opts)` and `t.assertTypeAnnotation(node, opts)`. - -Aliases: `Flow` +See also `t.isTypeofTypeAnnotation(node, opts)` and `t.assertTypeofTypeAnnotation(node, opts)`. - - `typeAnnotation`: `FlowType` (required) +Aliases: `Flow`, `FlowType` ---- + - `argument`: `FlowType` (required) -### typeCastExpression ```javascript -t.typeCastExpression(expression, typeAnnotation) -``` - -See also `t.isTypeCastExpression(node, opts)` and `t.assertTypeCastExpression(node, opts)`. +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -Aliases: `Flow`, `ExpressionWrapper`, `Expression` +const argument = t.anyTypeAnnotation(); +const ast = t.typeofTypeAnnotation(argument); - - `expression`: `Expression` (required) - - `typeAnnotation`: `TypeAnnotation` (required) +const { code } = generate(ast); +console.log(code); // typeof any +``` --- -### typeParameter +### TypeParameter ```javascript -t.typeParameter(bound, default, variance) +t.typeParameter() ``` See also `t.isTypeParameter(node, opts)` and `t.assertTypeParameter(node, opts)`. @@ -2859,11 +5100,11 @@ Aliases: `Flow` - `bound`: `TypeAnnotation` (default: `null`) - `default`: `FlowType` (default: `null`) - `variance`: `Variance` (default: `null`) - - `name`: `string` (default: `null`) + - `name`: `string` (default: `""`) --- -### typeParameterDeclaration +### TypeParameterDeclaration ```javascript t.typeParameterDeclaration(params) ``` @@ -2876,7 +5117,7 @@ Aliases: `Flow` --- -### typeParameterInstantiation +### TypeParameterInstantiation ```javascript t.typeParameterInstantiation(params) ``` @@ -2887,37 +5128,51 @@ Aliases: `Flow` - `params`: `Array` (required) ---- - -### typeofTypeAnnotation ```javascript -t.typeofTypeAnnotation(argument) -``` - -See also `t.isTypeofTypeAnnotation(node, opts)` and `t.assertTypeofTypeAnnotation(node, opts)`. +const generate = require("@babel/generator").default; +const t = require("@babel/types"); -Aliases: `Flow`, `FlowType` +const params = [ + t.anyTypeAnnotation(), + t.stringTypeAnnotation(), + t.numberTypeAnnotation(), +]; +const ast = t.typeParameterInstantiation(params); - - `argument`: `FlowType` (required) +const { code } = generate(ast); +console.log(code); // +``` --- -### unaryExpression +### UnaryExpression ```javascript -t.unaryExpression(operator, argument, prefix) +t.unaryExpression(operator, argument) ``` See also `t.isUnaryExpression(node, opts)` and `t.assertUnaryExpression(node, opts)`. -Aliases: `UnaryLike`, `Expression` +Aliases: `Expression`, `UnaryLike` - `operator`: `"void" | "throw" | "delete" | "!" | "+" | "-" | "~" | "typeof"` (required) - `argument`: `Expression` (required) - - `prefix`: `boolean` (default: `true`) + - `prefix`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const operator = "+"; +const argument = t.identifier("foo"); +const ast = t.unaryExpression(operator, argument); + +const { code } = generate(ast); +console.log(code); // +foo +``` --- -### unionTypeAnnotation +### UnionTypeAnnotation ```javascript t.unionTypeAnnotation(types) ``` @@ -2928,11 +5183,26 @@ Aliases: `Flow`, `FlowType` - `types`: `Array` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const types = [ + t.anyTypeAnnotation(), + t.stringTypeAnnotation(), + t.numberTypeAnnotation(), +]; +const ast = t.unionTypeAnnotation(types); + +const { code } = generate(ast); +console.log(code); // any | string | number +``` + --- -### updateExpression +### UpdateExpression ```javascript -t.updateExpression(operator, argument, prefix) +t.updateExpression(operator, argument) ``` See also `t.isUpdateExpression(node, opts)` and `t.assertUpdateExpression(node, opts)`. @@ -2943,37 +5213,81 @@ Aliases: `Expression` - `argument`: `Expression` (required) - `prefix`: `boolean` (default: `false`) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const operator = "++"; +const argument = t.identifier("foo"); +const ast = t.updateExpression(operator, argument); + +const { code } = generate(ast); +console.log(code); // foo++ +``` + --- -### variableDeclaration +### VariableDeclaration ```javascript t.variableDeclaration(kind, declarations) ``` See also `t.isVariableDeclaration(node, opts)` and `t.assertVariableDeclaration(node, opts)`. -Aliases: `Statement`, `Declaration` +Aliases: `Declaration`, `Statement` - `kind`: `"var" | "let" | "const"` (required) - `declarations`: `Array` (required) - - `declare`: `boolean` (default: `null`) + - `declare`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const kind = "var"; +const declarations = [ + t.variableDeclarator(t.identifier("foo"), t.identifier("bar")), + t.variableDeclarator(t.identifier("baz"), t.identifier("qux")), + t.variableDeclarator(t.identifier("quux"), t.identifier("corge")), +]; +const ast = t.variableDeclaration(kind, declarations); + +const { code } = generate(ast); +console.log(code); +// var foo = bar, +// baz = qux, +// quux = corge; +``` --- -### variableDeclarator +### VariableDeclarator ```javascript -t.variableDeclarator(id, init) +t.variableDeclarator(id) ``` See also `t.isVariableDeclarator(node, opts)` and `t.assertVariableDeclarator(node, opts)`. +Aliases: none + - `id`: `LVal` (required) - `init`: `Expression` (default: `null`) - - `definite`: `boolean` (default: `null`) + - `definite`: `boolean` (default: `false`) + +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const id = t.identifier("foo"); +const ast = t.variableDeclarator(id); + +const { code } = generate(ast); +console.log(code); // foo +``` --- -### variance +### Variance ```javascript t.variance(kind) ``` @@ -2984,35 +5298,70 @@ Aliases: `Flow` - `kind`: `"minus" | "plus"` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const kind = "minus"; +const ast = t.variance(kind); + +const { code } = generate(ast); +console.log(code); // - +``` + --- -### voidTypeAnnotation +### VoidTypeAnnotation ```javascript t.voidTypeAnnotation() ``` See also `t.isVoidTypeAnnotation(node, opts)` and `t.assertVoidTypeAnnotation(node, opts)`. -Aliases: `Flow`, `FlowType`, `FlowBaseAnnotation` +Aliases: `Flow`, `FlowBaseAnnotation`, `FlowType` +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const ast = t.voidTypeAnnotation(); + +const { code } = generate(ast); +console.log(code); // void +``` --- -### whileStatement +### WhileStatement ```javascript t.whileStatement(test, body) ``` See also `t.isWhileStatement(node, opts)` and `t.assertWhileStatement(node, opts)`. -Aliases: `Statement`, `BlockParent`, `Loop`, `While`, `Scopable` +Aliases: `BlockParent`, `Loop`, `Scopable`, `Statement`, `While` - `test`: `Expression` (required) - `body`: `BlockStatement | Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const test = t.identifier("foo"); +const body = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.whileStatement(test, body); + +const { code } = generate(ast); +console.log(code); +// while (foo) { +// return this; +// } +``` + --- -### withStatement +### WithStatement ```javascript t.withStatement(object, body) ``` @@ -3024,11 +5373,26 @@ Aliases: `Statement` - `object`: `Expression` (required) - `body`: `BlockStatement | Statement` (required) +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); + +const object = t.thisExpression(); +const body = t.blockStatement([t.returnStatement(t.thisExpression())]); +const ast = t.withStatement(object, body); + +const { code } = generate(ast); +console.log(code); +// with (this) { +// return this; +// } +``` + --- -### yieldExpression +### YieldExpression ```javascript -t.yieldExpression(argument, delegate) +t.yieldExpression() ``` See also `t.isYieldExpression(node, opts)` and `t.assertYieldExpression(node, opts)`. @@ -3038,5 +5402,14 @@ Aliases: `Expression`, `Terminatorless` - `argument`: `Expression` (default: `null`) - `delegate`: `boolean` (default: `false`) ---- +```javascript +const generate = require("@babel/generator").default; +const t = require("@babel/types"); +const ast = t.yieldExpression(); + +const { code } = generate(ast); +console.log(code); // yield +``` + +--- diff --git a/scripts/generate-docs-types-example.js b/scripts/generate-docs-types-example.js new file mode 100644 index 0000000000..5d8a7c3836 --- /dev/null +++ b/scripts/generate-docs-types-example.js @@ -0,0 +1,651 @@ +const prettier = require("prettier"); + +const generateDocTypesExample = (helper, helperName, params) => { + const log = new Log(["> HELPER", helper, "", "> PARAMS", params]); + + const requiredParams = params + .reduce((acc, cur, index, arr) => { + try { + if ( + (cur && cur.meta === "required") || + arr.slice(index).some(part => part && part.meta === "required") + ) { + return acc.concat([{ ...cur, meta: "required" }]); + } + } catch (error) { + log.push(cur); + log.push(error); + } + return acc; + }, []) + .map( + part => `const ${part.id} = ${getRightSide({ ...part, helperName })};` + ); + + log.push(["", "> REQUIRED PARAMS", requiredParams]); + + let code = [ + "const generate = require('@babel/generator').default;", + "const t = require('@babel/types');", + "", + ] + .concat(requiredParams) + .concat([ + `const ast = ${pickArgs(helper, requiredParams.length)};`, + "", + "const {code} = generate(ast);", + "code;", + ]) + .map(sanitize); + + log.push([ + "", + "> CODE", + prettier.format(code.join("\n"), { + parser: "babel", + singleQuote: false, + trailingComma: "es5", + }), + ]); + + let result; + try { + result = eval(code.join("\n")); + } catch (error) { + log.push([ + `unable to parse code for ${helper}, aborting. error:`, + error, + "", + ]); + log.write(); + identifiers.reset(); + return ""; + } + let appendResult = ""; + let blockResult = []; + + if (result.includes("\n")) { + blockResult = result.split("\n").map(s => `// ${s}`); + } else { + appendResult = ` // ${result}`; + } + + code.pop(); + code.push(`console.log(code);${appendResult}`); + code = code.concat(blockResult); + const example = prettier + .format(code.join("\n"), { + parser: "babel", + singleQuote: false, + trailingComma: "es5", + }) + .slice(0, -1); + + log.push(["", "> EXAMPLE", example]); + log.write(); + identifiers.reset(); + return example; +}; + +const pickArgs = (helper, argCount) => { + const match = helper.match(/t\.(\w+)\(([\w\s,]+)\)/); + if (match && match[1] && match[2]) { + const args = match[2].split(",").map(s => String(s).trim()); + return `t.${match[1]}(${args.slice(0, argCount).join(", ")})`; + } + return helper; +}; + +const getRightSide = ({ helperName, id, meta, type }) => { + if (meta === "required") { + return getType(helperName, type, id); + } else if (/default:\s/.test(meta)) { + return getType(helperName, meta.slice("default: ".length), id); + } +}; + +const getThreeTypes = (helperName, type, id) => { + const types = [ + getType(helperName, type, id), + getType(helperName, type, id, 1), + ]; + if (id === "consequent") { + types.push(getType(helperName, "BreakStatement")); + } else { + types.push(getType(helperName, type, id, 2)); + } + return inArray(types); +}; + +const getType = (helperName, type, id, variation = 0) => { + const isArray = (type || "").match(/^Array<(.+?)>/); + + if (isArray && isArray[1]) { + return getThreeTypes(helperName, isArray[1], id); + } + + const types = (type || "") + .split(" | ") + .map(s => String(s).trim()) + .filter(s => s); + + switch (types[0]) { + case "BlockStatement": { + const a = inArray(getType(helperName, "ReturnStatement", id)); + return `t.blockStatement(${a})`; + } + + case "BinaryExpression": { + const a = getType(helperName, "Identifier", id); + const b = getType(helperName, "NumericLiteral", id); + return `t.binaryExpression('>', ${a}, ${b})`; + } + + case "BreakStatement": + return "t.breakStatement()"; + + case "CallExpression": { + const a = getType(helperName, "Identifier", id); + const b = getType(helperName, "Identifier", id); + const c = getType(helperName, "NumericLiteral", id); + return `t.callExpression(${a}, [${b}, ${c}])`; + } + + case "ClassBody": { + const body = inArray(getType(helperName, "ClassMethod", id)); + return `t.classBody(${body})`; + } + + case "ClassMethod": { + const key = getType(helperName, "Identifier", id); + const b = getType(helperName, "Identifier", id); + const c = getType(helperName, "Identifier", id); + let body = `t.blockStatement([t.returnStatement(t.binaryExpression('>', ${b}, ${c}))])`; + let kind = "method"; + switch (variation) { + case 2: { + const leftB = `t.memberExpression(t.thisExpression(), ${b})`; + const assignB = `t.assignmentExpression("=", ${leftB}, ${b})`; + const leftC = `t.memberExpression(t.thisExpression(), ${c})`; + const assignC = `t.assignmentExpression("=", ${leftC}, ${c})`; + body = `t.blockStatement([t.expressionStatement(${assignB}), t.expressionStatement(${assignC})])`; + kind = "constructor"; + break; + } + } + return `t.classMethod("${kind}", ${key}, ${inArray([b, c])}, ${body})`; + } + + case "Declaration": + return getType(helperName, "VariableDeclaration"); + + case "DeclaredPredicate": { + const value = getType(helperName, "Flow", id); + return `t.declaredPredicate(${value})`; + } + + case "Decorator": { + const a = getType(helperName, "Expression", id); + return `t.decorator(${a})`; + } + + case "Directive": { + const value = getType(helperName, "DirectiveLiteral", id); + return `t.directive(${value})`; + } + + case "DirectiveLiteral": + return `t.directiveLiteral("${identifiers.next()}")`; + + case "ExportSpecifier": { + const local = getType(helperName, "Identifier", id); + const exported = getType(helperName, "Identifier", id); + return `t.exportSpecifier(${local}, ${exported})`; + } + + case "Expression": { + switch (id) { + case "object": + return "t.thisExpression()"; + + case "quasis": + return getType(helperName, "StringLiteral", id); + + default: + return getType(helperName, "Identifier", id); + } + } + + case "Flow": + return "t.anyTypeAnnotation()"; + + case "FlowType": + switch (variation) { + case 2: + return "t.numberTypeAnnotation()"; + + case 1: + return "t.stringTypeAnnotation()"; + + default: + return "t.anyTypeAnnotation()"; + } + + case "FunctionDeclaration": { + const ident = getType(helperName, "Identifier", id); + const params = getType(helperName, "Array", id); + const body = getType(helperName, "BlockStatement", id); + return `t.functionDeclaration(${ident}, ${params}, ${body})`; + } + + case "FunctionTypeParam": { + const name = getType(helperName, "Identifier", id); + const typeAnnotation = getType(helperName, "FlowType", id); + return `t.functionTypeParam(${name}, ${typeAnnotation})`; + } + + case "Identifier": + case "PatternLike": + case "TSEntityName": { + const a = identifiers.next(); + return `t.identifier("${a}")`; + } + + case "ImportSpecifier": { + const local = getType(helperName, "Identifier", id); + const imported = getType(helperName, "Identifier", id); + return `t.importSpecifier(${local}, ${imported})`; + } + + case "InterfaceExtends": { + const a = getType(helperName, "Identifier", id); + return `t.interfaceExtends(${a})`; + } + + case "JSXAttribute": { + const name = getType(helperName, "JSXIdentifier", id); + const value = getType(helperName, "StringLiteral", id); + return `t.jsxAttribute(${name}, ${value})`; + } + + case "JSXClosingElement": { + const name = getType(helperName, "JSXIdentifier", id); + return `t.jsxClosingElement(${name})`; + } + + case "JSXClosingFragment": + return "t.jsxClosingFragment()"; + + case "JSXElement": { + const openingElement = getType(helperName, "JSXOpeningElement", id); + const closingElement = getType(helperName, "JSXClosingElement", id); + const children = getType(helperName, "Array", id); + const selfClosing = true; + return `t.jsxElement(${openingElement}, ${closingElement}, ${children}, ${selfClosing})`; + } + + case "JSXIdentifier": { + const name = identifiers.next(); + return `t.jsxIdentifier("${name}")`; + } + + case "JSXMemberExpression": { + const object = getType(helperName, "JSXIdentifier", id); + const property = getType(helperName, "JSXIdentifier", id); + return `t.jsxMemberExpression(${object}, ${property})`; + } + + case "JSXOpeningElement": { + const name = getType(helperName, "JSXIdentifier", id); + const attributes = getType(helperName, "Array", id); + return `t.jsxOpeningElement(${name}, ${attributes})`; + } + + case "JSXOpeningFragment": + return "t.jsxOpeningFragment()"; + + case "JSXText": + return `t.jsxText("${identifiers.next()} ")`; + + case "LVal": { + const left = getType(helperName, "Identifier", id); + switch (variation) { + case 2: { + const right = getType(helperName, "NumericLiteral", id); + return `t.assignmentPattern(${left}, ${right})`; + } + + case 1: { + const right = getType(helperName, "StringLiteral", id); + return `t.assignmentPattern(${left}, ${right})`; + } + + default: + return left; + } + } + + case "NumericLiteral": + return "t.numericLiteral(1337)"; + + case "ObjectMethod": { + const key = getType(helperName, "Identifier", id); + const params = getType(helperName, "Array", id); + const body = getType(helperName, "BlockStatement", id); + return `t.objectMethod("method", ${key}, ${params}, ${body})`; + } + + case "ObjectProperty": { + const key = getType(helperName, "Identifier", id); + const value = getType(helperName, "StringLiteral", id); + return `t.objectProperty(${key}, ${value})`; + } + + case "ObjectTypeAnnotation": { + const a = getType(helperName, "ObjectTypeProperty", id); + return `t.objectTypeAnnotation([${a}])`; + } + + case "ObjectTypeProperty": { + const a = getType(helperName, "Identifier", id); + const b = getType(helperName, "FlowType", id, variation); + return `t.objectTypeProperty(${a}, ${b})`; + } + + case "PrivateName": { + const a = getType(helperName, "Identifier", id); + return `t.privateName(${a})`; + } + + case "Program": { + const body = getType(helperName, "Array", id); + return `t.program(${body})`; + } + + case "RestElement": { + const a = getType(helperName, "LVal", id); + return `t.restElement(${a})`; + } + + case "ReturnStatement": + return `t.returnStatement(t.thisExpression())`; + + case "Statement": { + return getType(helperName, "VariableDeclaration", id); + } + + case "StringLiteral": { + const str = identifiers.next(); + return `t.stringLiteral("${str}")`; + } + + case "SwitchCase": { + const test = getType(helperName, "StringLiteral", id); + const consequent = getType(helperName, "Array", "consequent"); + return `t.switchCase(${test}, ${consequent})`; + } + + case "TemplateElement": { + const value = getType(helperName, "{raw: string, cooked?: string}", id); + const tail = true; + return `t.templateElement(${value}, ${tail})`; + } + + case "TemplateLiteral": { + const quasis = inArray(getType(helperName, "TemplateElement", id)); + const expressions = getType(helperName, "Array", id); + return `t.templateLiteral(${quasis}, ${expressions})`; + } + + case "TSConstructSignatureDeclaration": { + const typeParameters = getType( + helperName, + "TSTypeParameterDeclaration", + id + ); + const parameters = getType(helperName, "Array", id); + const typeAnnotation = getType(helperName, "TSTypeAnnotation", id); + return `t.tsConstructSignatureDeclaration(${typeParameters}, ${parameters}, ${typeAnnotation})`; + } + + case "TSEnumMember": { + const a = getType(helperName, "Identifier", id); + return `t.tsEnumMember(${a})`; + } + + case "TSExpressionWithTypeArguments": { + const a = getType(helperName, "TSEntityName", id); + const b = getType(helperName, "TSTypeParameterInstantiation", id); + return `t.tsExpressionWithTypeArguments(${a}, ${b})`; + } + + case "TSInterfaceBody": { + const a = getType(helperName, "Array", id); + return `t.tsInterfaceBody(${a})`; + } + + case "TSModuleBlock": { + const a = inArray(getType(helperName, "ReturnStatement", id)); + return `t.tsModuleBlock(${a})`; + } + + case "TSType": + if (helperName === "tsIndexedAccessType") { + return getType(helperName, "Identifier", id); + } + switch (variation) { + case 2: + return "t.tsNumberKeyword()"; + + case 1: + return "t.tsStringKeyword()"; + + default: + return "t.tsAnyKeyword()"; + } + + case "TSTypeAnnotation": { + const a = getType(helperName, "TSType", id); + return `t.tsTypeAnnotation(${a})`; + } + + case "TSTypeElement": { + const a = inArray(getType(helperName, "Identifier", id)); + return `t.tsIndexSignature(${a})`; + } + + case "TSTypeParameter": { + const a = getType(helperName, "TSType", id, variation); + return `t.tsTypeParameter(${a}, ${a})`; + // FIXME: use this when https://github.com/babel/babel/issues/10317 is addressed + // return `t.tsTypeParameter(${a}, ${a}, "${identifiers.next()}")`; + } + + case "TSTypeParameterDeclaration": { + const a = getType(helperName, "Array", id); + return `t.tsTypeParameterDeclaration(${a})`; + } + + case "TSTypeParameterInstantiation": { + const a = getType(helperName, "Array"); + return `t.tsTypeParameterInstantiation(${a})`; + } + + case "TypeAnnotation": { + const typeAnnotation = getType(helperName, "FlowType", id, variation); + return `t.typeAnnotation(${typeAnnotation})`; + } + + case "TypeParameter": { + const bound = getType(helperName, "TypeAnnotation", id, variation); + const def = getType(helperName, "FlowType", id, variation); + const variance = getType(helperName, "Variance", id, variation); + return `t.typeParameter(${bound}, ${def}, ${variance})`; + } + + case "TypeParameterDeclaration": { + const params = getType(helperName, "Array", id, variation); + return `t.typeParameterDeclaration(${params})`; + } + + case "TypeParameterInstantiation": + return "t.typeParameterInstantiation([t.anyTypeAnnotation()])"; + + case "VariableDeclaration": { + const a = getType(helperName, "VariableDeclarator", id); + return `t.variableDeclaration("const", [${a}])`; + } + + case "VariableDeclarator": { + const a = getType(helperName, "Identifier", id); + const b = getType(helperName, "Identifier", id); + return `t.variableDeclarator(${a}, ${b})`; + } + + case "Variance": { + return `t.variance("${variation % 2 === 0 ? "minus" : "plus"}")`; + } + + case "{raw: string, cooked?: string}": + return `{ raw: "${identifiers.next()}" }`; + + // primitives + + case "any": + return `"${identifiers.next()}"`; + + case "boolean": + return "true"; + + case "`'method'`": + return '"method"'; + + case "`null`": + return "null"; + + case "number": + return 0; + + case "string": + switch (id) { + case "operator": + return '"="'; + + case "value": + return '"0"'; + + case "pattern": + return '"w+"'; + + case "name": + return `"${identifiers.next()}"`; + + default: + return `string ${id}`; + } + + case '"++"': + return '"++"'; + + case '"||"': + return '"||"'; + + default: + switch (id) { + case "callee": + case "comments": + case "key": + case "property": + case "tokens": + case "value": { + const a = identifiers.next(); + return `t.identifier("${a}")`; + } + + case "object": { + const a = identifiers.next(); + const b = identifiers.next(); + return `t.objectExpression([t.objectProperty(t.stringLiteral("${a}"), t.stringLiteral("${b}"))])`; + } + + case "operator": + return '"+"'; + + case "selfClosing": + return "false"; + + default: + return types[0] || id; + } + } +}; + +const inArray = elements => + Array.isArray(elements) ? `[${elements.join(", ")}]` : `[${elements}]`; + +const sanitize = s => + s.replace(/\bextends\b/g, "_extends").replace(/\sdefault/g, " _default"); + +class Log { + constructor(buffer = []) { + this.buffer = buffer; + } + + push(lines) { + if (Array.isArray(lines)) { + this.buffer = this.buffer.concat(lines); + } else { + this.buffer.push(lines); + } + } + + write() { + for (const line of this.buffer) { + console.log(line); + } + } +} + +class Identifier { + constructor() { + this.keywords = [ + "foo", + "bar", + "baz", + "qux", + "quux", + "corge", + "grault", + "garply", + "waldo", + "fred", + "plugh", + "xyzzy", + "thud", + ]; + this.reset(); + } + + next() { + return this.copy.shift(); + } + + reset() { + this.copy = Array.from(this.keywords).concat( + this.keywords + .map((key, _index, array) => { + const use = key.slice(0, 1).toUpperCase() + key.slice(1); + return array + .map(p => (p === key ? undefined : `${p}${use}`)) + .filter(Boolean); + }) + .reduce((acc, cur) => acc.concat(cur), []) + ); + } +} + +const identifiers = new Identifier(); + +module.exports = { + generateDocTypesExample, +}; diff --git a/scripts/generate-docs-types.js b/scripts/generate-docs-types.js new file mode 100644 index 0000000000..8d3c68573b --- /dev/null +++ b/scripts/generate-docs-types.js @@ -0,0 +1,328 @@ +const fs = require("fs").promises; +const path = require("path"); +const { generateDocTypesExample } = require("./generate-docs-types-example"); + +const argv = Array.from(process.argv).slice(2); +const grabAgument = rgx => + argv.filter(arg => rgx.test(arg)).map(arg => arg.split("=")[1])[0]; + +const typesArg = grabAgument(/^--types=/); + +let t = require("@babel/types"); + +if (typesArg) { + try { + t = require(typesArg); + } catch (error) { + console.log(`unable to get --types=${typesArg}`); + console.log(error); + process.exit(1); + } +} + +if (!Object.hasOwnProperty.call(t, "noop")) { + console.log("unable to load types"); + process.exit(1); +} + +const callRegex = /(\w+)(\??:)\s(.+)/; +const signatureRegex = /\((.*)\):\s(\w+)/i; +const typeRegex = /:\s(\w+);$/i; + +const signature = (type, args) => { + const required = args + .map(line => { + const match = line.match(callRegex); + if (!match || match[2] === "?:") { + return; + } + return match[1].slice(0, 1) === "_" ? match[1].slice(1) : match[1]; + }) + .filter(s => Boolean(s)) + .join(", "); + return ["```javascript", `t.${type}(${required})`, "```", ""]; +}; + +const removeNullUndefined = type => { + if (type.includes("null") || type.includes("undefined")) { + const types = type.split(" | "); + return types.filter(a => a !== "null" && a !== "undefined").join(" | "); + } + return type; +}; + +const typeOverwrites = { + classMethod: { + key: "if computed then `Expression` else `Identifier | Literal`", + }, + memberExpression: { + property: "if computed then `Expression` else `Identifier`", + }, + objectMethod: { + key: "if computed then `Expression` else `Identifier | Literal`", + }, + objectProperty: { + key: "if computed then `Expression` else `Identifier | Literal`", + }, +}; + +const requirementOverwrites = { + classMethod: { + kind: "default: `method`", + }, + program: { + sourceType: "default: `'script'`", + }, + regExpLiteral: { + flags: 'default: `""`', + }, + tsDeclareMethod: { + kind: "default: `'method'`", + }, +}; + +const getRequirement = (required, id, type, types) => { + if ( + requirementOverwrites.hasOwnProperty(type) && + requirementOverwrites[type].hasOwnProperty(id) + ) { + return requirementOverwrites[type][id]; + } + + if (required) { + return "required"; + } + + if (types.slice(0, "`Array<".length) === "`Array<") { + return "default: `[]`"; + } + + switch (types) { + case "`boolean`": + return "default: `false`"; + + case "`string`": + return 'default: `""`'; + + default: + return "default: `null`"; + } +}; + +const getFirstType = input => { + let types = input; + if (input.slice(0, 1) === "`" && input.slice(-1) === "`") { + types = input.slice(1, -1); + } + return types.split("|")[0].trim(); +}; + +const idMetaTypes = type => argLine => { + const match = argLine.match(callRegex); + const id = match[1].slice(0, 1) === "_" ? match[1].slice(1) : match[1]; + const required = match[2] === ":"; + const types = + typeOverwrites.hasOwnProperty(type) && + typeOverwrites[type].hasOwnProperty(id) + ? typeOverwrites[type][id] + : "`" + removeNullUndefined(match[3]) + "`"; + return { + id, + meta: getRequirement(required, id, type, types), + type: getFirstType(types), + types: types, + }; +}; + +const seeAlso = type => { + const see = { assert: "", is: "" }; + const is = String(`is${type}`).toLowerCase(); + const assert = String(`assert${type}`).toLocaleLowerCase(); + for (const type in t) { + if (type.toLowerCase() === assert) { + see.assert = type; + } + if (type.toLowerCase() === is) { + see.is = type; + } + } + return `See also \`t.${see.is}(node, opts)\` and \`t.${ + see.assert + }(node, opts)\`.`; +}; + +const findAliases = typeDefLines => { + const aliasBlock = []; + let collect = false; + for (const line of typeDefLines) { + if (line === "export interface Aliases {") { + collect = true; + } else if (collect && line === "}") { + collect = false; + } else if (collect) { + const parts = line.split(":"); + aliasBlock.push(parts[0].trim()); + } + } + const aliases = {}; + aliasBlock.forEach(alias => { + const types = typeDefLines.filter(l => + new RegExp(`^export type ${alias} = `).test(l) + ); + const names = types[0] + .slice(`export type ${alias} = `.length) + .split(" | ") + .map(s => (s.slice(-1) === ";" ? s.slice(0, -1) : s)); + for (const name of names) { + if (aliases.hasOwnProperty(name)) { + aliases[name].push(alias); + } else { + aliases[name] = [alias]; + } + } + }); + for (const key in aliases) { + aliases[key].sort((a, b) => a.localeCompare(b)); + } + return aliases; +}; + +const getArgumentsAndReturnType = def => { + const match = def.match(signatureRegex); + if (match && match[1] && match[2]) { + return [match[1].split(",").map(s => String(s).trim()), match[2]]; + } else if (def.includes("()")) { + const r = def.match(typeRegex); + if (r && r[1]) { + return [[], r[1]]; + } + } + return [[], "unknown"]; +}; + +(async () => { + const typeDefs = await fs.readFile( + typesArg + ? path.resolve(__dirname, "..", typesArg) + : path.resolve( + __dirname, + "..", + "node_modules", + "@babel", + "types", + "lib", + "index.d.ts" + ), + "utf-8" + ); + const typeDefLines = typeDefs.split("\n"); + const aliases = findAliases(typeDefLines); + const sections = []; + + for (const type in t) { + const isBuilder = /_builder\.default/.test(t[type].toString()); + if (!isBuilder) { + continue; + } + + const findFunction = typeDefLines.filter(l => + new RegExp(`^export function ${type}\\(`).test(l) + ); + + if (findFunction.length === 0 && (type === "import" || type === "super")) { + findFunction.push( + `export function ${type}(): ${type + .slice(0, 1) + .toUpperCase()}${type.slice(1)};` + ); + } + + if (findFunction.length === 0) { + continue; + } + + const section = { + aliases: ["Aliases: none", ""], + args: [], + example: "", + header: [`### ${type}`], + returnType: "unknown", + seeAlso: [seeAlso(type), ""], + signature: [], + type, + }; + + const [args, returnType] = getArgumentsAndReturnType(findFunction[0]); + + if (returnType) { + section.header = [`### ${returnType}`]; + } + + const params = args.length ? args.map(idMetaTypes(type)) : []; + section.args = params.length + ? params + .map(({ id, meta, types }) => ` - \`${id}\`: ${types} (${meta})`) + .concat([""]) + : []; + section.signature = signature(type, args, returnType); + section.returnType = returnType; + if (aliases.hasOwnProperty(section.returnType)) { + section.aliases = [ + `Aliases: \`${aliases[section.returnType].join("`, `")}\``, + "", + ]; + } + const example = generateDocTypesExample( + section.signature[1], + returnType, + params + ); + section.example = example ? ["```javascript", example, "```", ""] : []; + sections.push(section); + seeAlso(type); + } + + const overWriteId = grabAgument(/^--babel-types-id=/); + + const content = [ + "---", + `id: ${overWriteId ? overWriteId : "babel-types"}`, + "title: @babel/types", + "sidebar_label: types", + ] + .concat(overWriteId ? ["original_id: babel-types"] : []) + .concat([ + "---", + "", + "## Install", + "", + "```sh", + "npm install --save-dev @babel/types", + "```", + "", + "## API", + "", + ]) + .concat( + sections + .sort((a, b) => a.type.localeCompare(b.type)) + .reduce( + (acc, cur) => + acc.concat( + cur.header + .concat(cur.signature) + .concat(cur.seeAlso) + .concat(cur.aliases) + .concat(cur.args) + .concat(cur.example) + .concat(["---", ""]) + ), + [] + ) + ); + + await fs.writeFile( + path.resolve(__dirname, "..", "docs", "types.md"), + content.join("\n") + ); +})();