Skip to content
DashboardWebsite

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.

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:

SkippedWhy
*.d.tsType declarations, often generated
*.min.jsMinified bundles
any dist/ directoryBuild output
any node_modules/ directoryInstalled 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.

parse_diff (opentremor_pack_typescript_code_change/parser.py) reads the diff hunk by hunk with core’s iter_hunks (opentremor_core.libs.diff).

  1. Mask each line. lexer.py replaces 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.
  2. Find block openers on the masked lines: function, class, interface, type and enum declarations; function-valued variables; exported constants; methods and class properties holding an arrow function; and calls such as router.post(...).
  3. 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.
  4. Absorb decorators. @Get(':id') and @UseGuards(AuthGuard) above a NestJS method belong to its unit. They provide evidence for the auth rule.
  5. 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.
  6. 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.

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 opens withtypename
export async function loadSettings(path) {functionloadSettings
export const getInvoice = async (req, res) => {functiongetInvoice
export default function InvoicesPage() { in a .tsx filecomponentInvoicesPage
export const Banner = ({ html }) => { in a .tsx filecomponentBanner
export class Repository {classRepository
async findOne(id: string) { inside a classmethodInvoicesController.findOne
export const RETRY_POLICY = {constantRETRY_POLICY
export type Role =typeRole
router.post("/invoices/:id/export", ...)callrouter.post("/invoices/:id/export")
changed imports and private constantsmodule(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.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.
VariantFileFor
node (default)rules/node-typescript-code-change-rules.mdExpress, Fastify, Koa, hapi, NestJS and plain http
nextjsrules/nextjs-typescript-code-change-rules.mdRoute 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.

RuleSeverity / confidenceWhat it catches
TSCC-001MEDIUM / LOWReview prompt: a route handler or server action with no visible auth check
TSCC-002HIGH / MEDIUMAn object read or written by a request ID with no ownership or tenant check
TSCC-003HIGH / MEDIUMSQL or ORM raw queries built from request values without parameters
TSCC-004CRITICAL / HIGHRequest values reaching exec, execSync, spawn with shell: true, eval or new Function
TSCC-005HIGH / MEDIUMUser content in dangerouslySetInnerHTML or innerHTML with no sanitizer
TSCC-006HIGH / MEDIUMfetch or axios to a URL built from input with no allowlist
TSCC-007HIGH / MEDIUMfs calls on a path built from input with no containment check
TSCC-008HIGH / HIGHA secret literal in code, or a server secret read through NEXT_PUBLIC_
TSCC-009MEDIUM / LOWReview prompt: request data deep-merged into an object (prototype pollution)
TSCC-010HIGH / HIGHjwt.decode used to authorize, or a verify that accepts the none algorithm
TSCC-011HIGH / MEDIUMCORS that reflects any origin with credentials
TSCC-012MEDIUM / LOWReview prompt: a session or auth cookie without httpOnly, secure or sameSite
TSCC-013MEDIUM / MEDIUMAn open redirect to a URL from a query parameter
TSCC-014MEDIUM / MEDIUMMath.random() for tokens, or a fast hash for passwords
TSCC-015MEDIUM / MEDIUMA regular expression open to ReDoS on unbounded input
TSCC-PERF-001 to -008LOW–MEDIUMCalls 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 -004LOW–MEDIUMOutbound 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.