How to use eslint - 10 common examples

To help you get started, we’ve selected a few eslint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sindresorhus / eslint-plugin-unicorn / test / lint / lint.js View on Github external
// Make sure rules are loaded from codebase
const loadedRules = cli.getRules();
if (!ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`))) {
	console.error('`eslint-plugin-unicorn` rules are not loaded from codebase.');
	process.exit(1);
}

const report = cli.executeOnFiles(files);

const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report;

const hasFixable = fixableErrorCount || fixableWarningCount;

if (fix && hasFixable) {
	CLIEngine.outputFixes(report);
}

if (errorCount || warningCount) {
	const formatter = cli.getFormatter();
	console.log(formatter(report.results));

	console.log();
	console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint \` to check again.`);

	if (hasFixable) {
		console.log();
		console.log('You may also want run `npm run lint  --fix` to fix fixable problems.');
	}

	console.log();
	console.log('* If you\'re making a new rule, you can fix this later. *');
github benmosher / eslint-plugin-import / tests / src / rules / no-reassign.js View on Github external
'use strict'

var linter = require('eslint').linter,
    ESLintTester = require('eslint-tester')

var eslintTester = new ESLintTester(linter)

var test = require('../../utils').test

eslintTester.addRuleTest('lib/rules/no-reassign', {
  valid: [
    test({code: 'import { foo } from \'./bar\'; bar = 42;'})
    // may assign to imported names\' members
  , test({code: 'import { foo } from \'./bar\'; foo.x = 42; '})
    // may assign to imported namespaces\' names\' members
  , test({code: 'import * as foo from \'./bar\'; foo.x.y = 42; '})

    // ensure unnamed imports work
  , test({code: 'import \'./bar\'; '})
github pelotom / runtypes / testSetup.js View on Github external
// Register babel so that it will transpile ES6 to ES5
// before our tests run.
require('babel-register')()

// Run ESLint
const { CLIEngine } = require('eslint')
const cli = new CLIEngine()
const { results } = cli.executeOnFiles(['src'])
const formatter = cli.getFormatter('stylish')
// Indent results to align with mocha output
const summary = formatter(results).replace(/\n/g, '\n  ')
console.log(summary)
if (CLIEngine.getErrorResults(results).length) {
  // Throw to short-circuit the test suite if we have linting errors
  const err = new Error()
  // Don't allow Mocha to print a useless stack trace
  err.stack = ''
  throw err
}
github postmanlabs / postman-runtime / npm / test-lint.js View on Github external
function (report, next) {
            var errorReport = ESLintCLIEngine.getErrorResults(report.results);

            // log the result to CLI
            console.info(ESLintCLIEngine.getFormatter()(report.results));
            // log the success of the parser if it has no errors
            (errorReport && !errorReport.length) && console.info('eslint ok!'.green);
            // ensure that the exit code is non zero in case there was an error
            next(Number(errorReport && errorReport.length) || 0);
        }
    ], exit);
github postmanlabs / postman-sandbox / npm / test-lint.js View on Github external
function (report, next) {
            var errorReport = ESLintCLIEngine.getErrorResults(report.results);
            // log the result to CLI
            console.log(ESLintCLIEngine.getFormatter()(report.results));
            // log the success of the parser if it has no errors
            (errorReport && !errorReport.length) && console.log(chalk.green('eslint ok!'));
            // ensure that the exit code is non zero in case there was an error
            next(Number(errorReport && errorReport.length) || 0);
        }
    ], exit);
github postmanlabs / newman / npm / test-lint.js View on Github external
function (report, next) {
            var errorReport = ESLintCLIEngine.getErrorResults(report.results);
            // log the result to CLI
            console.info(ESLintCLIEngine.getFormatter()(report.results));
            // log the success of the parser if it has no errors
            (errorReport && !errorReport.length) && console.info('eslint ok!'.green);
            // ensure that the exit code is non zero in case there was an error
            next(Number(errorReport && errorReport.length) || 0);
        }
    ], exit);
github postmanlabs / postman-runtime / npm / test-lint.js View on Github external
function (report, next) {
            var errorReport = ESLintCLIEngine.getErrorResults(report.results);

            // log the result to CLI
            console.info(ESLintCLIEngine.getFormatter()(report.results));
            // log the success of the parser if it has no errors
            (errorReport && !errorReport.length) && console.info('eslint ok!'.green);
            // ensure that the exit code is non zero in case there was an error
            next(Number(errorReport && errorReport.length) || 0);
        }
    ], exit);
github postmanlabs / newman / npm / test-lint.js View on Github external
function (report, next) {
            var errorReport = ESLintCLIEngine.getErrorResults(report.results);
            // log the result to CLI
            console.info(ESLintCLIEngine.getFormatter()(report.results));
            // log the success of the parser if it has no errors
            (errorReport && !errorReport.length) && console.info('eslint ok!'.green);
            // ensure that the exit code is non zero in case there was an error
            next(Number(errorReport && errorReport.length) || 0);
        }
    ], exit);
github benmosher / eslint-plugin-import / tests / src / rules / no-amd.js View on Github external
'require("x", "y")',
    // random other function
    'setTimeout(foo, 100)',
    // non-identifier callee
    '(a || b)(1, 2, 3)',

    // nested scope is fine
    'function x() { define(["a"], function (a) {}) }',
    'function x() { require(["a"], function (a) {}) }',

    // unmatched arg types/number
    'define(0, 1, 2)',
    'define("a")',
	],

	invalid: semver.satisfies(eslintPkg.version, '< 4.0.0') ? [] : [
      { code: 'define([], function() {})', errors: [ { message: 'Expected imports instead of AMD define().' }] },
      { code: 'define(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD define().' }] },

      { code: 'require([], function() {})', errors: [ { message: 'Expected imports instead of AMD require().' }] },
      { code: 'require(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD require().' }] },
	],
})
github vuejs / eslint-plugin-vue / tests / lib / rules / valid-v-cloak.js View on Github external
* See LICENSE file in root directory for full license.
 */
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/valid-v-cloak')

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const tester = new RuleTester({
  parser: require.resolve('vue-eslint-parser'),
  parserOptions: { ecmaVersion: 2015 }
})

tester.run('valid-v-cloak', rule, {
  valid: [
    {
      filename: 'test.vue',
      code: ''
    },
    {
      filename: 'test.vue',
      code: '<template><div></div></template>'
    }
  ],
  invalid: [