TypeScript Code-Change Pack
URL slug: typescript-code-change
This page describes the TypeScript code-change pack for engineers reviewing TypeScript and JavaScript changes. The pack parses a unified git diff into analysis units and checks them against rules for risks a linter cannot express, such as a route with no visible auth check or a request value reaching a shell command or SQL query.
Files it reads
Section titled “Files it reads”file_globs is *.ts, *.tsx, *.js, *.jsx, *.mjs and *.cjs. Auto-routing sends files with these extensions to this pack by path glob.
The parser skips generated and vendored code, which produces no unit:
| Skipped | Why |
|---|---|
*.d.ts | Type declarations, often generated |
*.min.js | Minified bundles |
any dist/ directory | Build output |
any node_modules/ directory | Installed packages |
The pack does not override sniff(). Its ingest() method only understands unified-diff input, so a bare .ts file pasted into the Analyze form can match by content but produces no unit.
Parsing strategy
Section titled “Parsing strategy”parse_diff (opentremor_pack_typescript_code_change/parser.py) reads the diff hunk by hunk with core’s iter_hunks (opentremor_core.libs.diff).
- Mask each line.
lexer.pyreplaces strings, comments, template literals and regular expressions with spaces, so a}inside one of them is not counted. Block comments and template literals span lines, so their state carries from one line to the next. The old and new versions of the file are lexed separately. - Find block openers on the masked lines:
function,class,interface,typeandenumdeclarations; function-valued variables; exported constants; methods and class properties holding an arrow function; and calls such asrouter.post(...). - Find where each block ends by counting brackets.
{,(and[count together. A block ends on the first line where all brackets opened since its first line are closed and the statement does not continue (for example, a line ending in=>or=, or a next line starting with.,|or{). An added block is counted on added and context lines; a removed one is counted on removed and context lines. - Absorb decorators.
@Get(':id')and@UseGuards(AuthGuard)above a NestJS method belong to its unit. They provide evidence for the auth rule. - Merge a changed first line. A removed opener followed by an added opener of the same kind (
-export const TIMEOUT = 30;,+export const TIMEOUT = 5;) is one modified unit, named after the new code. - Collect the rest. Each changed line not claimed by a block becomes part of one unit per hunk. This includes imports, private constants and lines inside a function whose first line is above the hunk.
Why not a TypeScript parser?
Section titled “Why not a TypeScript parser?”A diff hunk is a fragment. It can start mid-function, omit the enclosing class and mix old and new lines. A full parser rejects much of this input. Bracket counting over the masked diff text works on these fragments.
The lexer is not exact. It does not read an apostrophe in JSX text (<p>Don't</p>) as the start of a string, because a JavaScript string cannot span lines. A / immediately after < is treated as a JSX closing tag, not a regular expression.
Block-to-identity mapping
Section titled “Block-to-identity mapping”| Block opens with | type | name |
|---|---|---|
export async function loadSettings(path) { | function | loadSettings |
export const getInvoice = async (req, res) => { | function | getInvoice |
export default function InvoicesPage() { in a .tsx file | component | InvoicesPage |
export const Banner = ({ html }) => { in a .tsx file | component | Banner |
export class Repository { | class | Repository |
async findOne(id: string) { inside a class | method | InvoicesController.findOne |
export const RETRY_POLICY = { | constant | RETRY_POLICY |
export type Role = | type | Role |
router.post("/invoices/:id/export", ...) | call | router.post("/invoices/:id/export") |
| changed imports and private constants | module | (module-level) |
When a changed class opener includes its methods, they are part of the class unit. A method gets its own unit when the hunk starts inside the class. Its name is then qualified by the class named in the git hunk heading (@@ -40,6 +40,6 @@ export class MemberService {). The same heading names the unit for changed lines inside a function whose first line is above the hunk.
Metadata
Section titled “Metadata”metadata.source_path— the changed file’s path, from the diff header.metadata.group_path— the file’s directory (app/api/invoices/[id]). In a JavaScript repository this can identify a package or, in Next.js, a route.
Rule variants
Section titled “Rule variants”| Variant | File | For |
|---|---|---|
node (default) | rules/node-typescript-code-change-rules.md | Express, Fastify, Koa, hapi, NestJS and plain http |
nextjs | rules/nextjs-typescript-code-change-rules.md | Route handlers, server actions, middleware, pages/api and NEXT_PUBLIC_ variables |
Both files use the same rule IDs, worded for their framework. suggest_rule_type counts imports of next against imports of Express, Fastify, Koa, hapi, NestJS and Hono in the diff, then suggests the variant with more imports. A tie or no framework imports produces no suggestion. Removed imports do not count. The suggestion is stored on the repository for a person to apply; it does not switch the rules on its own.
Rule IDs use TSCC-NNN for security, TSCC-PERF-NNN for performance and TSCC-REL-NNN for reliability. Performance and reliability rules are LOW or MEDIUM, so they do not fail the merge check on their own.
Some risks depend on code outside the unit. Three rules are review prompts: they ask a person to check something the unit cannot show. They have LOW confidence and MEDIUM severity at most, so they do not fail the merge check on their own. A LOW-confidence finding starts in needs_review.
| Rule | Severity / confidence | What it catches |
|---|---|---|
TSCC-001 | MEDIUM / LOW | Review prompt: a route handler or server action with no visible auth check |
TSCC-002 | HIGH / MEDIUM | An object read or written by a request ID with no ownership or tenant check |
TSCC-003 | HIGH / MEDIUM | SQL or ORM raw queries built from request values without parameters |
TSCC-004 | CRITICAL / HIGH | Request values reaching exec, execSync, spawn with shell: true, eval or new Function |
TSCC-005 | HIGH / MEDIUM | User content in dangerouslySetInnerHTML or innerHTML with no sanitizer |
TSCC-006 | HIGH / MEDIUM | fetch or axios to a URL built from input with no allowlist |
TSCC-007 | HIGH / MEDIUM | fs calls on a path built from input with no containment check |
TSCC-008 | HIGH / HIGH | A secret literal in code, or a server secret read through NEXT_PUBLIC_ |
TSCC-009 | MEDIUM / LOW | Review prompt: request data deep-merged into an object (prototype pollution) |
TSCC-010 | HIGH / HIGH | jwt.decode used to authorize, or a verify that accepts the none algorithm |
TSCC-011 | HIGH / MEDIUM | CORS that reflects any origin with credentials |
TSCC-012 | MEDIUM / LOW | Review prompt: a session or auth cookie without httpOnly, secure or sameSite |
TSCC-013 | MEDIUM / MEDIUM | An open redirect to a URL from a query parameter |
TSCC-014 | MEDIUM / MEDIUM | Math.random() for tokens, or a fast hash for passwords |
TSCC-015 | MEDIUM / MEDIUM | A regular expression open to ReDoS on unbounded input |
TSCC-PERF-001 to -008 | LOW–MEDIUM | Calls in a loop, unbounded Promise.all, retries with no backoff, synchronous I/O in a handler, list queries with no limit, sequential independent awaits, large bodies read whole, whole-library imports in browser code |
TSCC-REL-001 to -004 | LOW–MEDIUM | Outbound calls with no timeout, floating promises in a handler, catch blocks that swallow errors, process.exit in a handler or library |
Like every pack’s built-in ruleset, these rules are seeded into an organization’s custom_rules as editable rows. An organization can reword, disable or replace them without changing this package. Core keeps rows an organization has not edited in sync with these files (OpenTremor-core#0034).
The pack ships a demo sample in demo/: Express invoice routes that read an invoice without an ownership check and build a shell command from the request body, plus a session secret moved into the code.