-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lint transformed files, init lint tests
- Loading branch information
Showing
4 changed files
with
195 additions
and
24 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
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,65 @@ | ||
const { ESLint } = require('eslint'); | ||
const eslintrc = require('../../../.eslintrc.js'); | ||
|
||
// This is a singleton | ||
let eslintInstance; | ||
|
||
// Only initialize the ESLint instance if necessary | ||
// This also makes it easier to test this module | ||
const initializeESLint = () => { | ||
if (!eslintInstance) { | ||
eslintInstance = new ESLint({ baseConfig: eslintrc, useEslintrc: false }); | ||
} | ||
}; | ||
|
||
// Four spaces | ||
const TAB = ' '; | ||
|
||
module.exports = { | ||
lintTransformedFile, | ||
}; | ||
|
||
/** | ||
* Lints a transformed file by invoking ESLint programmatically on the string | ||
* file contents. The path to the file must be specified so that the repository | ||
* ESLint config can be applied properly. | ||
* | ||
* An error is thrown if linting produced any errors, or if the file is ignored | ||
* by ESLint. Files linted by this function should never be ignored. | ||
* | ||
* @param {string} content - The file content. | ||
* @param {string} filePath - The path to the file. | ||
* @returns {Promise<void>} Returns `undefined` or throws an error if linting produced | ||
* any errors, or if the linted file is ignored. | ||
*/ | ||
async function lintTransformedFile(content, filePath) { | ||
initializeESLint(); | ||
|
||
const lintResult = ( | ||
await eslintInstance.lintText(content, { filePath, warnIgnored: false }) | ||
)[0]; | ||
|
||
// This indicates that the file is ignored, which should never be the case for | ||
// a transformed file. | ||
if (lintResult === undefined) { | ||
throw new Error( | ||
`MetaMask build: Transformed file "${filePath}" appears to be ignored by ESLint.`, | ||
); | ||
} | ||
|
||
// This is the success case | ||
if (lintResult.errorCount === 0) { | ||
return; | ||
} | ||
|
||
// Errors are stored in the messages array, and their "severity" is 2 | ||
const errorsString = lintResult.messages | ||
.filter(({ severity }) => severity === 2) | ||
.reduce((allErrors, { message, ruleId }) => { | ||
return allErrors.concat(`${TAB}${ruleId}\n${TAB}${message}\n\n`); | ||
}, ''); | ||
|
||
throw new Error( | ||
`MetaMask build: Lint errors found in transformed file "${filePath}":\n\n${errorsString}`, | ||
); | ||
} |
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,38 @@ | ||
const { lintTransformedFile } = require('./utils'); | ||
|
||
let mockESLint; | ||
|
||
jest.mock('eslint', () => ({ | ||
ESLint: class MockESLint { | ||
constructor() { | ||
if (mockESLint) { | ||
throw new Error('Mock ESLint ref already assigned!'); | ||
} | ||
|
||
// eslint-disable-next-line consistent-this | ||
mockESLint = this; | ||
|
||
// eslint-disable-next-line jest/prefer-spy-on | ||
this.lintText = jest.fn(); | ||
} | ||
}, | ||
})); | ||
|
||
describe('transform utils', () => { | ||
describe('lintTransformedFile', () => { | ||
it('initializes the ESLint singleton', async () => { | ||
expect(mockESLint).not.toBeDefined(); | ||
|
||
// This error is an artifact of how we're mocking the ESLint singleton, | ||
// and won't actually occur in production. | ||
await expect(() => lintTransformedFile()).rejects.toThrow( | ||
`Cannot read property '0' of undefined`, | ||
); | ||
expect(mockESLint).toBeDefined(); | ||
}); | ||
|
||
it.todo('returns if linting passes with no errors'); | ||
it.todo('throws if the file is ignored by ESLint'); | ||
it.todo('throws if linting produced any errors'); | ||
}); | ||
}); |