de-linting | | delint specific cells | Search

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.

Run example

npm run import -- "delint notebooks"

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;

What the code could have been:

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:

  1. Initialization:

  2. delintCode Function:

  3. Export: