This commit is contained in:
+19
-12
@@ -70,14 +70,18 @@ const noDisableCommentsRule: ProjectRule = {
|
||||
/** A module specifier read as a path: relative, absolute, or a `#` alias. */
|
||||
const PATH_SPECIFIER_PATTERN: RegExp = /^[./#]/u;
|
||||
|
||||
/** Script module extensions an import may name: our `.ts` files and `.js` package subpaths. */
|
||||
const SCRIPT_EXTENSIONS: readonly string[] = [".ts", ".js"];
|
||||
/**
|
||||
* Script module extensions an import may name: our `.ts` files and `.js`,
|
||||
* `.mts`, or `.mjs` package subpaths. `.cts`, `.cjs`, `.tsx`, and `.jsx` are
|
||||
* not among them.
|
||||
*/
|
||||
const SCRIPT_EXTENSIONS: ReadonlySet<string> = new Set([".ts", ".js", ".mts", ".mjs"]);
|
||||
|
||||
/**
|
||||
* Whether `source` names an asset rather than a script module: a query string
|
||||
* such as `?raw`, or a final path segment with an extension other than `.ts`
|
||||
* or `.js`. Bare package names are left alone, dots and all, as are
|
||||
* extensionless paths.
|
||||
* such as `?raw`, or a final path segment whose extension, from its last dot,
|
||||
* is not `.ts`, `.js`, `.mts`, or `.mjs`. Bare package names are left alone,
|
||||
* dots and all, as are extensionless paths.
|
||||
*/
|
||||
function isAssetSource(source: string): boolean {
|
||||
if (source.includes("?")) {
|
||||
@@ -94,25 +98,28 @@ function isAssetSource(source: string): boolean {
|
||||
if (lastSegment === "." || lastSegment === ".." || !lastSegment.includes(".")) {
|
||||
return false;
|
||||
}
|
||||
return !SCRIPT_EXTENSIONS.some((extension: string): boolean => lastSegment.endsWith(extension));
|
||||
const extension: string = lastSegment.slice(lastSegment.lastIndexOf("."));
|
||||
return !SCRIPT_EXTENSIONS.has(extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* `no-asset-imports` — rejects importing anything but a script module: a
|
||||
* source path, static or a dynamic `import()` with a string literal, ending in
|
||||
* an extension other than `.ts` or `.js` (`.css`, `.json`, `.svg`, `.tsx`) or
|
||||
* carrying a query string (`?raw`, `?url`), and any import or export with
|
||||
* import attributes. `.js` covers package subpaths such as
|
||||
* `lit/decorators.js`. Bare package names, `#` aliases, and `node:` builtins
|
||||
* an extension other than `.ts`, `.js`, `.mts`, or `.mjs` (`.css`, `.json`,
|
||||
* `.svg`, `.jsx`, `.tsx`, `.cjs`) or carrying a query string (`?raw`, `?url`),
|
||||
* and any import or export with import attributes. `.js`, `.mts`, and `.mjs`
|
||||
* cover package subpaths such as `lit/decorators.js` or
|
||||
* `pdfjs-dist/legacy/build/pdf.mjs`; our own files are still `.ts` only. Bare
|
||||
* package names, `#` aliases, and `node:` builtins
|
||||
* without an extension pass; package subpaths are checked like paths.
|
||||
*/
|
||||
const noAssetImportsRule: ProjectRule = {
|
||||
meta: {
|
||||
type: "problem",
|
||||
docs: { description: "Disallow importing assets instead of .ts or .js modules" },
|
||||
docs: { description: "Disallow importing assets instead of .ts, .js, .mts, or .mjs modules" },
|
||||
schema: [],
|
||||
messages: {
|
||||
extension: "Only .ts and .js modules may be imported.",
|
||||
extension: "Only .ts, .js, .mts and .mjs modules may be imported.",
|
||||
attributes: "Import attributes are not allowed.",
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user