Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseRegExp(node) {
const evaluated = eslint_utils_1.getStaticValue(node, globalScope);
if (evaluated == null || !(evaluated.value instanceof RegExp)) {
return null;
}
const { pattern, flags } = regexpp_1.parseRegExpLiteral(evaluated.value);
if (pattern.alternatives.length !== 1 ||
flags.ignoreCase ||
flags.global) {
return null;
}
// Check if it can determine a unique string.
const chars = pattern.alternatives[0].elements;
if (!chars.every(c => c.type === 'Character')) {
return null;
}
// To string.
return String.fromCodePoint(...chars.map(c => c.value));
function parseRegExp(node: TSESTree.Node): string | null {
const evaluated = getStaticValue(node, globalScope);
if (evaluated == null || !(evaluated.value instanceof RegExp)) {
return null;
}
const { pattern, flags } = parseRegExpLiteral(evaluated.value);
if (
pattern.alternatives.length !== 1 ||
flags.ignoreCase ||
flags.global
) {
return null;
}
// Check if it can determine a unique string.
const chars = pattern.alternatives[0].elements;
if (!chars.every(c => c.type === 'Character')) {
function parseRegExp(node) {
const evaluated = eslint_utils_1.getStaticValue(node, globalScope);
if (evaluated == null || !(evaluated.value instanceof RegExp)) {
return null;
}
const { source, flags } = evaluated.value;
const isStartsWith = source.startsWith('^');
const isEndsWith = source.endsWith('$');
if (isStartsWith === isEndsWith ||
flags.includes('i') ||
flags.includes('m')) {
return null;
}
const text = parseRegExpText(source, flags.includes('u'));
if (text == null) {
return null;
}
return { isEndsWith, isStartsWith, text };
function parseRegExp(
node: TSESTree.Node,
): { isStartsWith: boolean; isEndsWith: boolean; text: string } | null {
const evaluated = getStaticValue(node, globalScope);
if (evaluated == null || !(evaluated.value instanceof RegExp)) {
return null;
}
const { source, flags } = evaluated.value;
const isStartsWith = source.startsWith('^');
const isEndsWith = source.endsWith('$');
if (
isStartsWith === isEndsWith ||
flags.includes('i') ||
flags.includes('m')
) {
return null;
}
const text = parseRegExpText(source, flags.includes('u'));
function isLengthExpression(node, expectedObjectNode) {
if (node.type === 'MemberExpression') {
return (eslint_utils_1.getPropertyName(node, globalScope) === 'length' &&
isSameTokens(node.object, expectedObjectNode));
}
const evaluatedLength = eslint_utils_1.getStaticValue(node, globalScope);
const evaluatedString = eslint_utils_1.getStaticValue(expectedObjectNode, globalScope);
return (evaluatedLength != null &&
evaluatedString != null &&
typeof evaluatedLength.value === 'number' &&
typeof evaluatedString.value === 'string' &&
evaluatedLength.value === evaluatedString.value.length);
}
/**
function isLengthExpression(
node: TSESTree.Node,
expectedObjectNode: TSESTree.Node,
): boolean {
if (node.type === 'MemberExpression') {
return (
getPropertyName(node, globalScope) === 'length' &&
isSameTokens(node.object, expectedObjectNode)
);
}
const evaluatedLength = getStaticValue(node, globalScope);
const evaluatedString = getStaticValue(expectedObjectNode, globalScope);
return (
evaluatedLength != null &&
evaluatedString != null &&
typeof evaluatedLength.value === 'number' &&
typeof evaluatedString.value === 'string' &&
evaluatedLength.value === evaluatedString.value.length
);
}
function isNumber(node: TSESTree.Node, value: number): boolean {
const evaluated = getStaticValue(node, globalScope);
return evaluated !== null && evaluated.value === value;
}
function isNumber(
node: TSESTree.Node,
value: number,
): node is TSESTree.Literal {
const evaluated = getStaticValue(node, globalScope);
return evaluated != null && evaluated.value === value;
}