Skip to content

Commit

Permalink
Fixes false positives with import/extensions in ESM. (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotny authored Mar 21, 2022
1 parent a6175bd commit bb49e89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ const config = {
};

if (packageJson.type === 'module') {
// Need to modify rule as file extensions are mandatory in ESM.
// Need to modify rules as file extensions are mandatory in ESM.
// https://nodejs.org/api/esm.html#mandatory-file-extensions
config.rules['import/extensions'] = [
'error',
'ignorePackages',
{
cjs: 'never',
},
];
config.rules['import/no-useless-path-segments'] = [
'error',
{
Expand Down
34 changes: 33 additions & 1 deletion src/import.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
describe('import/extensions', () => {
beforeEach(() => {
jest.resetModules();
});

test('cjs', () => {
jest.doMock('./utils/files/contents', () => ({
packageJson: {},
}));

const {rules} = require('./import');

const [, option, config] = rules['import/extensions'];

expect(option).toBe('never');
expect(config.mjs).toBe('ignorePackages');
});

test('esm', () => {
jest.doMock('./utils/files/contents', () => ({
packageJson: {type: 'module'},
}));

const {rules} = require('./import');

const [, option, config] = rules['import/extensions'];

expect(option).toBe('ignorePackages');
expect(config.cjs).toBe('never');
});
});

describe('import/no-useless-path-segments', () => {
beforeEach(() => {
jest.resetModules();
});

test('packageJson.type = "module"', () => {
test('esm', () => {
jest.doMock('./utils/files/contents', () => ({
packageJson: {type: 'module'},
}));
Expand Down

0 comments on commit bb49e89

Please sign in to comment.