mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-22 06:17:20 +00:00

There are a lot of new rules since the last time I revised the ESLint rules documentation. I've updated the main `.eslintrc.yml` to include some newer additions, plus I added another ESLint configuration file inside `tests`, so we can add some stricted rules to the production code while relaxing them for the test suite (due to the fact that Mocha is not very ES6 friendly and Angular tests require a bit of dark magic to setup). This is a summary of the most important changes: - Disallow "magic numbers" These should now be extracted to constants, which forces us to think of a good name for them, and thus make the code more self-documenting (I had to Google up the meaning of some existing magic numbers, so I guess this will be great for readability purposes). - Require consistent `return` statements Some functions relied on JavaScript relaxed casting mechanism to work, which now have explicit return values. This flag also helped me detect some promises that were not being returned, and therefore risked not being caught by the exception handlers in case of errors. - Disallow redefining function arguments Immutability makes functions easier to reason about. - Enforce JavaScript string templates instead of string concatenation We were heavily mixing boths across the codebase. There are some extra rules that I tweaked, however most of codebase changes in this commit are related to the rules mentioned above. Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
/**
|
|
* This script setups and runs linting modules on our HTML files.
|
|
*
|
|
* See https://github.com/nikestep/html-angular-validate
|
|
*
|
|
* Usage:
|
|
*
|
|
* node scripts/html-lint.js
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const chalk = require('chalk');
|
|
const path = require('path');
|
|
const _ = require('lodash');
|
|
const angularValidate = require('html-angular-validate');
|
|
const EXIT_CODES = require('../lib/shared/exit-codes');
|
|
const PROJECT_ROOT = path.join(__dirname, '..');
|
|
const FILENAME = path.relative(PROJECT_ROOT, __filename);
|
|
|
|
console.log('Scanning...');
|
|
|
|
angularValidate.validate(
|
|
[
|
|
path.join(PROJECT_ROOT, 'lib', 'gui', '**/*.html')
|
|
],
|
|
{
|
|
customattrs: [
|
|
|
|
// Internal
|
|
'os-open-external',
|
|
'os-dropzone',
|
|
'manifest-bind',
|
|
|
|
// External
|
|
'hide-if-state',
|
|
'show-if-state',
|
|
'uib-tooltip'
|
|
|
|
],
|
|
angular: true,
|
|
tmplext: 'tpl.html',
|
|
doctype: 'HTML5',
|
|
charset: 'utf-8',
|
|
reportpath: null,
|
|
reportCheckstylePath: null
|
|
}
|
|
).then((result) => {
|
|
_.each(result.failed, (failure) => {
|
|
|
|
// The module has a typo in the "numbers" property
|
|
console.error(chalk.red(`${failure.numerrs} errors at ${path.relative(PROJECT_ROOT, failure.filepath)}`));
|
|
|
|
_.each(failure.errors, (error) => {
|
|
const errorPosition = `[${error.line}:${error.col}]`;
|
|
console.error(` ${chalk.yellow(errorPosition)} ${error.msg}`);
|
|
|
|
if (/^Attribute (.*) not allowed on/.test(error.msg)) {
|
|
console.error(chalk.dim(` If this is a valid directive attribute, add it to the whitelist at ${FILENAME}`));
|
|
}
|
|
});
|
|
|
|
console.error('');
|
|
});
|
|
|
|
if (result.filessucceeded === result.fileschecked) {
|
|
console.log(chalk.green('Passed'));
|
|
} else {
|
|
console.error(chalk.red(`Total: ${result.filessucceeded}/${result.fileschecked}`));
|
|
}
|
|
|
|
if (!result.allpassed) {
|
|
const EXIT_TIMEOUT_MS = 500;
|
|
|
|
// Add a small timeout, otherwise the scripts exits
|
|
// before every string was printed on the screen.
|
|
setTimeout(() => {
|
|
process.exit(EXIT_CODES.GENERAL_ERROR);
|
|
}, EXIT_TIMEOUT_MS);
|
|
|
|
}
|
|
|
|
}, (error) => {
|
|
console.error(error);
|
|
process.exit(EXIT_CODES.GENERAL_ERROR);
|
|
});
|