mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-30 08:09:15 +01:00
1e494f3004
We would like to insert TypeScript into the ESLint configuration, and because of the way that the current config is organized, that is not easy to do. Most files are assumed to be files that are suited for running in a browser context. This isn't correct, as we should expect most files to work in a Node context instead. This is because all browser-based files will be run through a transpiler that is able to make use of Node-specific variables anyway. There are a couple of important ways we can categories files which our ESLint config should be capable of handling well: * Is the file a script or a module? In other words, does the file run procedurally or is the file intended to be brought into an existing file? * If the file is a module, does it use the CommonJS syntax (`require()`) or does it use the ES syntax (`import`/`export`)? When we introduce TypeScript, this set of questions will become: * Is the file a script or a module? * If the file is a module, is it a JavaScript module or a TypeScript module? * If the file is a JavaScript module, does it use the CommonJS syntax (`require()`) or does it use the ES syntax (`import`/`export`)? To represent these divisions, this commit removes global rules — so now all of the rules are kept in `overrides` for explicitness — and sets up rules for CommonJS- and ES-module-compatible files that intentionally do not overlap with each other. This way TypeScript (which has its own set of rules independent from JavaScript and therefore shouldn't overlap with the other rules either) can be easily added later. Finally, this commit splits up the ESLint config into separate files and adds documentation to each section. This way sets of rules which are connected to a particular plugin (`jsdoc`, `@babel`, etc.) can be easily understood instead of being obscured.
94 lines
2.7 KiB
JavaScript
94 lines
2.7 KiB
JavaScript
const { ESLint } = require('eslint');
|
|
const eslintrc = require('../../../.eslintrc.js');
|
|
|
|
eslintrc.overrides.forEach((override) => {
|
|
const rules = override.rules ?? {};
|
|
|
|
// We don't want linting to fail for purely stylistic reasons.
|
|
rules['prettier/prettier'] = 'off';
|
|
// Sometimes we use `let` instead of `const` to assign variables depending on
|
|
// the build type.
|
|
rules['prefer-const'] = 'off';
|
|
|
|
override.rules = rules;
|
|
});
|
|
|
|
// Remove all test-related overrides. We will never lint test files here.
|
|
eslintrc.overrides = eslintrc.overrides.filter((override) => {
|
|
return !(
|
|
(override.extends &&
|
|
override.extends.find(
|
|
(configName) =>
|
|
configName.includes('jest') || configName.includes('mocha'),
|
|
)) ||
|
|
(override.plugins &&
|
|
override.plugins.find((pluginName) => pluginName.includes('jest')))
|
|
);
|
|
});
|
|
|
|
/**
|
|
* The singleton ESLint instance.
|
|
*
|
|
* @type {ESLint}
|
|
*/
|
|
let eslintInstance;
|
|
|
|
// We only need a single ESLint instance, and we only initialize it if necessary
|
|
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 encountered for transformed file "${filePath}":\n\n${errorsString}`,
|
|
);
|
|
}
|