-
Notifications
You must be signed in to change notification settings - Fork 2
feat(compiler): SYN011 — warn on dynamic import() calls that bypass the module capability model (?bs 0.7+) #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
664fc68
feat(compiler): SYN011 — warn on dynamic import() calls that bypass t…
d62efbc
fix(syn011): address Copilot review — shorten message and improve tes…
3b596f2
fix(compiler): address SYN011 Copilot review feedback — method shorth…
707d24a
fix(syn011): fn keyword token kind — use kind==='keyword' for fn decl…
5cd476a
fix(syn011): handle import?.() optional-call form and add regression …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /** | ||
| * Tests for SYN011: dynamic import() call detection (?bs 0.7+). | ||
| * | ||
| * SYN011 is a non-blocking warning — transform must not throw. | ||
| */ | ||
|
|
||
| import { describe, it, expect } from "vitest"; | ||
| import { transform } from "../src/index.js"; | ||
|
|
||
| function compile(src: string) { | ||
| return transform(src, {}); | ||
| } | ||
|
marcelofarias marked this conversation as resolved.
|
||
|
|
||
| describe("SYN011: dynamic import() call detection", () => { | ||
| it("fires on bare import(specifier) inside a fn body", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn loadMod(path: string) -> any {\n" + | ||
| " return import(path)\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(true); | ||
| }); | ||
|
|
||
| it("fires on template literal import(`./module`)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn loadMod(name: string) -> any {\n" + | ||
| " return import(`./modules/${name}`)\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(true); | ||
| }); | ||
|
|
||
| it("fires on awaited const mod = await import(path)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "async fn loadPlugin(path: string) -> any {\n" + | ||
| " const mod = await import(path)\n" + | ||
| " return mod\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(true); | ||
| }); | ||
|
|
||
| it("does NOT fire inside an unsafe block", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "async fn loadPlugin(path: string) -> any {\n" + | ||
| ' const mod = await unsafe "loads plugin dynamically" { import(path) }\n' + | ||
| " return mod\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("does NOT fire inside an unsafe fn body", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| 'unsafe "dynamic loader" async fn loadPlugin(path: string) -> any {\n' + | ||
| " const mod = await import(path)\n" + | ||
| " return mod\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("does NOT fire on import.meta.url (import.meta property access)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn getUrl() -> string {\n" + | ||
| " return import.meta.url\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("does NOT fire below ?bs 0.7", () => { | ||
| const src = | ||
| "?bs 0.6\n" + | ||
| "fn loadMod(path: string) -> any {\n" + | ||
| " return import(path)\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("severity is warning (non-blocking)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn loadMod(path: string) -> any {\n" + | ||
| " return import(path)\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| const w = result.warnings.find((w) => w.code === "SYN011"); | ||
| expect(w?.severity).toBe("warning"); | ||
| }); | ||
|
|
||
| it("fires once per distinct import() call in the same fn", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "async fn loadBoth(a: string, b: string) -> any {\n" + | ||
| " const m1 = await import(a)\n" + | ||
| " const m2 = await import(b)\n" + | ||
| " return { m1, m2 }\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.filter((w) => w.code === "SYN011").length).toBe(2); | ||
| }); | ||
|
|
||
| it("does NOT fire on bare import reference (not called)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn getImport() -> any {\n" + | ||
| " return import\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("does NOT fire on object method shorthands named import", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn test() -> void {\n" + | ||
| " const handler = { import(x) { return x; } };\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("does NOT fire on a botscript fn declaration named import", () => { | ||
| // `fn` is a keyword token (kind==='keyword'), not an ident — the exclusion | ||
| // must check kind==='keyword' to avoid false positives on fn declarations. | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "fn import(specifier: string) -> any {\n" + | ||
| " return specifier\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(false); | ||
| }); | ||
|
|
||
| it("fires on optional-call form import?.(specifier)", () => { | ||
| const src = | ||
| "?bs 0.7\n" + | ||
| "async fn loadMod(path: string) -> any {\n" + | ||
| " return await import?.(path)\n" + | ||
| "}\n"; | ||
| const result = compile(src); | ||
| expect(result.warnings.some((w) => w.code === "SYN011")).toBe(true); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.