📄 getStaticStringValue¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 3 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/util/getStaticStringValue.ts
📦 Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
isNullLiteral |
./isNullLiteral |
Functions¶
getStaticStringValue(node: TSESTree.Node): string | null¶
Returns the result of the string conversion applied to the evaluated value of the given expression node, if it can be determined statically.
This function returns a string value for all Literal nodes and simple TemplateLiteral nodes only.
In all other cases, this function returns null.
Parameters:
nodeany: Expression node.
Returns: undefined
String value if it can be determined. Otherwise, null.
Raw JSDoc
/**
* Returns the result of the string conversion applied to the evaluated value of the given expression node,
* if it can be determined statically.
*
* This function returns a `string` value for all `Literal` nodes and simple `TemplateLiteral` nodes only.
* In all other cases, this function returns `null`.
* @param node Expression node.
* @returns String value if it can be determined. Otherwise, `null`.
*/
Calls:
isNullLiteral (from ./isNullLiteral)String
Internal Comments:
// eslint-disable-next-line eqeqeq, @typescript-eslint/internal/eqeq-nullish -- intentional strict comparison for literal value
Code
export function getStaticStringValue(node: TSESTree.Node): string | null {
switch (node.type) {
case AST_NODE_TYPES.Literal:
// eslint-disable-next-line eqeqeq, @typescript-eslint/internal/eqeq-nullish -- intentional strict comparison for literal value
if (node.value === null) {
if (isNullLiteral(node)) {
return String(node.value); // "null"
}
if ('regex' in node) {
return `/${node.regex.pattern}/${node.regex.flags}`;
}
if ('bigint' in node) {
return node.bigint;
}
// Otherwise, this is an unknown literal. The function will return null.
} else {
return String(node.value);
}
break;
case AST_NODE_TYPES.TemplateLiteral:
if (node.expressions.length === 0 && node.quasis.length === 1) {
return node.quasis[0].value.cooked;
}
break;
// no default
}
return null;
}
Generated by Syntax Scribe