Skip to content

Commit da5f6ec

Browse files
committed
[Fix] enforce-node-protocol-usage: avoid a crash with some TS code
1 parent 6e49a58 commit da5f6ec

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1919
- configs: added missing name attribute for eslint config inspector ([#3151], thanks [@NishargShah])
2020
- [`order`]: ensure arcane imports do not cause undefined behavior ([#3128], thanks [@Xunnamius])
2121
- [`order`]: resolve undefined property access issue when using `named` ordering ([#3166], thanks [@Xunnamius])
22+
- [`enforce-node-protocol-usage`]: avoid a crash with some TS code ([#3173], thanks [@ljharb])
2223

2324
### Changed
2425
- [Docs] [`extensions`], [`order`]: improve documentation ([#3106], thanks [@Xunnamius])
@@ -1175,6 +1176,7 @@ for info on changes for earlier releases.
11751176

11761177
[`memo-parser`]: ./memo-parser/README.md
11771178

1179+
[#3173]: https://github.com/import-js/eslint-plugin-import/pull/3173
11781180
[#3167]: https://github.com/import-js/eslint-plugin-import/pull/3167
11791181
[#3166]: https://github.com/import-js/eslint-plugin-import/pull/3166
11801182
[#3151]: https://github.com/import-js/eslint-plugin-import/pull/3151

src/rules/enforce-node-protocol-usage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function replaceStringLiteral(
2929
}
3030

3131
function isStringLiteral(node) {
32-
return node.type === 'Literal' && typeof node.value === 'string';
32+
return node && node.type === 'Literal' && typeof node.value === 'string';
3333
}
3434

3535
function isStaticRequireWith1Param(node) {
@@ -44,7 +44,7 @@ function isStaticRequireWith1Param(node) {
4444

4545
function checkAndReport(src, context) {
4646
// TODO use src.quasis[0].value.raw
47-
if (src.type === 'TemplateLiteral') { return; }
47+
if (!src || src.type === 'TemplateLiteral') { return; }
4848
const moduleName = 'value' in src ? src.value : src.name;
4949
if (typeof moduleName !== 'string') { console.log(src, moduleName); }
5050
const { settings } = context;

tests/src/rules/enforce-node-protocol-usage.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RuleTester } from '../rule-tester';
22
import flatMap from 'array.prototype.flatmap';
33
import { satisfies } from 'semver';
44

5-
import { test, testVersion } from '../utils';
5+
import { getTSParsers, test, testVersion } from '../utils';
66

77
const ruleTester = new RuleTester();
88
const rule = require('rules/enforce-node-protocol-usage');
@@ -343,3 +343,28 @@ ruleTester.run('enforce-node-protocol-usage', rule, {
343343
})),
344344
),
345345
});
346+
347+
context('TypeScript', function () {
348+
getTSParsers().forEach((parser) => {
349+
ruleTester.run('enforce-node-protocol-usage', rule, {
350+
valid: [
351+
test({
352+
code: `
353+
export class Thing {
354+
constructor(public readonly name: string) {
355+
// Do nothing.
356+
}
357+
358+
public sayHello(): void {
359+
console.log(\`Hello, \${this.name}!\`);
360+
}
361+
}
362+
`,
363+
parser,
364+
options: preferUsingProtocol,
365+
}),
366+
],
367+
invalid: [],
368+
});
369+
});
370+
});

0 commit comments

Comments
 (0)