This code provides a function delintCode
that lints code using ESLint, optionally fixes identified issues, and returns both the original and fixed code along with linting results.
npm run import -- "delint notebooks"
var CLIEngine = require("eslint").CLIEngine;
var esLintConfig = {
parser: "babel-eslint",
"plugins": ["prettier"],
envs: ["es6", "node", "browser", "mocha"],
useEslintrc: false,
extends: ["eslint:recommended", "google"],
fix: true,
'fix-dry-run': true,
'fix-type': 'problem,suggestion,layout',
rules: {
'semi': 2,
'prefer-const': 2,
'no-var': 2,
'no-undef': 2,
'no-unused-vars': [2, {vars: 'all', args: 'all'}],
'max-len': 2,
'max-depth': 2,
"prettier/prettier": "error",
'quotes': [2, 'single']
}
};
var cli = new CLIEngine(Object.assign(esLintConfig, {fix: false}));
var fix = new CLIEngine(Object.assign(esLintConfig, {fix: true}));
function delintCode(code) {
// TODO: accept a file path
if(typeof code === 'string') {
code = [code]
}
try {
return code
.map(c => Object.assign(cli.executeOnText(c).results[0], {
source: c,
fixed: fix.executeOnText(c).results[0].output || c
}))
} catch (e) {
console.log(e.message)
console.log('Error: doing nothing to code.')
return [{
code: code
}]
}
}
module.exports = delintCode;
const { CLIEngine } = require('eslint');
const defaultConfig = {
parser: 'babel-eslint',
plugins: ['prettier'],
envs: ['es6', 'node', 'browser','mocha'],
useEslintrc: false,
extends: ['eslint:recommended', 'google'],
fix: false,
'fix-dry-run': false,
'fix-type': 'problem,suggestion,layout',
rules: {
'semi': 2,
'prefer-const': 2,
'no-var': 2,
'no-undef': 2,
'no-unused-vars': [2, { vars: 'all', args: 'all' }],
'max-len': 2,
'max-depth': 2,
"prettier/prettier": "error",
'quotes': [2,'single']
}
};
class ESLintConfig {
constructor(options) {
this.config = Object.assign({}, defaultConfig, options);
}
getFixConfig() {
return Object.assign({}, this.config, { fix: true });
}
getDryRunConfig() {
return Object.assign({}, this.config, { fix: false, 'fix-dry-run': true });
}
}
class ESLintEngine {
constructor(config) {
this.config = config;
}
executeOnText(text) {
const engine = new CLIEngine(this.config);
return engine.executeOnText(text);
}
}
function lintCode(code, configOptions) {
if (typeof code ==='string') {
code = [code];
}
try {
const config = new ESLintConfig(configOptions);
const fixConfig = config.getFixConfig();
const dryRunConfig = config.getDryRunConfig();
return code.map((text) => ({
...config.executeOnText(text).results[0],
source: text,
fixed: fixConfig.executeOnText(text).results[0].output || text
}));
} catch (error) {
console.error(error.message);
console.log('Error: doing nothing to code.');
return [{ code: code }];
}
}
module.exports = lintCode;
This code defines a function delintCode
that performs code linting using ESLint and optionally fixes identified issues.
Here's a breakdown:
Initialization:
CLIEngine
class from the eslint
package.esLintConfig
object with various ESLint configuration options:
parser
: Specifies the parser to use (Babel for ES6+ code).plugins
: Includes the prettier
plugin for code formatting.envs
: Sets the environment contexts for linting (ES6, Node.js, browser, Mocha).useEslintrc
: Disables using a separate .eslintrc
file.extends
: Inherits rules from ESLint's recommended rules and Google's style guide.fix
: Enables automatic fixing of linting issues.fix-dry-run
: Enables a dry run of the fixers without actually modifying the code.fix-type
: Specifies the types of issues to fix (problems, suggestions, layout).rules
: Defines custom ESLint rules and their severity levels.CLIEngine
instances:
cli
: For linting without fixing issues.fix
: For linting and fixing issues.delintCode
Function:
cli.executeOnText
to lint the code and fix.executeOnText
to fix issues.Export:
delintCode
function for use in other parts of the application.