Find Import File without Extension
Description
In ECMAScript modules (ESM), the module specifier must include the file extension, such as .js
or .mjs
, when importing local or absolute modules. This is because ESM does not perform any automatic file extension resolution, unlike CommonJS modules tools such as Webpack or Babel. This behavior matches how import behaves in browser environments, and is specified by the ESM module spec.
The rule finds all imports (static and dynamic) for files without a file extension.
YAML
yaml
id: find-import-file
language: js
rule:
regex: "/[^.]+[^/]$"
kind: string_fragment
any:
- inside:
stopBy: end
kind: import_statement
- inside:
stopBy: end
kind: call_expression
has:
field: function
regex: "^import$"
Example
ts
import a, {b, c, d} from "./file";
import e from "./other_file.js";
import "./folder/";
import {x} from "package";
import {y} from "package/with/path";
import("./dynamic1");
import("./dynamic2.js");
my_func("./unrelated_path_string")
Contributed by
DasSurma in this tweet.