How to use the eslint.linter function in eslint

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 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 google / eslint-closure / packages / eslint-plugin-googlejs / lib / config-tester.js View on Github external
goog.module('googlejs.plugin.config-tester');

const types = goog.require('googlejs.plugin.types');

// Hide node_module requires from closure.  Yes, I know it's awful but a bare
// `require` didn't work and neither did simply aliasing require to
// require.  There's no other way to have the code runnable by Node.js
// and Closure.  See//
// https://gist.github.com/ChadKillingsworth/b86a4cffaa71571b5d01 for possible
// solutions in the distant future.
const externalRequire = /** @type {function(string)} */ (eval('require'));

const lodash = require('lodash');
const assert = require('assert');
const util = require('utils');
const linter = require('eslint').linter;

/*
 * List every parameters possible on a test case that are not related to eslint
 * configuration
 */
const ConfigTesterParameters = [
  'code',
  'filename',
  'options',
  'args',
  'errors',
];

/**
 * Runs the rule for the given item.
 * @param {string} ruleName Name of the rule.
github mozfreddyb / eslint-plugin-scanjs-rules / tests / rules / call_insertAdjacentHTML.js View on Github external
* @copyright 2015 Mozilla Corporation. All rights reserved
 */
"use strict";

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

var eslint = require("eslint"),
  ESLintTester = require("eslint-tester");

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

var eslintTester = new ESLintTester(eslint.linter);
eslintTester.addRuleTest("lib/rules/call_insertAdjacentHTML", {
  valid: [
    { code: "insertAdjacentHTML" }
  ],    // Examples of code that should trigger the rule
  invalid: [

    {
      code: "insertAdjacentHTML(foo)",
      errors: [
        { message: "The function insertAdjacentHTML can be unsafe" }
      ]
    },
  ]
});  // auto-generated from scanjs rules.json
github microsoft / react-native-windows / lint / linterTransform.js View on Github external
eslint.linter.verify = function(text, config, filename, saveState) {
    var transformedText;
    try {
      transformedText = transformSource(text, filename);
    } catch (e) {
      return [{
        severity: 2,
        line: e.lineNumber,
        message: e.message,
        source: text
      }];
    }
    var originalLines = text.split('\n');
    var transformedLines = transformedText.split('\n');
    var warnings = originalVerify.call(eslint.linter, transformedText, config, filename, saveState);

    // JSX and ES6 transforms usually generate pretty ugly code. Let's skip lint warnings
    // about code style for lines that have been changed by transform step.
    // Note that more important issues, like use of undefined vars, will still be reported.
    return warnings.filter(function(error) {
      var lineHasBeenTransformed = originalLines[error.line - 1] !== transformedLines[error.line - 1];
      var shouldIgnore = ignoredStylisticRules[error.ruleId] && lineHasBeenTransformed;
      return !shouldIgnore;
    });
  };
}
github microsoft / react-native-windows / lint / linterTransform.js View on Github external
function setLinterTransform(transformSource) {
  var originalVerify = eslint.linter.verify;
  eslint.linter.verify = function(text, config, filename, saveState) {
    var transformedText;
    try {
      transformedText = transformSource(text, filename);
    } catch (e) {
      return [{
        severity: 2,
        line: e.lineNumber,
        message: e.message,
        source: text
      }];
    }
    var originalLines = text.split('\n');
    var transformedLines = transformedText.split('\n');
    var warnings = originalVerify.call(eslint.linter, transformedText, config, filename, saveState);

    // JSX and ES6 transforms usually generate pretty ugly code. Let's skip lint warnings
github zuixjs / zuix / build / scripts / lint.js View on Github external
*  This file is part of
 *  zUIx, Javascript library for component-based development.
 *        https://zuixjs.github.io/zuix
 *
 * @author Generoso Martello 
 */

const baseFolder = process.cwd();
// Commons
const fs = require('fs');
const path = require('path');
const recursive = require('fs-readdir-recursive');
// logging
const tlog = require(path.join(baseFolder, 'src/lib/logger'));
// ESLint
const linter = require('eslint').linter;
const lintConfig = require(path.join(baseFolder, '.eslintrc.json'));

const sourceFolder = path.join(baseFolder, 'src/js/');
const stats = {
    error: 0,
    warning: 0
};

function lint(callback) {
    recursive(sourceFolder).map((f, i) => {
        if (f.endsWith('.js')) {
            tlog.info('^B%s^R', f);
            const code = fs.readFileSync(sourceFolder + f, 'utf8');
            const issues = linter.verify(code, lintConfig, sourceFolder + f);
            issues.map((m, i)=>{
                if (m.fatal || m.severity > 1) {
github SebastienElet / eslint-plugin-mongodb / src / lib / rules / check-rename-updates.mocha.js View on Github external
'use strict';

var linter = require('eslint').linter;
var RuleTester = require('eslint').RuleTester;
var rule = require('./check-rename-updates');

var ruleTester = new RuleTester(linter);

ruleTester.run('check-rename-updates', rule, {
  valid: [
    "db.collection('users').updateMany({}, { $rename: { qty: 'quantity' } });",
    "mongoClient.db.collection('users').updateMany({}, { $rename: { qty: newName } });", // Computed
    "mongoClient.db.collection('users').updateMany({}, { $rename: { qty: 'quantity', qty2: newName, } });", // Mix
  ],
  invalid: [{
    code: "db.collection('users').updateMany({}, { $rename: 'test' });",
    errors: [{
      message: 'Expected $rename operator value to be an object.',
    }],
github benmosher / eslint-plugin-import / tests / lib / rules / test-import-visitor.js View on Github external
"use strict";

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

var eslintTester = new ESLintTester(linter);

eslintTester.addRuleTest("lib/rules/test-import-visitor", {
  valid: [
    "var validVariable = true;"
  ],

  invalid: [
    {
      code: "import foo from 'bar'",
      ecmaFeatures: { modules: true },
      errors: [ { message: "This is an import!" } ]
    }
  ]
github SebastienElet / eslint-plugin-mongodb / src / lib / rules / check-update-calls.mocha.js View on Github external
'use strict';

var linter = require('eslint').linter;
var RuleTester = require('eslint').RuleTester;
var rule = require('./check-update-calls');

var ruleTester = new RuleTester(linter);

ruleTester.run('check-update-calls', rule, {
  valid: [
    "db.collection('users').updateMany({}, { $rename: { qty: 'quantity' } });",
    "mongoClient.db.collection('users').updateOne({}, { $rename: { qty: newName } }, {});",
    "mongoClient.db.collection('users').updateMany(gen(), {}, {});",
    "mongoClient.db.collection('users').updateOne(ref, {}, {});",
    "mongoClient.db.collection('users').updateMany(gen(), gen(), gen());",
    "mongoClient.db.collection('users').updateOne(ref, ref, ref);",
  ],
  invalid: [{
    code: "db.collection('users').updateMany();",
github liferay / liferay-frontend-source-formatter / lib / lint_js.js View on Github external
loadPlugin(item, configPath);
			}
		);
	}

	var originalGlobals = config.globals;

	var importedAliases = config.importedAliases;

	if (importedAliases) {
		var newGlobals = _.zipObject(importedAliases, _.times(importedAliases.length, _.stubTrue));

		config.globals = _.assign({}, originalGlobals, newGlobals);
	}

	var results = eslint.linter.verify(contents, config, file);

	if (importedAliases) {
		config.globals = originalGlobals;
	}

	if (results.length) {
		var fixedContent = SourceCodeFixer.applyFixes(contents, results);

		if (fixedContent.fixed) {
			contents = fixedContent.output;
		}
	}

	return {
		contents,
		results