Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseValue(value: any) {
switch (value.kind) {
case 'BooleanValue':
return ts.createLiteral(value.value);
case 'IntValue':
return ts.createLiteral(parseInt(value.value, 10));
case 'FloatValue':
return ts.createLiteral(parseFloat(value.value));
case 'StringValue':
return ts.createLiteral(value.value);
case 'EnumValue':
return ts.createLiteral(value.value);
case 'ListValue':
return ts.createArrayLiteral(value.values.map((item: any) => parseValue(item)), /* multiLine */ true);
default:
throw new Error(
'TSTransformRelay: Unsupported literal type `' + value.kind + '`.',
);
}
}
// static reaodnly _capnp = { displayName: 'MyStruct', id: '4732bab4310f81', size = new __O(8, 8) };
members.push(
ts.createProperty(
__,
[STATIC, READONLY],
"_capnp",
__,
__,
ts.createObjectLiteral(
[
ts.createPropertyAssignment(
"displayName",
ts.createLiteral(displayNamePrefix)
),
ts.createPropertyAssignment("id", ts.createLiteral(nodeIdHex)),
ts.createPropertyAssignment(
"size",
ts.createNew(OBJECT_SIZE, __, [
ts.createNumericLiteral(dataByteLength.toString()),
ts.createNumericLiteral(pointerCount.toString())
])
)
].concat(defaultValues)
)
)
);
// private static _ConcreteListClass: MyStruct_ConcreteListClass;
members.push(...concreteLists.map(f => createConcreteListProperty(ctx, f)));
// getFoo() { ... } initFoo() { ... } setFoo() { ... }
function createTslibImport(
name: string,
aliases?: string,
useRequire = false,
): ts.VariableStatement | ts.ImportDeclaration {
if (useRequire) {
// Use `var __helper = /*@__PURE__*/ require("tslib").__helper`.
const requireCall = ts.createCall(ts.createIdentifier('require'), undefined,
[ts.createLiteral('tslib')]);
const pureRequireCall = ts.addSyntheticLeadingComment(
requireCall, ts.SyntaxKind.MultiLineCommentTrivia, '@__PURE__', false);
const helperAccess = ts.createPropertyAccess(pureRequireCall, name);
const variableDeclaration = ts.createVariableDeclaration(aliases || name, undefined, helperAccess);
const variableStatement = ts.createVariableStatement(undefined, [variableDeclaration]);
return variableStatement;
} else {
// Use `import { __helper } from "tslib"`.
const namedImports = ts.createNamedImports([
ts.createImportSpecifier(aliases ? ts.createIdentifier(name) : undefined, ts.createIdentifier(aliases || name)),
]);
const importClause = ts.createImportClause(undefined, namedImports);
const newNode = ts.createImportDeclaration(undefined, undefined, importClause,
ts.createLiteral('tslib'));
propsDecoratedMeta.map(meta =>
ts.createStatement(
ts.createAssignment(
ts.createPropertyAccess(ts.createThis(), meta.componentPropName),
ts.createElementAccess(state, ts.createLiteral(meta.statePropName))
)
)
),
function setDisplayName(d: ComponentDoc): ts.Statement {
return insertTsIgnoreBeforeStatement(
ts.createStatement(
ts.createBinary(
ts.createPropertyAccess(
ts.createIdentifier(d.displayName),
ts.createIdentifier("displayName"),
),
ts.SyntaxKind.EqualsToken,
ts.createLiteral(d.displayName),
),
),
);
}
function createRequireExpression(node: ts.Expression, loader?: string): ts.Expression {
const url = getResourceUrl(node, loader);
if (!url) {
return node;
}
const callExpression = ts.createCall(
ts.createIdentifier('require'),
undefined,
[ts.createLiteral(url)],
);
return ts.createPropertyAccess(
ts.createCall(
ts.setEmitFlags(
ts.createIdentifier('__importDefault'),
ts.EmitFlags.HelperName | ts.EmitFlags.AdviseOnEmitNode,
),
undefined,
[callExpression],
),
'default',
);
}
function replace(prop: ts.PropertyDeclaration, functionName: string, arg: string, newMembers: ts.ClassElement[]) {
const memberIndex = newMembers.findIndex(m => m === prop);
newMembers[memberIndex] = ts.createProperty(
undefined,
undefined,
prop.name.getText(),
undefined,
undefined,
ts.createCall(
ts.createIdentifier(functionName),
undefined,
[
ts.createLiteral(arg),
ts.createThis()
]
)
);
}
visitJSDocCommentStmt(stmt: JSDocCommentStmt, context: Context): ts.NotEmittedStatement {
const commentStmt = ts.createNotEmittedStatement(ts.createLiteral(''));
const text = stmt.toString();
const kind = ts.SyntaxKind.MultiLineCommentTrivia;
ts.setSyntheticLeadingComments(commentStmt, [{kind, text, pos: -1, end: -1}]);
return commentStmt;
}
(typeExpr: ts.TypeNode, view): ts.TypeNode => {
return ts.createConditionalTypeNode(
ts.createTypeReferenceNode(VIEW_NAME_TYPE_PARAMETER, undefined),
ts.createLiteralTypeNode(
ts.createLiteral(view.name)
),
createPickPropertiesType(className, view, allowedAttrs),
typeExpr
)
},
ts.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword)
function createTwizInstrumentStatement(name: string, fileOffset: number, filename: string, opts: IExtraOptions) {
return ts.createStatement(
ts.createCall(
ts.createIdentifier('$_$twiz'),
[],
[
ts.createLiteral(name),
ts.createIdentifier(name),
ts.createNumericLiteral(fileOffset.toString()),
ts.createLiteral(filename),
ts.createLiteral(JSON.stringify(opts)),
],
),
);
}