Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return expression.getProperties().map(property => {
if (TypeGuards.isPropertyAssignment(property)) {
const name = property
.getNameNode()
.getSymbolOrThrow()
.getEscapedName();
return {
name,
expression: parseExpression(property.getInitializerOrThrow())
};
} else {
throw new Error("expected property assignment");
}
});
}
return expression.getElements().map(exp => {
if (TypeGuards.isObjectLiteralExpression(exp)) {
const nameProperty = exp.getPropertyOrThrow("name");
if (!TypeGuards.isPropertyAssignment(nameProperty)) {
throw new Error("expected property assignment");
}
const name = nameProperty
.getInitializerIfKindOrThrow(ts.SyntaxKind.StringLiteral)
.getLiteralValue();
const paramsProperty = extractOptionalObjectProperty(exp, "params");
const params =
paramsProperty && objectExpressionToProperties(paramsProperty.value);
return {
name,
params
};
} else {
throw new Error("expected object literal expression");
export function extractObjectProperty(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): Locatable {
const property = objectLiteral.getPropertyOrThrow(propertyName);
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
const value = property.getInitializerIfKindOrThrow(
ts.SyntaxKind.ObjectLiteralExpression
);
const location = property.getSourceFile().getFilePath();
const line = value.getStartLineNumber();
return { value, location, line };
}
export function extractStringProperty(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): string {
const property = objectLiteral.getPropertyOrThrow(propertyName);
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
return property
.getInitializerIfKindOrThrow(ts.SyntaxKind.StringLiteral)
.getLiteralText();
}
export function extractOptionalObjectProperty(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): Locatable | undefined {
const property = objectLiteral.getProperty(propertyName);
if (property) {
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
const value = property.getInitializerIfKindOrThrow(
ts.SyntaxKind.ObjectLiteralExpression
);
const location = property.getSourceFile().getFilePath();
const line = value.getStartLineNumber();
return { value, location, line };
} else {
return undefined;
}
}
.map(property => {
if (TypeGuards.isPropertyAssignment(property)) {
const nameNode = property.getNameNode();
let name;
if (TypeGuards.isIdentifier(nameNode)) {
name = nameNode.getText();
} else if (TypeGuards.isStringLiteral(nameNode)) {
name = nameNode.getLiteralValue();
} else {
throw new Error(
"only identifiers and string literals are valid object property keys"
);
}
return {
name,
expression: parseExpression(property.getInitializerOrThrow())
};
} else {
export function extractBooleanProperty(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): Locatable {
const property = objectLiteral.getPropertyOrThrow(propertyName);
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
const literal = property.getInitializerOrThrow();
if (!TypeGuards.isBooleanLiteral(literal)) {
throw new Error("expected boolean literal");
}
const value = literal.getLiteralValue();
const location = property.getSourceFile().getFilePath();
const line = literal.getStartLineNumber();
return { value, location, line };
}
export function extractStringPropertyValueLocatable(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): Locatable {
const property = objectLiteral.getPropertyOrThrow(propertyName);
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
const literal = property.getInitializerIfKindOrThrow(
ts.SyntaxKind.StringLiteral
);
const value = literal.getLiteralText();
const location = property.getSourceFile().getFilePath();
const line = literal.getStartLineNumber();
return { value, location, line };
}
export function extractOptionalArrayProperty(
objectLiteral: ObjectLiteralExpression,
propertyName: string
): Locatable | undefined {
const property = objectLiteral.getProperty(propertyName);
if (property) {
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
const value = property.getInitializerIfKindOrThrow(
ts.SyntaxKind.ArrayLiteralExpression
);
const location = property.getSourceFile().getFilePath();
const line = value.getStartLineNumber();
return { value, location, line };
} else {
return undefined;
}
}
export function getObjLiteralProp(
objectLiteral: ObjectLiteralExpression,
propertyName: Extract
): PropertyAssignment | undefined {
const property = objectLiteral.getProperty(propertyName);
if (!property) {
return undefined;
}
if (!TypeGuards.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
return property;
}