isStringLiteral method
bool
isStringLiteral(
- Expression expression
)
Implementation
bool isStringLiteral(Expression expression) {
// Direct string literals
if (expression is SimpleStringLiteral) {
// Don't consider it a problematic string literal if it only contains special characters
final value = expression.stringValue ?? '';
if (isOnlySpecialCharacters(value) || value.trim().isEmpty) {
return false;
}
return true;
} else if (expression is StringInterpolation) {
// Check if the interpolation only contains special characters
String fullString = '';
for (var element in expression.elements) {
if (element is InterpolationString) {
fullString += element.value;
}
}
// If it's only special characters, don't consider it a problematic string
return !isOnlySpecialCharacters(fullString);
} else if (expression is MethodInvocation) {
// Check if it's a method call on a string or another method call
if (expression.target is StringLiteral ||
expression.target is MethodInvocation ||
(expression.target is SimpleIdentifier)) {
// Common string manipulation methods
final methodName = expression.methodName.name;
final stringMethods = [
'toLowerCase',
'toUpperCase',
'trim',
'substring',
'replaceAll',
'replaceFirst',
'split',
'join',
'padLeft',
'padRight',
'contains',
'startsWith',
'endsWith',
'concat',
];
if (stringMethods.contains(methodName)) {
// Only consider it a string literal if the target is a string literal itself
return expression.target is StringLiteral;
}
}
}
// Binary expressions for string concatenation (string + string)
else if ((expression is BinaryExpression) &&
expression.operator.type.toString() == 'PLUS') {
// Only consider it a string literal if one operand is a string literal
return isStringLiteral(expression.leftOperand) ||
isStringLiteral(expression.rightOperand);
}
return false;
}