Skip to content

Instantly share code, notes, and snippets.

@jeremy-code
Created February 11, 2025 01:38
Show Gist options
  • Save jeremy-code/6ba37e66495e1e8a881024997184cd9f to your computer and use it in GitHub Desktop.
Save jeremy-code/6ba37e66495e1e8a881024997184cd9f to your computer and use it in GitHub Desktop.

Some brief explorations of getStaticValue() from @eslint-community/eslint-utils (or in this case, the typed version from @typescript-eslint/utils". Intending to update this later with better examples (and code) once I get more time.

import { analyze } from "@typescript-eslint/scope-manager";
import { parse } from "@typescript-eslint/typescript-estree";
import { ASTUtils, AST_NODE_TYPES } from "@typescript-eslint/utils";
import dedent from "dedent";
const getAstAndScopeManager = (code: string) => {
const ast = parse(code, {
range: true,
});
return [ast, analyze(ast, { sourceType: "script" })] as const;
};
[
`"string";`, // { value: 'string'}
"1;", // { value: 1 }
"null;", // { value: null }
"true;", // { value: true }
"undefined;", // null
].forEach((code) => {
const [ast, scopeManager] = getAstAndScopeManager(code);
const node = ast.body[0];
if (ASTUtils.isNodeOfType(AST_NODE_TYPES.ExpressionStatement)(node)) {
const staticValue = ASTUtils.getStaticValue(
node.expression,
scopeManager.scopes[0]
);
console.log(staticValue);
}
});
[
// Same as above
`const a = "string";`,
`const a = 1;`,
`const a = null;`,
`const a = true;`,
`const a = undefined;`,
`const obj = { a: "string", b: 1, c: null, d: true, e: undefined };`, // null
`const obj = { a: "string", b: 1, c: null, d: true };`, // { value: { a: 'string', b: 1, c: null, d: true } }
].forEach((code) => {
const [ast, scopeManager] = getAstAndScopeManager(code);
const variableDeclaration = ast.body[0];
if (
ASTUtils.isNodeOfType(AST_NODE_TYPES.VariableDeclaration)(
variableDeclaration
)
) {
const init = variableDeclaration.declarations[0].init;
if (init !== null) {
const staticValue = ASTUtils.getStaticValue(init, scopeManager.scopes[0]);
console.log(code, staticValue);
}
}
});
[
dedent`
const a = 1;
const b = a;
b;
`, // { value: 1 }
dedent`
const a = 1;
const b = a + 1;
b;
`, // { value: 2 }
].forEach((code) => {
const [ast, scopeManager] = getAstAndScopeManager(code);
const node = ast.body[2];
if (ASTUtils.isNodeOfType(AST_NODE_TYPES.ExpressionStatement)(node)) {
const staticValue = ASTUtils.getStaticValue(
node.expression,
scopeManager.scopes[0]
);
console.log(code, staticValue);
}
});
[
dedent`
const objectA = { a: "string", b: 1, c: null, d: true };
const objectB = { ...objectA, e: false };
`, // { value: { a: 'string', b: 1, c: null, d: true, e: false } }
].forEach((code) => {
const [ast, scopeManager] = getAstAndScopeManager(code);
const variableDeclaration = ast.body[1];
if (
ASTUtils.isNodeOfType(AST_NODE_TYPES.VariableDeclaration)(
variableDeclaration
)
) {
const init = variableDeclaration.declarations[0].init;
console.log(init);
if (init !== null) {
const staticValue = ASTUtils.getStaticValue(init, scopeManager.scopes[0]);
console.log(code, staticValue);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment