@babel/types#arrayExpression TypeScript Examples
The following examples show how to use
@babel/types#arrayExpression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: pathCompiler.ts From engine with MIT License | 6 votes |
pathCompiler = (
babel: typeof Babel,
path: InvokablePath
): ArrayExpression => {
const t = babel.types;
const parts = path.map((x) => {
let type = t.objectProperty(t.identifier("type"), t.stringLiteral(x.type));
let value = t.objectProperty(t.identifier("ignored"), t.nullLiteral());
if (x.type === ValueTypes.CONST) {
let paramValue;
if (x.value.__node__) {
paramValue = x.value.__node__;
} else {
paramValue = t.stringLiteral(x.value.toString());
}
value = t.objectProperty(t.identifier("value"), paramValue);
} else if (
x.type === ValueTypes.INTERNAL ||
x.type === ValueTypes.EXTERNAL
) {
const path = x.path.map((y: string) => t.stringLiteral(y));
value = t.objectProperty(t.identifier("path"), t.arrayExpression(path));
} else if (x.type === ValueTypes.INVOKE) {
const path = x.path.map((y: string) => t.stringLiteral(y));
value = t.objectProperty(t.identifier("path"), t.arrayExpression(path));
}
return t.objectExpression([type, value]);
});
const result = t.arrayExpression(parts);
return result;
}
Example #2
Source File: pathCompiler.ts From engine with MIT License | 6 votes |
pathCompiler = (path: InvokablePath): ArrayExpression => {
const parts = path.map((x) => {
let type = objectProperty(identifier("type"), stringLiteral(x.type));
let value = objectProperty(identifier("ignored"), nullLiteral());
if (x.type === ValueTypes.CONST) {
let paramValue;
if (x.value.__node__) {
paramValue = x.value.__node__;
} else {
paramValue = stringLiteral(x.value.toString());
}
value = objectProperty(identifier("value"), paramValue);
} else if (
x.type === ValueTypes.INTERNAL ||
x.type === ValueTypes.EXTERNAL
) {
const path = x.path.map((y: string) => stringLiteral(y));
value = objectProperty(identifier("path"), arrayExpression(path));
} else if (x.type === ValueTypes.INVOKE) {
const path = x.path.map((y: string) => stringLiteral(y));
value = objectProperty(identifier("path"), arrayExpression(path));
}
return objectExpression([type, value]);
});
const result = arrayExpression(parts);
return result;
}
Example #3
Source File: jsx.ts From react-optimized-image with MIT License | 6 votes |
getNumberedArrayAttribute = (path: NodePath<JSXElement>, attributeName: string): number[] | undefined => {
const attribute = getAttribute(path, attributeName);
if (attribute) {
if (
attribute.node.value &&
attribute.node.value.type === 'JSXExpressionContainer' &&
attribute.node.value.expression.type === 'ArrayExpression'
) {
const values: number[] = [];
attribute.node.value.expression.elements.forEach((element, i) => {
if (element && element.type === 'NumericLiteral') {
values.push(element.value);
} else if (element) {
// todo: better error message with link to docs when ready & create test for this error
throw (((attribute.get('value') as NodePath<JSXExpressionContainer>).get('expression') as NodePath<
ArrayExpression
>).get(`elements.${i}`) as NodePath).buildCodeFrameError('Only static number values are allowed');
}
});
return values;
}
// todo: better error message with link to docs when ready & create test for this error
throw attribute.get('value').buildCodeFrameError('Only static array with number values is allowed');
}
return undefined;
}
Example #4
Source File: webpackParser.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 6 votes |
private parseArray(file: File, ast: NodePath<ArrayExpression>, modules: Module[]): void {
ast.get('elements').forEach((element, i) => {
if (!element.isFunctionExpression()) return;
if (element.node.body.body.length === 0) return;
const dependencyValues: number[] = [];
const requireIdentifer = element.node.params[2];
if (isIdentifier(requireIdentifer)) {
element.traverse({
CallExpression: (dependencyPath) => {
if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
}
},
});
}
const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
newModule.calculateFields();
modules[i] = newModule;
});
}
Example #5
Source File: webpackParser.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 6 votes |
private parseArray(file: File, ast: NodePath<ArrayExpression>, modules: Module[]): void {
ast.get('elements').forEach((element, i) => {
if (!element.isFunctionExpression()) return;
if (element.node.body.body.length === 0) return;
const dependencyValues: number[] = [];
const requireIdentifer = element.node.params[2];
if (isIdentifier(requireIdentifer)) {
element.traverse({
CallExpression: (dependencyPath) => {
if (!isIdentifier(dependencyPath.node.callee) || !isNumericLiteral(dependencyPath.node.arguments[0])) return;
if (dependencyPath.scope.bindingIdentifierEquals(dependencyPath.node.callee.name, requireIdentifer)) {
dependencyValues[dependencyPath.node.arguments[0].value] = dependencyPath.node.arguments[0].value;
}
},
});
}
const newModule = new Module(file, element, i, dependencyValues, this.PARAM_MAPPING);
newModule.calculateFields();
modules[i] = newModule;
});
}
Example #6
Source File: funcOperationCompiler.ts From engine with MIT License | 6 votes |
funcOperationCompiler = (op: FuncOperation): ObjectExpression => {
const type = objectProperty(identifier("type"), stringLiteral(op.type));
const paramsList = op.value.params.map((x) => {
const type = objectProperty(identifier("type"), stringLiteral(x.type));
const value = objectProperty(identifier("value"), stringLiteral("value"));
return objectExpression([type, value]);
});
const fn = objectProperty(identifier("fn"), stringLiteral("fn"));
const paramsArray = arrayExpression(paramsList);
const params = objectProperty(identifier("params"), paramsArray);
const internal = objectExpression([params, fn]);
const value = objectProperty(identifier("value"), internal);
return objectExpression([type, value]);
}
Example #7
Source File: pathCompiler.ts From engine with MIT License | 6 votes |
pathCompiler = (path: InvokablePath): ArrayExpression => {
const parts = path.map((x) => {
let type = objectProperty(identifier("type"), stringLiteral(x.type));
let value = objectProperty(identifier("ignored"), nullLiteral());
if (x.type === ValueTypes.CONST) {
let paramValue;
if (x.value.__node__) {
paramValue = x.value.__node__;
} else {
paramValue = stringLiteral(x.value.toString());
}
value = objectProperty(identifier("value"), paramValue);
} else if (
x.type === ValueTypes.INTERNAL ||
x.type === ValueTypes.EXTERNAL
) {
const path = x.path.map((y: string) => stringLiteral(y));
value = objectProperty(identifier("path"), arrayExpression(path));
} else if (x.type === ValueTypes.INVOKE) {
const path = x.path.map((y: string) => stringLiteral(y));
value = objectProperty(identifier("path"), arrayExpression(path));
}
return objectExpression([type, value]);
});
const result = arrayExpression(parts);
return result;
}
Example #8
Source File: rawObjectCompiler.ts From engine with MIT License | 6 votes |
rawObjectCompiler = (obj: any): ObjectExpression => {
const props = Object.keys(obj).reduce((acc, x) => {
let val: any = obj[x];
let result;
if (isString(val)) {
result = stringLiteral(val);
} else if (typeof val === "number") {
result = numericLiteral(toNumber(val));
} else if (isArray(val)) {
let list = val.map((x) => stringLiteral(x));
result = arrayExpression(list);
} else if (isPlainObject(val)) {
result = rawObjectCompiler(val);
} else {
throw new Error("Meta type for " + val + " not supported");
}
if (result) {
acc.push(objectProperty(identifier(x), result));
}
return acc;
}, [] as ObjectProperty[]);
return objectExpression(props);
}
Example #9
Source File: babel-polyfill.ts From nota with MIT License | 5 votes |
arrayExpression = (
elements: Array<null | Expression | SpreadElement>
): ArrayExpression => ({
type: "ArrayExpression",
elements,
...baseNode,
})
Example #10
Source File: valueOperationCompiler.ts From engine with MIT License | 5 votes |
valueOperationCompiler = (
op: ValueOperation
): ObjectExpression => {
let value = objectProperty(identifier("path"), stringLiteral("___"));
const type = objectProperty(identifier("type"), stringLiteral(op.type));
if (op.value.type === ValueTypes.CONST) {
const val = op.value.value;
let valType;
if (val && val.__node__) {
valType = val.__node__;
} else if (typeof val === "string") {
valType = stringLiteral(val);
} else if (typeof val === "number") {
valType = numericLiteral(val);
} else if (typeof val === "boolean") {
valType = booleanLiteral(val);
} else {
throw new Error("Value type not supported yet: " + typeof val);
}
value = objectProperty(
identifier("value"),
objectExpression([
objectProperty(identifier("type"), stringLiteral(ValueTypes.CONST)),
objectProperty(identifier("value"), valType),
])
);
} else if (
op.value.type === ValueTypes.EXTERNAL ||
op.value.type === ValueTypes.INTERNAL
) {
const path = arrayExpression(op.value.path.map((x) => stringLiteral(x)));
value = objectProperty(
identifier("value"),
objectExpression([
objectProperty(identifier("type"), stringLiteral(op.value.type)),
objectProperty(identifier("path"), path),
])
);
}
return objectExpression([type, value]);
}
Example #11
Source File: add-exports-array.ts From mpflow with MIT License | 4 votes |
/**
* 向配置文件如
* module.exports = { plugins: [] }
* 中的 plugins 添加插件信息
*/
export default function (api: typeof babel, options: Options): PluginObj {
const { types: t, template } = api
const { fieldName, items } = options
let pluginsArrayExpression: NodePath<ArrayExpression> | null
if (!fieldName || !items || !items.length)
return {
name: 'add-exports-array',
visitor: {},
}
return {
name: 'add-exports-array',
pre() {
pluginsArrayExpression = null
},
visitor: {
AssignmentExpression(p) {
// 寻找 module.exports = { plugins: [] }
if (
!m
.assignmentExpression(
'=',
m.memberExpression(m.identifier('module'), m.identifier('exports')),
m.objectExpression(),
)
.match(p.node)
)
return
const objectExpression = p.get('right') as NodePath<ObjectExpression>
const properties = objectExpression.get('properties')
properties.forEach(property => {
if (
!m
.objectProperty(m.or(m.stringLiteral(fieldName), m.identifier(fieldName)), m.arrayExpression())
.match(property.node)
)
return
pluginsArrayExpression = property.get('value') as NodePath<ArrayExpression>
})
},
Program: {
exit(p) {
if (!pluginsArrayExpression) {
// 如果找不到 module.exports = { plugins: [] }
// 则在末尾加一句 exports.plugins = (exports.plugins || []).concat([])
const statement = template.statement(`
exports.FIELD_NAME = (exports.FIELD_NAME || []).concat([]);
`)({
FIELD_NAME: t.identifier(fieldName),
})
const [statementPath] = p.pushContainer('body', statement)
pluginsArrayExpression = statementPath.get('expression.right.arguments.0') as NodePath<ArrayExpression>
}
const targetArray = pluginsArrayExpression
// 添加 item
items.forEach(item => {
const [pluginName, option] = Array.isArray(item) ? item : [item]
if (!option) {
targetArray.pushContainer('elements', t.stringLiteral(pluginName))
} else {
targetArray.pushContainer(
'elements',
t.arrayExpression([t.stringLiteral(pluginName), template.expression(JSON.stringify(option))()]),
)
}
})
},
},
},
}
}