This repository has been archived by the owner on Jul 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rogeliog/initial-approach
[Proposal] Initial approach
- Loading branch information
Showing
14 changed files
with
316 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function formatErrors(errors, formatters, errorType) { | ||
const format = (formatter) => formatter(errors, errorType) || []; | ||
const flatten = (accum, curr) => accum.concat(curr); | ||
|
||
return formatters.map(format).reduce(flatten, []) | ||
} | ||
|
||
module.exports = formatErrors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const chalk = require('chalk'); | ||
|
||
function displayError(index, severity, { file, message, origin }) { | ||
const baseError = chalk.red(`${index + 1}) ${severity}`); | ||
|
||
return [ | ||
`${baseError} ${file ? 'in ' + file : ''}`, | ||
'', | ||
message, | ||
(origin ? origin : undefined), | ||
'' | ||
].filter((chunk) => chunk !== undefined); | ||
} | ||
|
||
function isDefaultError(error) { | ||
return !error.type || error.type === 'babel-syntax-error'; | ||
} | ||
|
||
function format(errors, type) { | ||
return errors | ||
.filter(isDefaultError) | ||
.reduce((accum, error, i ) => ( | ||
accum.concat(displayError(i, type, error)) | ||
), []); | ||
} | ||
|
||
module.exports = format; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function dependenciesNotFound(count) { | ||
if (count === 1) { | ||
return 'This dependency was not found in node_modules:'; | ||
} | ||
|
||
return 'These dependencies were not found in node_modules:'; | ||
} | ||
|
||
function forgetToInstall(count) { | ||
if (count === 1) { | ||
return 'Did you forget to run npm install --save for it?'; | ||
} | ||
|
||
return 'Did you forget to run npm install --save for them?'; | ||
} | ||
|
||
function formatErrors(errors) { | ||
if (errors.length === 0) { | ||
return []; | ||
} | ||
|
||
return [ | ||
dependenciesNotFound(errors.length), | ||
'', | ||
...errors.map(({ module }) =>`* ${module}`), | ||
'', | ||
forgetToInstall(errors.length), | ||
]; | ||
} | ||
|
||
function format(errors) { | ||
return formatErrors(errors.filter((e) => ( | ||
e.type === 'module-not-found' | ||
))); | ||
} | ||
|
||
module.exports = format; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const extractError = require('./extractError'); | ||
|
||
function processErrors(errors, transformers) { | ||
const transform = (error, transformer) => transformer(error); | ||
const applyTransformations = (error) => transformers.reduce(transform, error); | ||
|
||
return errors.map(extractError).map(applyTransformations); | ||
} | ||
|
||
module.exports = processErrors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const TYPE = 'babel-syntax-error'; | ||
|
||
function cleanStackTrace(message) { | ||
return message | ||
.replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, ''); // at ... ...:x:y | ||
} | ||
|
||
function isBabelSyntaxError(e) { | ||
return e.type === TYPE || | ||
e.name === 'ModuleBuildError' && | ||
e.message.indexOf('SyntaxError') >= 0; | ||
} | ||
|
||
function transform(error) { | ||
if (isBabelSyntaxError(error)) { | ||
return Object.assign({}, error, { | ||
message: cleanStackTrace(error.message + '\n'), | ||
type: TYPE, | ||
severity: 1000, | ||
}); | ||
} | ||
|
||
return error; | ||
} | ||
|
||
module.exports = transform; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const TYPE = 'module-not-found'; | ||
|
||
function isModuleNotFoundError(e) { | ||
const { webpackError = {} } = e; | ||
return webpackError.dependencies && webpackError.dependencies.length && | ||
(e.type === TYPE || | ||
e.name === 'ModuleNotFoundError' && | ||
e.message.indexOf('Module not found') === 0); | ||
} | ||
|
||
function transform(error) { | ||
const webpackError = error.webpackError; | ||
if (isModuleNotFoundError(error)) { | ||
const module = webpackError.dependencies[0].request; | ||
return Object.assign({}, error, { | ||
message: `Module not found ${module}`, | ||
type: TYPE, | ||
severity: 900, | ||
module, | ||
}); | ||
} | ||
|
||
return error; | ||
} | ||
|
||
module.exports = transform; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const formatErrors = require('../../src/formatErrors'); | ||
const expect = require('expect'); | ||
const test = require('ava'); | ||
|
||
const simple = (errors) => errors | ||
.filter(({ type }) => !type).map(({ message }) => message); | ||
|
||
const allCaps = (errors) => errors | ||
.filter(({ type }) => type == 'other').map((e) => e.message.toUpperCase()); | ||
|
||
const notFound = (errors) => errors | ||
.filter(({ type }) => type === 'not-found').map(() => 'Not found'); | ||
|
||
const formatters = [allCaps] | ||
|
||
test('formats the error based on the matching formatters', () => { | ||
const errors = [ | ||
{ message: 'Error 1', type: undefined }, | ||
{ message: 'Error 2', type: 'other' }, | ||
{ message: 'Error 3', type: 'not-found' }, | ||
]; | ||
|
||
expect(formatErrors(errors, [simple, allCaps, notFound], 'Error')).toEqual([ | ||
'Error 1', | ||
'ERROR 2', | ||
'Not found', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const defaultError = require('../../../src/formatters/defaultError'); | ||
const expect = require('expect'); | ||
const test = require('ava'); | ||
const chalk = require('chalk'); | ||
|
||
const noColor = (arr) => arr.map(chalk.stripColor) | ||
const error = { message: 'Error message', file: './src/index.js' }; | ||
|
||
test('Formats errors with no type', () => { | ||
expect(noColor(defaultError([error], 'Warning'))).toEqual([ | ||
'1) Warning in ./src/index.js', | ||
'', | ||
'Error message', | ||
'', | ||
]); | ||
}); | ||
|
||
test('Formats babel-syntax-errors', () => { | ||
const babeError = { ...error, type: 'babel-syntax-error' }; | ||
expect(noColor(defaultError([babeError], 'Error'))).toEqual([ | ||
'1) Error in ./src/index.js', | ||
'', | ||
'Error message', | ||
'', | ||
]); | ||
}); | ||
|
||
test('Does not format other errors', () => { | ||
const otherError = { ...error, type: 'other-error' }; | ||
expect(noColor(defaultError([otherError], 'Error'))).toEqual([]); | ||
}); |
Oops, something went wrong.