Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function maybeRewriteDeclareModuleId(
original: ts.Statement, call: ts.CallExpression): ts.Statement|null {
// Verify that the call is a call to goog.declareModuleId(...).
if (!ts.isPropertyAccessExpression(call.expression)) {
return null;
}
const propAccess = call.expression;
if (propAccess.name.escapedText !== 'declareModuleId') {
return null;
}
if (!ts.isIdentifier(propAccess.expression) ||
propAccess.expression.escapedText !== 'goog') {
return null;
}
// Verify the call takes a single string argument and grab it.
if (call.arguments.length !== 1) {
return null;
}
const arg = call.arguments[0];
if (!ts.isStringLiteral(arg)) {
return null;
}
const moduleId = arg.text;
// replace goog.declareModuleId['foo.bar'] with:
// goog.loadedModules_['foo.bar'] = {
// exports: exports,
type: typeChecker.getTypeAtLocation(node.parent.parent.right),
};
}
// Case:
// declare class Container { values: T; }
// type Values = {};
// const container = new Container({});
// container.values.laterAssigned = true;
// Case:
// declare class Container { values: T; }
// type Values = {};
// new Container({}).values.quicklyAssigned = true;
if (
ts.isPropertyAccessExpression(node.parent.parent) &&
ts.isIdentifier(node.parent.parent.name) &&
isNodeAssigningBinaryExpression(node.parent.parent.parent) &&
typeChecker.getTypeAtLocation(node.parent) === originalType
) {
return {
name: node.parent.parent.name.getText(),
type: typeChecker.getTypeAtLocation(node.parent.parent.parent.right),
};
}
}
return undefined;
};
private _isReferenceToHammerConfigToken(expr: ts.Expression) {
const unwrapped = unwrapExpression(expr);
if (ts.isIdentifier(unwrapped)) {
return this._hammerConfigTokenReferences.some(r => r.node === unwrapped);
} else if (ts.isPropertyAccessExpression(unwrapped)) {
return this._hammerConfigTokenReferences.some(r => r.node === unwrapped.name);
}
return false;
}
function createContainerName(
callExpr: ts.CallExpression
):
| "createFragmentContainer"
| "createRefetchContainer"
| "createPaginationContainer" {
if (
ts.isIdentifier(callExpr.expression) &&
isCreateContainerFunction(callExpr.expression.text)
) {
return callExpr.expression.text;
}
if (
ts.isPropertyAccessExpression(callExpr.expression) &&
ts.isIdentifier(callExpr.expression.expression) &&
callExpr.expression.expression.text === "Relay"
) {
if (isCreateContainerFunction(callExpr.expression.name.text)) {
return callExpr.expression.name.text;
}
}
throw new Error("Not a relay create container call");
}
function findPostSwitchIdentifier(
statements: ReadonlyArray, name: string): ts.Identifier|null {
for (const stmt of statements) {
if (ts.isVariableStatement(stmt)) {
const decl = stmt.declarationList.declarations.find(
decl => ts.isIdentifier(decl.name) && decl.name.text === name);
if (decl !== undefined) {
return decl.name as ts.Identifier;
}
} else if (ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) {
if (stmt.name !== undefined && ts.isIdentifier(stmt.name) && stmt.name.text === name) {
return stmt.name;
}
}
}
return null;
}
function getQualifiedNameRoot(qualifiedName: ts.QualifiedName): ts.Identifier|null {
while (ts.isQualifiedName(qualifiedName.left)) {
qualifiedName = qualifiedName.left;
}
return ts.isIdentifier(qualifiedName.left) ? qualifiedName.left : null;
}
function findNamespaceOfIdentifier(id: ts.Identifier): ts.Identifier|null {
return id.parent && ts.isPropertyAccessExpression(id.parent) &&
ts.isIdentifier(id.parent.expression) ?
id.parent.expression :
null;
}
function getIdentifier(node) {
if (!node.name || !tsc.isIdentifier(node.name)) throw new Error('no Identifier found');
return node.name.text;
}
function extractObjectType(
sourceFile: ts.SourceFile,
declaration: ts.TypeLiteralNode | ts.InterfaceDeclaration
): ObjectType {
const properties: {
[key: string]: Type;
} = {};
for (const member of declaration.members) {
if (
!member.name ||
!ts.isIdentifier(member.name) ||
!ts.isPropertySignature(member) ||
!member.type
) {
throw panic(
`Expected a named and typed property, got this instead: ${member.getText(
sourceFile
)}`
);
}
let type = extractType(sourceFile, member.type);
if (member.questionToken) {
type = optionalType(type);
}
properties[member.name.getText(sourceFile)] = type;
}
return objectType(properties);
function f(n: ts.Node) {
seenAdd(kinds(n));
if (ts.isIdentifier(n)) {
pr(`${indent} ${loc(n)} ${strKind(n)} ${n.text} \n`);
}
else if (ts.isPropertySignature(n) || ts.isEnumMember(n)) {
pra(`${indent} ${loc(n)} ${strKind(n)} \n`);
}
else if (ts.isTypeLiteralNode(n)) {
let m = n.members;
pr(`${indent} ${loc(n)} ${strKind(n)} ${m.length} \n`);
}
else if (ts.isStringLiteral(n)) {
pr(`${indent} ${loc(n)} ${strKind(n)} ${n.text} \n`);
}
else { pr(`${indent} ${loc(n)} ${strKind(n)} \n`); }
indent += ' .';
ts.forEachChild(n, f);
indent = indent.slice(0, indent.length - 2);