Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Commit b450dee

Browse files
christophehurpeaugeowarin
authored andcommitted
fix: relative module with ../ (#34)
* fix: relative module with ../ * feat: formatter module not found separate dependencies and relative modules
1 parent adb639f commit b450dee

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

src/formatters/moduleNotFound.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
'use strict';
22
const concat = require('../utils').concat;
33

4-
function moduleNotFound (count) {
5-
if (count === 1) {
6-
return 'This module was not found:';
7-
}
4+
function isRelative (module) {
5+
return module.startsWith('./') || module.startsWith('../');
6+
}
87

9-
return 'These modules were not found:';
8+
function formatFileList (files) {
9+
const length = files.length;
10+
if (!length) return '';
11+
return ` in ${files[0]}${files[1] ? `, ${files[1]}` : ''}${length > 2 ? ` and ${length - 2} other${length === 3 ? '' : 's'}` : ''}`;
1012
}
1113

14+
function formatGroup (group) {
15+
const files = group.errors.map(e => e.file).filter(Boolean);
16+
return `* ${group.module}${formatFileList(files)}`;
17+
}
18+
19+
1220
function forgetToInstall (missingDependencies) {
1321
const moduleNames = missingDependencies.map(missingDependency => missingDependency.module);
1422

@@ -19,8 +27,26 @@ function forgetToInstall (missingDependencies) {
1927
return `To install them, you can run: npm install --save ${moduleNames.join(' ')}`;
2028
}
2129

22-
function isRelative (module) {
23-
return module.startsWith('./');
30+
function dependenciesNotFound (dependencies) {
31+
if (dependencies.length === 0) return;
32+
33+
return concat(
34+
dependencies.length === 1 ? 'This dependency was not found:' : 'These dependencies were not found:',
35+
'',
36+
dependencies.map(formatGroup),
37+
'',
38+
forgetToInstall(dependencies),
39+
);
40+
}
41+
42+
function relativeModulesNotFound (modules) {
43+
if (modules.length === 0) return;
44+
45+
return concat(
46+
modules.length === 1 ? 'This relative module was not found:' : 'These relative modules were not found:',
47+
'',
48+
modules.map(formatGroup),
49+
);
2450
}
2551

2652
function groupModules (errors) {
@@ -40,33 +66,20 @@ function groupModules (errors) {
4066
}));
4167
}
4268

43-
function formatFileList (files) {
44-
const length = files.length;
45-
if (!length) return '';
46-
return ` in ${files[0]}${files[1] ? `, ${files[1]}` : ''}${length > 2 ? ` and ${length - 2} other${length === 3 ? '' : 's'}` : ''}`;
47-
}
48-
49-
function formatGroup (group) {
50-
const files = group.errors.map(e => e.file).filter(Boolean);
51-
return `* ${group.module}${formatFileList(files)}`;
52-
}
53-
5469
function formatErrors (errors) {
5570
if (errors.length === 0) {
5671
return [];
5772
}
5873

5974
const groups = groupModules(errors);
60-
const missingDependencies = groups.filter(group => !group.relative);
75+
76+
const dependencies = groups.filter(group => !group.relative);
77+
const relativeModules = groups.filter(group => group.relative);
6178

6279
return concat(
63-
moduleNotFound(errors.length),
64-
'',
65-
groups.map(formatGroup),
66-
missingDependencies.length === 0 ? undefined : [
67-
'',
68-
forgetToInstall(missingDependencies)
69-
]
80+
dependenciesNotFound(dependencies),
81+
dependencies.length && relativeModules.length ? ['', ''] : null,
82+
relativeModulesNotFound(relativeModules),
7083
);
7184
}
7285

test/fixtures/module-errors/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require('not-found');
2-
require('./non-existing');
2+
require('./non-existing');
3+
require('../non-existing');

test/integration.spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ it('integration : module-errors', async() => {
4545
const logs = await executeAndGetLogs('./fixtures/module-errors/webpack.config.js');
4646

4747
expect(logs).toEqual([
48-
'ERROR Failed to compile with 2 errors',
48+
'ERROR Failed to compile with 3 errors',
4949
'',
50-
'These modules were not found:',
50+
'This dependency was not found:',
5151
'',
52-
'* ./non-existing in ./test/fixtures/module-errors/index.js',
5352
'* not-found in ./test/fixtures/module-errors/index.js',
5453
'',
55-
'To install it, you can run: npm install --save not-found'
54+
'To install it, you can run: npm install --save not-found',
55+
'',
56+
'',
57+
'These relative modules were not found:',
58+
'',
59+
'* ./non-existing in ./test/fixtures/module-errors/index.js',
60+
'* ../non-existing in ./test/fixtures/module-errors/index.js',
5661
]);
5762
});
5863

@@ -123,11 +128,15 @@ it('integration : webpack multi compiler : module-errors', async() => {
123128
expect(logs).toEqual([
124129
'ERROR Failed to compile with 2 errors',
125130
'',
126-
'These modules were not found:',
131+
'This dependency was not found:',
127132
'',
128-
'* ./non-existing in ./test/fixtures/multi-compiler-module-errors/index.js',
129133
'* not-found in ./test/fixtures/multi-compiler-module-errors/index2.js',
130134
'',
131-
'To install it, you can run: npm install --save not-found'
135+
'To install it, you can run: npm install --save not-found',
136+
'',
137+
'',
138+
'This relative module was not found:',
139+
'',
140+
'* ./non-existing in ./test/fixtures/multi-compiler-module-errors/index.js',
132141
]);
133142
});

test/unit/formatters/moduleNotFound.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const expect = require('expect');
44
it('Formats module-not-found errors', () => {
55
const error = { type: 'module-not-found', module: 'redux' };
66
expect(moduleNotFound([error])).toEqual([
7-
'This module was not found:',
7+
'This dependency was not found:',
88
'',
99
'* redux',
1010
'',
@@ -16,7 +16,7 @@ it('Groups all module-not-found into one', () => {
1616
const reduxError = { type: 'module-not-found', module: 'redux' };
1717
const reactError = { type: 'module-not-found', module: 'react' };
1818
expect(moduleNotFound([reduxError, reactError])).toEqual([
19-
'These modules were not found:',
19+
'These dependencies were not found:',
2020
'',
2121
'* redux',
2222
'* react',
@@ -28,12 +28,12 @@ it('Groups all module-not-found into one', () => {
2828
it('Groups same module in module-not-found with 2 files', () => {
2929
const reduxError = { type: 'module-not-found', module: 'redux' };
3030
const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
31-
const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
31+
const reactError2 = { type: 'module-not-found', module: 'react', file: '../src/file2.js' };
3232
expect(moduleNotFound([reduxError, reactError1, reactError2])).toEqual([
33-
'These modules were not found:',
33+
'These dependencies were not found:',
3434
'',
3535
'* redux',
36-
'* react in ./src/file1.js, ./src/file2.js',
36+
'* react in ./src/file1.js, ../src/file2.js',
3737
'',
3838
'To install them, you can run: npm install --save redux react'
3939
]);
@@ -45,7 +45,7 @@ it('Groups same module in module-not-found with 3 files', () => {
4545
const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
4646
const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
4747
expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3])).toEqual([
48-
'These modules were not found:',
48+
'These dependencies were not found:',
4949
'',
5050
'* redux',
5151
'* react in ./src/file1.js, ./src/file2.js and 1 other',
@@ -61,7 +61,7 @@ it('Groups same module in module-not-found with 4 files', () => {
6161
const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
6262
const reactError4 = { type: 'module-not-found', module: 'react', file: './src/file4.js' };
6363
expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3, reactError4])).toEqual([
64-
'These modules were not found:',
64+
'These dependencies were not found:',
6565
'',
6666
'* redux',
6767
'* react in ./src/file1.js, ./src/file2.js and 2 others',

0 commit comments

Comments
 (0)