Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] importType: avoid crashing on a non-string #2305

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions CHANGELOG.md
Expand Up @@ -6,23 +6,26 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Fixed
- `importType`: avoid crashing on a non-string' ([#2305], thanks [@ljharb])

## [2.25.3] - 2021-11-09

### Fixed
- [`extensions`]: ignore unresolveable type-only imports ([#2270], [#2271], [@jablko])
- `importType`: fix `isExternalModule` calculation ([#2282], [@mx-bernhard])
- [`no-import-module-exports`]: avoid false positives with a shadowed `module` or `exports` ([#2297], [@ljharb])
- [`extensions`]: ignore unresolveable type-only imports ([#2270], [#2271], thanks [@jablko])
- `importType`: fix `isExternalModule` calculation ([#2282], thanks [@mx-bernhard])
- [`no-import-module-exports`]: avoid false positives with a shadowed `module` or `exports` ([#2297], thanks [@ljharb])

### Changed
- [Docs] [`order`]: add type to the default groups ([#2272], [@charpeni])
- [readme] Add note to TypeScript docs to install appropriate resolver ([#2279], [@johnthagen])
- [Refactor] `importType`: combine redundant `isScoped` and `isScopedModule` ([@ljharb])
- [Docs] HTTP => HTTPS ([#2287], [@Schweinepriester])
- [Docs] [`order`]: add type to the default groups ([#2272], thanks [@charpeni])
- [readme] Add note to TypeScript docs to install appropriate resolver ([#2279], thanks [@johnthagen])
- [Refactor] `importType`: combine redundant `isScoped` and `isScopedModule` (thanks [@ljharb])
- [Docs] HTTP => HTTPS ([#2287], thanks [@Schweinepriester])

## [2.25.2] - 2021-10-12

### Fixed
- [Deps] update `eslint-module-utils` for real this time ([#2255])
- [Deps] update `eslint-module-utils` for real this time ([#2255], thanks [@ljharb])

## [2.25.1] - 2021-10-11

Expand Down Expand Up @@ -942,6 +945,7 @@ for info on changes for earlier releases.

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

[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
[#2282]: https://github.com/import-js/eslint-plugin-import/pull/2282
Expand Down
2 changes: 1 addition & 1 deletion src/core/importType.js
Expand Up @@ -14,7 +14,7 @@ function baseModule(name) {
}

export function isAbsolute(name) {
return nodeIsAbsolute(name);
return typeof name === 'string' && nodeIsAbsolute(name);
}

// path is defined only when a resolver resolves to a non-standard path
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-absolute-path.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {

create(context) {
function reportIfAbsolute(source) {
if (typeof source.value === 'string' && isAbsolute(source.value)) {
if (isAbsolute(source.value)) {
context.report(source, 'Do not import modules using an absolute path');
}
}
Expand Down
13 changes: 12 additions & 1 deletion tests/src/core/importType.js
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as path from 'path';

import importType, { isExternalModule, isScoped } from 'core/importType';
import importType, { isExternalModule, isScoped, isAbsolute } from 'core/importType';

import { testContext, testFilePath } from '../utils';

Expand Down Expand Up @@ -256,3 +256,14 @@ describe('importType(name)', function () {
expect(isScoped('@a/abc/def')).to.equal(true);
});
});

describe('isAbsolute', () => {
it('does not throw on a non-string', () => {
expect(() => isAbsolute()).not.to.throw();
expect(() => isAbsolute(null)).not.to.throw();
expect(() => isAbsolute(true)).not.to.throw();
expect(() => isAbsolute(false)).not.to.throw();
expect(() => isAbsolute(0)).not.to.throw();
expect(() => isAbsolute(NaN)).not.to.throw();
});
});