Skip to content

Commit 29d49f4

Browse files
committed
Import restricting rules now catch specific names if they are immediately destructured
1 parent 47b2a0d commit 29d49f4

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/imports/defineImportsRule/check.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,21 @@ export function makeCheckingFunction({
141141
? node.specifiers.filter((specifier) =>
142142
names.includes(getSpecifierName(specifier.exported)),
143143
)
144-
: [];
145-
if (specifiers.length === 0) {
146-
throw Error(
147-
"You have provided a forbiddenName or forbiddenNames, but I did not pass you any of these as actually imported.",
148-
);
149-
}
150-
specifiers.forEach((specifier) => {
151-
context.report({
152-
loc: specifier.loc,
153-
message,
144+
: "can-not-find-specifiers";
145+
if (specifiers !== "can-not-find-specifiers") {
146+
if (specifiers.length === 0) {
147+
throw Error(
148+
"You have provided a forbiddenName or forbiddenNames, but I did not pass you any of these as actually imported.",
149+
);
150+
}
151+
specifiers.forEach((specifier) => {
152+
context.report({
153+
loc: specifier.loc,
154+
message,
155+
});
154156
});
155-
});
156-
return;
157+
return;
158+
}
157159
}
158160
context.report({
159161
fix: fix

src/imports/defineImportsRule/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,25 @@ export function defineImportsRule(f: (details: ImportDetails) => CheckResult) {
8383
ImportExpression(node) {
8484
if (node.source.type !== "Literal") return;
8585
if (typeof node.source.value !== "string") return;
86+
87+
const names: string[] = (function () {
88+
const { parent } = node;
89+
if (parent.type !== "AwaitExpression") return [];
90+
const grandParent = parent.parent;
91+
if (grandParent.type !== "VariableDeclarator") return [];
92+
const declarator = grandParent;
93+
if (declarator.id.type !== "ObjectPattern") return [];
94+
const { properties } = declarator.id;
95+
return properties
96+
.map((property) => {
97+
if (property.type !== "Property") return undefined;
98+
if (property.key.type !== "Identifier") return undefined;
99+
return property.key.name;
100+
})
101+
.filter(Boolean);
102+
})();
86103
check({
87-
names: [],
104+
names,
88105
node: {
89106
...node,
90107
source: { ...node.source, value: node.source.value },

src/rules/no-forward-ref.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ invalid(
2121
"import { type Ref, forwardRef, useState } from 'react'",
2222
);
2323

24+
invalid(
25+
rule,
26+
"can not import forwardRef using dynamic import",
27+
"const { forwardRef } = await import('react')",
28+
);
29+
2430
invalid(
2531
rule,
2632
"can not import ForwardedRef",

0 commit comments

Comments
 (0)