How to use the arrayPattern function from @babel/types
Find comprehensive JavaScript @babel/types.arrayPattern code examples handpicked from public code repositorys.
@babel/types.arrayPattern is a module that provides utilities for working with array patterns in Babel ASTs.
60 61 62 63 64 65 66 67 68 69
} }); // 生成useState _self.state.forEach(item => { const decl = t.arrayPattern([t.identifier(item.key), t.identifier(`set${item.key[0].toUpperCase()}${item.key.slice(1)}`)]); const call = t.callExpression(t.identifier("useState"), [item.value]); // 防止命名重复的问题,暂时用var _self.componentBody.unshift(t.variableDeclaration("var", [t.variableDeclarator(decl, call)])); });
69 70 71 72 73 74 75 76 77 78
_self.handleClassFn(path) } }) // 生成useState _self.state.forEach((item) => { const decl = t.arrayPattern([ t.identifier(item.key), t.identifier(`set${item.key[0].toUpperCase()}${item.key.slice(1)}`) ]) const call = t.callExpression(t.identifier('useState'), [item.value])
+ 5 other calls in file
How does @babel/types.arrayPattern work?
@babel/types.arrayPattern is a module that exports several functions that provide utilities for working with array patterns in Babel ASTs. An array pattern is a pattern that matches an array in destructuring. For example, the following code destructures an array into two variables: javascript Copy code {{{{{{{ const [x, y] = [1, 2]; In this case, the pattern [x, y] matches the array [1, 2]. The @babel/types.arrayPattern module provides several utility functions for working with array patterns in Babel ASTs. For example: arrayPattern(elements, typeAnnotation): Creates an AST node that represents an array pattern with the given elements and type annotation. isArrayPattern(node): Checks whether the given AST node represents an array pattern. getArrayPatternElements(node): Gets an array of AST nodes that represent the elements of the given array pattern node. These functions can be used to create, manipulate, and analyze array pattern AST nodes in Babel.
28 29 30 31 32 33 34 35 36 37 38 39
var visitors = {}; var expTracker = []; var useEffectNode = null; var _useStateTemplate = function _useStateTemplate(initValueNode, idText) { // const leftExpression = types.arrayPattern([ // types.identifier(idText), types.identifier("".concat(setStatePrefix).concat(idText)); // ]); var objectConstruct = types.objectExpression([types.objectProperty(types.identifier(idText), initValueNode)]);
7 8 9 10 11 12 13 14 15 16
let hookAst = t.variableDeclaration( "const", [ t.variableDeclarator( t.arrayPattern([ t.identifier('formData'), t.identifier('setFormData') ]), t.callExpression(
Ai Example
1 2 3 4 5
const t = require("@babel/types"); const pattern = t.arrayPattern([t.identifier("x"), t.identifier("y")], null); console.log(pattern);
This code would output the following AST node: json Copy code
@babel/types.identifier is the most popular function in @babel/types (20936 examples)