Skip to content

Commit 9f4a364

Browse files
authored
fix: Ignore static imports in no-unreachable (#21276)
* fix: Ignore static imports in no-unreachable Fixes #21273 * test: add split import unreachable coverage --------- Co-authored-by: Taha Kotil <[email protected]>
1 parent 9cecb8a commit 9f4a364

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎lib/rules/no-unreachable.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ module.exports = {
210210
currentCodePathSegments.add(segment);
211211
},
212212

213-
// Registers for all statement nodes (excludes FunctionDeclaration).
213+
// Registers for all statement nodes (excludes FunctionDeclaration and ImportDeclaration).
214214
BlockStatement: reportIfUnreachable,
215215
BreakStatement: reportIfUnreachable,
216216
ClassDeclaration: reportIfUnreachable,
@@ -222,7 +222,6 @@ module.exports = {
222222
ForOfStatement: reportIfUnreachable,
223223
ForStatement: reportIfUnreachable,
224224
IfStatement: reportIfUnreachable,
225-
ImportDeclaration: reportIfUnreachable,
226225
LabeledStatement: reportIfUnreachable,
227226
ReturnStatement: reportIfUnreachable,
228227
SwitchStatement: reportIfUnreachable,

‎tests/lib/rules/no-unreachable.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ ruleTester.run("no-unreachable", rule, {
2828
"function foo() { function bar() { return 1; } return bar(); }",
2929
"function foo() { return bar(); function bar() { return 1; } }",
3030
"function foo() { return x; var x; }",
31+
{
32+
code: "throw new Error(); import foo from 'foo';",
33+
languageOptions: { ecmaVersion: 6, sourceType: "module" },
34+
},
3135
"function foo() { var x = 1; var y = 2; }",
3236
"function foo() { var x = 1; var y = 2; return; }",
3337
"while (true) { switch (foo) { case 1: x = 1; x = 2;} }",
@@ -93,6 +97,39 @@ ruleTester.run("no-unreachable", rule, {
9397
},
9498
],
9599
invalid: [
100+
{
101+
code: "throw new Error(); import foo from 'foo'; foo();",
102+
languageOptions: { ecmaVersion: 6, sourceType: "module" },
103+
errors: [
104+
{
105+
messageId: "unreachableCode",
106+
line: 1,
107+
column: 43,
108+
endLine: 1,
109+
endColumn: 49,
110+
},
111+
],
112+
},
113+
{
114+
code: "throw new Error(); foo(); import foo from 'foo'; foo();",
115+
languageOptions: { ecmaVersion: 6, sourceType: "module" },
116+
errors: [
117+
{
118+
messageId: "unreachableCode",
119+
line: 1,
120+
column: 20,
121+
endLine: 1,
122+
endColumn: 26,
123+
},
124+
{
125+
messageId: "unreachableCode",
126+
line: 1,
127+
column: 50,
128+
endLine: 1,
129+
endColumn: 56,
130+
},
131+
],
132+
},
96133
{
97134
code: "function foo() { return x; var x = 1; }",
98135
errors: [

0 commit comments

Comments
 (0)