Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TSModuleDeclaration(node): void {
// Do nothing if the name is a string.
if (!node.id || node.id.type === AST_NODE_TYPES.Literal) {
return;
}
// Get tokens of the declaration header.
const moduleType = sourceCode.getTokenBefore(node.id);
if (
moduleType &&
moduleType.type === AST_TOKEN_TYPES.Identifier &&
moduleType.value === 'module'
) {
context.report({
node,
messageId: 'useNamespace',
fix(fixer) {
return fixer.replaceText(moduleType, 'namespace');
},
}
},
});
const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet = new Set([
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.OptionalCallExpression,
AST_NODE_TYPES.OptionalMemberExpression,
AST_NODE_TYPES.ThisExpression,
]);
const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet = new Set([
AST_NODE_TYPES.BigIntLiteral,
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.Literal,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.OptionalMemberExpression,
AST_NODE_TYPES.TemplateLiteral,
]);
const ALLOWED_NON_COMPUTED_PROP_TYPES: ReadonlySet = new Set([
AST_NODE_TYPES.Identifier,
]);
function isValidChainTarget(
node: TSESTree.Node,
allowIdentifier: boolean,
): node is ValidChainTarget {
if (
node.type === AST_NODE_TYPES.MemberExpression ||
node.type === AST_NODE_TYPES.OptionalMemberExpression
) {
function isIgnoredMethod(
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
ignoredMethods: Set,
): boolean {
if (
node.key.type === AST_NODE_TYPES.Literal &&
typeof node.key.value === 'string'
) {
return ignoredMethods.has(node.key.value);
}
if (
node.key.type === AST_NODE_TYPES.TemplateLiteral &&
node.key.expressions.length === 0
) {
return ignoredMethods.has(node.key.quasis[0].value.raw);
}
if (!node.computed && node.key.type === AST_NODE_TYPES.Identifier) {
return ignoredMethods.has(node.key.name);
}
return false;
}
MemberExpression(node) {
if ('name' in node.object && node.object.name === 'jasmine') {
const { parent, property } = node;
if (parent && parent.type === AST_NODE_TYPES.AssignmentExpression) {
if (
'name' in property &&
property.name === 'DEFAULT_TIMEOUT_INTERVAL'
) {
const { right } = parent;
if (right.type === AST_NODE_TYPES.Literal) {
context.report({
fix: fixer => [
fixer.replaceText(
parent,
`jest.setTimeout(${right.value})`,
),
],
node,
messageId: 'illegalJasmine',
});
return;
}
}
context.report({ node, messageId: 'illegalJasmine' });
}
return (
isFunctionCall(init, 'String') ||
isLiteral(init, 'string') ||
init.type === AST_NODE_TYPES.TemplateLiteral
);
case AST_NODE_TYPES.TSSymbolKeyword:
return isFunctionCall(init, 'Symbol');
case AST_NODE_TYPES.TSTypeReference: {
if (
annotation.typeName.type === AST_NODE_TYPES.Identifier &&
annotation.typeName.name === 'RegExp'
) {
const isRegExpLiteral =
init.type === AST_NODE_TYPES.Literal &&
init.value instanceof RegExp;
const isRegExpNewCall =
init.type === AST_NODE_TYPES.NewExpression &&
init.callee.type === AST_NODE_TYPES.Identifier &&
init.callee.name === 'RegExp';
const isRegExpCall = isFunctionCall(init, 'RegExp');
return isRegExpLiteral || isRegExpCall || isRegExpNewCall;
}
return false;
}
case AST_NODE_TYPES.TSUndefinedKeyword:
return (
hasUnaryPrefix(init, 'void') || isIdentifier(init, 'undefined')
const isStringLiteral = (
node: TSESTree.Node,
value?: V,
): node is StringLiteral =>
node.type === AST_NODE_TYPES.Literal &&
typeof node.value === 'string' &&
(value === undefined || node.value === value);
function isLiteral(init: TSESTree.Expression, typeName: string): boolean {
return (
init.type === AST_NODE_TYPES.Literal && typeof init.value === typeName
);
}
function isIdentifier(
- foo !== undefined
- foo != undefined
*/
if (
node.type === AST_NODE_TYPES.BinaryExpression &&
['!==', '!='].includes(node.operator) &&
isValidChainTarget(node.left, allowIdentifier)
) {
if (
node.right.type === AST_NODE_TYPES.Identifier &&
node.right.name === 'undefined'
) {
return true;
}
if (
node.right.type === AST_NODE_TYPES.Literal &&
node.right.value === null
) {
return true;
}
}
return false;
}
const hasStringAsFirstArgument = (
node: TSESTree.CallExpression,
): node is FirstArgumentStringCallExpression =>
node.arguments &&
node.arguments[0] &&
(node.arguments[0].type === AST_NODE_TYPES.Literal ||
node.arguments[0].type === AST_NODE_TYPES.TemplateLiteral);
) {
return false;
}
const expectAssertionName = getAccessorValue(expression.callee.property);
if (expectAssertionName !== 'assertions') {
return expectAssertionName === 'hasAssertions';
}
const [arg] = expression.arguments;
return (
expression.arguments &&
expression.arguments.length === 1 &&
arg.type === AST_NODE_TYPES.Literal &&
typeof arg.value === 'number' &&
Number.isInteger(arg.value)
);
};