Skip to content

Commit 40da1fb

Browse files
authored
chore: add no-external-code rule to Deno Style Guide lint plugin (#6562)
1 parent 9b4e122 commit 40da1fb

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

_tools/lint_plugin.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ export default {
5656
};
5757
},
5858
},
59+
// https://docs.deno.com/runtime/contributing/style_guide/#do-not-depend-on-external-code.
60+
"no-external-code": {
61+
create(context) {
62+
if (context.filename.includes("_tools")) {
63+
// Tools are allowed to use external code
64+
return {};
65+
}
66+
return {
67+
ImportDeclaration(node) {
68+
const resolvedSpecifier = import.meta.resolve(node.source.value);
69+
if (
70+
resolvedSpecifier.startsWith("file:") ||
71+
resolvedSpecifier.startsWith("jsr:@std") ||
72+
resolvedSpecifier.startsWith("jsr:/@std") ||
73+
resolvedSpecifier.startsWith("node:")
74+
) {
75+
return;
76+
}
77+
context.report({
78+
node,
79+
range: node.source.range,
80+
message: "External imports are not allowed outside of tools",
81+
hint:
82+
'Use code from within `@std` instead of external code. E.g. Use `import { foo } from "@std/foo"` instead of `import { foo } from "https://deno.land/x/foo/mod.ts"`.',
83+
});
84+
},
85+
};
86+
},
87+
},
5988
// https://docs.deno.com/runtime/contributing/style_guide/#naming-convention/
6089
"naming-convention": {
6190
create(context) {

_tools/lint_plugin_test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ class MyClass {
5656
);
5757
});
5858

59+
Deno.test("deno-style-guide/no-external-code", {
60+
ignore: !Deno.version.deno.startsWith("2"),
61+
}, () => {
62+
// Good
63+
assertLintPluginDiagnostics('import { walk } from "@std/fs/walk";', []);
64+
65+
// Bad
66+
assertLintPluginDiagnostics(
67+
`
68+
import { bad } from "https://deno.land/malicious-muffin/bad.ts";
69+
import { bad } from "jsr:@malicious-muffin/bad";
70+
`,
71+
[{
72+
id: "deno-style-guide/no-external-code",
73+
fix: [],
74+
range: [1, 65],
75+
message: "External imports are not allowed outside of tools",
76+
hint:
77+
'Use code from within `@std` instead of external code. E.g. Use `import { foo } from "@std/foo"` instead of `import { foo } from "https://deno.land/x/foo/mod.ts"`.',
78+
}, {
79+
id: "deno-style-guide/no-external-code",
80+
fix: [],
81+
range: [66, 114],
82+
message: "External imports are not allowed outside of tools",
83+
hint:
84+
'Use code from within `@std` instead of external code. E.g. Use `import { foo } from "@std/foo"` instead of `import { foo } from "https://deno.land/x/foo/mod.ts"`.',
85+
}],
86+
);
87+
});
88+
5989
Deno.test("deno-style-guide/naming-convention", {
6090
ignore: !Deno.version.deno.startsWith("2"),
6191
}, () => {

cli/_tools/compare_with_rust.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { unicodeWidth } from "../unicode_width.ts";
55
import { fromFileUrl } from "../../path/mod.ts";
6-
import fc from "https://esm.sh/[email protected]";
6+
import fc from "npm:[email protected]";
77

88
// Note: This test is optional. It requires the Rust code to be compiled locally
99
Deno.test("fast-check equality with unicode_width Rust crate", async (t) => {

0 commit comments

Comments
 (0)