Skip to content
Myr

Read a dynamic import's template specifier and a division after a postfix ++ in the gate's import scan, so neither hides an import from ReachedInternals

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Review of 402 against its issue (Spec) and AGENTS.md with spec §7 (Standards). - imports.ts: `import(`x`)` and `require(`x`)` with a template that has no `${}` name a module as fixed as a string, but the scan read only quotes, so a backtick was a one-character way past ReachedInternals. The two call forms now take a backtick too; a template with a substitution keeps its `${` in the masked text and still matches nothing (stage 3's to resolve). - imports.ts: after a postfix `++` or `--` a `/` divides; the lexer read it as a regular expression to the end of the line, hiding any import after it there (`POSTFIX`). - Tests (red by assertion first): imports.test.ts rows for template specifiers in import() and require(), none for a template with a substitution, and imports after `n++ / 2` and `m-- / 2`; shape.test.ts row for a dynamic import of another module's internal file by template. - Test text: the reasoned-escape shape row now says escapeDecisions counts it (no longer the ratchet stage); the `text` helper's doc no longer promises specifiers. - Left as recorded by the builder (402 Comments #1-#5): the one-line edit in packages/git/test/gateway.test.ts outside the territory, `PushFacts.escapeCounts` beyond the Interface, the two escape paths for 403/417, and the "waits" wording. - ASSUMPTION: a code line that opens with `*` still reads as a doc comment's inside, so an import after a multiplication continuation on that line is missed; an accepted cost of reading added lines without their context. - bun run fix && bun run verify green: gate 185 tests; platform entry 1,517,431 of 1,665,000.

authored by oscabriel · committed by oscabriel · parent e64c648 · Browse files · 3 files+47 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+47 added−9 removed

  • tests:+34 added−2 removed
  • other:+13 added−7 removed

3 files+47 added−9 removed

modified:packages/gate/src/imports.ts+13 added-7 removed
20 unmodified lines
2122// After one of these, a `/` opens a regular expression, not a division.23const OPENS_REGEX = new Set("(,=:[!&|?{};+-*%<>~^");2425// A postfix `++` or `--` ends an operand, so the `/` after it divides.26const POSTFIX = /(?:\+\+|--)$/u;2728const KEYWORD_BEFORE_REGEX =29 /(?:^|[^\w$])(?:return|typeof|case|do|else|in|of|new|delete|void|throw|yield|await)$/u;3031/**
124 unmodified lines
156 const before = tail.trimEnd();157158 return (159 before === "" ||157 OPENS_REGEX.has(before.slice(-1)) ||160 (OPENS_REGEX.has(before.slice(-1)) && !POSTFIX.test(before)) ||161 KEYWORD_BEFORE_REGEX.test(before)162 );163 };164
78 unmodified lines
243};244245// Each form of import, read in the masked text: `… from "x"` (an import or246// re-export, or the last line of one), `import "x"`, `import("x")` and244// `require("x")`. A string's contents read as `_`.247// `require("x")`. A string's contents read as `_`. A call may also name its248// module by a template with no `${}`, which is as fixed as a string; one with249// a substitution keeps its `${`, so it never matches.250const FORMS = [251 /(?<![\w$.])from\s*(?<quote>["'])(?<contents>_+)\k<quote>/gu,252 /(?<![\w$.])import\s*(?<quote>["'])(?<contents>_+)\k<quote>/gu,248 /(?<![\w$.])import\s*\(\s*(?<quote>["'])(?<contents>_+)\k<quote>/gu,249 /(?<![\w$.])require\s*\(\s*(?<quote>["'])(?<contents>_+)\k<quote>/gu,253 /(?<![\w$.])import\s*\(\s*(?<quote>["'`])(?<contents>_+)\k<quote>/gu,254 /(?<![\w$.])require\s*\(\s*(?<quote>["'`])(?<contents>_+)\k<quote>/gu,255];256257/**258 * Every module specifier `text` imports: static imports and re-exports259 * (`… from "x"`, over any number of lines), side-effect imports, dynamic255 * `import("x")` and `require("x")`, each on the line its specifier sits on,256 * in the order they appear. Comments, strings, templates and regular257 * expressions import nothing.260 * `import("x")` and `require("x")` (their specifier a string or a template261 * with no substitution), each on the line its specifier sits on, in the262 * order they appear. Comments, strings, templates and regular expressions263 * import nothing.264 */265export const importSpecifiers = (text: string): readonly ImportSpecifier[] => {266 const masked = maskOf(text);267
119 unmodified lines
modified:packages/gate/test/imports.test.ts+28 added-1 removed
3 unmodified lines
4import { Option, Schema } from "effect";56import { importSpecifiers, reachedInternals } from "../src/imports.ts";78/** The text of `lines`, one per line, and each specifier it should give. */8/** The text of `lines`, one per line. */9const text = (...lines: readonly string[]) => lines.join("\n");1011describe(importSpecifiers, () => {12 it.each([
59 unmodified lines
72 "import config = require('./config.cjs');"73 ),74 },75 {76 found: [77 { line: 1, specifier: "../b/src/internal.ts" },78 { line: 2, specifier: "../b/src/cache.ts" },79 ],80 name: "a dynamic import and a require whose specifier is a template with no substitution",81 text: text(82 "const lazy = await import(`../b/src/internal.ts`);",83 "const cache = require(`../b/src/cache.ts`);"84 ),85 },86 {87 found: [],88 name: "nothing for a template specifier with a substitution, which only stage 3 resolves",89 text: `const locale = await import(\`./locales/\${lang}.ts\`);`,90 },91 {92 found: [93 { line: 1, specifier: "./lazy.ts" },94 { line: 2, specifier: "./later.ts" },95 ],96 name: "an import after a division by a postfix increment or decrement",97 text: text(98 'const half = n++ / 2, lazy = import("./lazy.ts");',99 'const rest = m-- / 2, later = import("./later.ts");'100 ),101 },102 {103 found: [{ line: 1, specifier: "../b/src/internal.ts" }],104 name: "the last line of an import whose first line is not in the text",105 text: ' trim } from "../b/src/internal.ts";',106 },
131 unmodified lines
modified:packages/gate/test/shape.test.ts+6 added-1 removed
218 unmodified lines
219 facts: push(220 [week],221 [[week, 7, "// oxlint-disable-next-line no-x -- reason given"]]222 ),223 name: "an added escape with a reason (counted by the ratchet stage)",223 name: "an added escape with a reason (counted by escapeDecisions)",224 refusals: [],225 },226 {227 facts: push([weekTest], [[weekTest, 2, 'vi.mock("../src/week.ts");']], {
147 unmodified lines
375 name: "a dynamic import and a require of another module's internal file",376 refusals: [reachedFromWeek(2, pad), reachedFromWeek(6, pad)],377 },378 {379 facts: push([week], [[week, 4, `const pad = await import(\`${pad}\`);`]]),380 name: "a dynamic import of another module's internal file, its specifier a template",381 refusals: [reachedFromWeek(4, pad)],382 },383 {384 facts: push(385 [week],386 [387 [
74 unmodified lines