UNPKG

eslint-config-ao

Version:

AO ESLint config

79 lines (67 loc) 2.26 kB
// Use commonjs rather than es6 imports so it can be used from other modules. // const { CLIEngine } = require('eslint'); const eslintConsts = require('../rules/eslint-consts'); const path = require('path'); const fs = require('fs'); const OFF = eslintConsts.LEVEL_LOOKUP[eslintConsts.OFF]; const WARN = eslintConsts.LEVEL_LOOKUP[eslintConsts.WARN]; const ERROR = eslintConsts.LEVEL_LOOKUP[eslintConsts.ERROR]; /** * Creates a linter * * @see http://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles * @see http://eslint.org/docs/developer-guide/nodejs-api.html#executeontext * @param {Function} CLIEngine CLIEngine to help eslint find plugins relative to package this is run from. * @param {*} eslintrc Base eslint to use * @param {*} disableRules a list of specify rules to disable */ function createLinter(CLIEngine, eslintrc, disableRules = []){ const config = { useEslintrc: false, baseConfig: eslintrc, rules: disableRules.reduce(function(acc, rule) { acc[rule] = OFF; return acc; }, {}) }; const cli = new CLIEngine(config); return function(text){ const linter = cli.executeOnText(text); return linter.results[0]; }; } function testCounts(expected, count, results, severity) { if (typeof expected == 'number') { expect(count).toBe(expected); } else if (typeof expected == 'object') { var errorCounts = results.messages.filter(m => m.severity === severity).reduce((acc, { ruleId }) => { if (acc.hasOwnProperty(ruleId)) { acc[ruleId] += 1; } else { acc[ruleId] = 1; } return acc; }, {}); expect(errorCounts).toEqual(expected); } } function testCase(CLIEngine, eslintrc, rootPath = __dirname) { return function(t){ const text = fs.readFileSync(path.resolve(rootPath, t.file)).toString(); const lint = createLinter(CLIEngine, eslintrc, t.disableRules); const results = lint(text); try { testCounts(t.warnings, results.warningCount, results, WARN); testCounts(t.errors, results.errorCount, results, ERROR); } catch (e) { // eslint-disable-next-line no-console // console.log(results); throw e; } }; } module.exports = { createLinter, testCounts, testCase };