Skip to content

Commit

Permalink
normalizing, merging & utils; use new config format entirely
Browse files Browse the repository at this point in the history
Signed-off-by: Charlike Mike Reagent <[email protected]>
  • Loading branch information
tunnckoCore committed Mar 15, 2020
1 parent 7b3440b commit cbb9119
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 125 deletions.
3 changes: 3 additions & 0 deletions custom-eslint-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module.exports = {
parsers: {
'eslint-esnext': require('babel-eslint'),
},
// rules: {
// 'my-foo-plugin/some-my-rule': () => {},
// },
};
100 changes: 86 additions & 14 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
const myConfig = {
plugins: {
unicorn: require('eslint-plugin-unicorn'),
},
rules: {
...require('eslint-config-airbnb').rules,
'unicorn/no-process-exit': ['error'],
'unicorn/consistent-function-scoping': 'off',
'no-unused-vars': 'error',
semi: 'error',
},
};

module.exports = [
// {
// files: 'packages/*/src/**/*.js',
// plugins: {
// unicorn: require('eslint-plugin-unicorn'),
// 'custom-plugin': require('./custom-eslint-plugin'),
// },
// languageOptions: {
// // directly requiring the parser
// // parser: require('babel-eslint'),

// // or: through the loaded plugin
// parser: 'custom-plugin/eslint-esnext',
// ecmaVersion: 2020,
// sourceType: 'module',
// },
// rules: {
// ...require('eslint-config-airbnb').rules,
// 'unicorn/no-process-exit': ['error'],
// 'unicorn/consistent-function-scoping': 'off',
// semi: 'error',
// },
// },
// {
// files: '**/__tests__/**/*.{js,jsx}',
// plugins: {
// unicorn: require('eslint-plugin-unicorn'),
// 'custom-plugin': require('./custom-eslint-plugin'),
// react: require('eslint-plugin-react'),
// },
// languageOptions: {
// // directly requiring the parser
// // parser: require('babel-eslint'),

// // or: through the loaded plugin
// parser: 'custom-plugin/eslint-esnext',
// ecmaVersion: 2020,
// sourceType: 'module',
// },
// rules: {
// ...require('eslint-config-airbnb').rules,
// 'unicorn/no-process-exit': ['error'],
// 'unicorn/consistent-function-scoping': 'off',
// semi: 'error',
// 'react/jsx-uses-react': 'error',
// 'global-require': 'error',
// },
// },
{
name: 'loading-babel-eslint-parser-through-custom-plugin',
plugins: {
Expand All @@ -8,7 +68,14 @@ module.exports = [

// example using the custom loaded parser
{
name: 'use-parser-from-custom-plugin',
plugins: {
baw: require('eslint-plugin-import'),
},
languageOptions: {
globals: {
qwqwqw: true,
},
// directly requiring the parser
// parser: require('babel-eslint'),

Expand All @@ -17,24 +84,29 @@ module.exports = [
},
},
{
name: 'lint-sources',
files: 'packages/*/src/**/*.js',
plugins: {
unicorn: require('eslint-plugin-unicorn'),
},
rules: {
'unicorn/no-process-exit': ['error'],
'unicorn/consistent-function-scoping': 'error',
semi: 'error',
...myConfig,
},
{
name: 'lang-options',
languageOptions: {
ecmaVersion: 2020,
sourceType: 'commonjs',
},
},
{
name: 'lint-tests',
files: '**/__tests__/**/*.{js,jsx}',
plugins: {
react: require('eslint-plugin-react'),
},
rules: {
'react/jsx-uses-react': 'error',
'global-require': 'error',
},
...myConfig,
// {
// plugins: {
// react: require('eslint-plugin-react'),
// },
// rules: {
// 'react/jsx-uses-react': 'error',
// 'global-require': 'error',
// },
// },
},
];
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
"@zeit/ncc": "^0.21.1",
"acorn-globals": "ForbesLindesay/acorn-globals#greenkeeper/acorn-7.1.1",
"babel-eslint": "^10.1.0",
"defaults-deep": "^0.2.4",
"eslint": "^6.8.0",
"husky": "^4.2.3",
"jest": "^25.1.0",
"lerna": "^3.20.2",
"lint-staged": "^10.0.8",
"lockfile-lint": "^4.0.0",
"merge-deep": "^3.0.2",
"mixin-deep": "^2.0.1",
"prettier": "^1.19.1",
"prettier-plugin-pkgjson": "^0.2.4"
},
Expand Down
10 changes: 1 addition & 9 deletions packages/eslint/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const utils = require('./src/utils');
await lintConfigItems(config, {
config: oldCfg,
// linter,
inject: true,
fix: true,
// mapper: (ctx) => {
// const meta = ctx.cacheFile && ctx.cacheFile.metadata;
// const rep = (meta && meta.report) || null;
Expand All @@ -30,14 +30,6 @@ const utils = require('./src/utils');
// },
});

const used = process.memoryUsage();

Object.keys(used).forEach((key) => {
console.log(
`${key} ${Math.round((used[key] / 1024 / 1024) * 100) / 100} MB`,
);
});

// if (report.errorCount === 0 && report.warningCount === 0) {
// console.log('No problems found.');
// return;
Expand Down
108 changes: 108 additions & 0 deletions packages/eslint/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// async function pFlatten(arr, ...args) {
// const items = await (await arr).reduce(async (acc, e) => {
// const accum = await acc;

// let item = await e;

// if (typeof item === 'function') {
// item = await item(...args);
// }
// if (!item) {
// return accum;
// }
// if (Array.isArray(item)) {
// // if the element is an array, fall flatten on it again and then take the returned value and concat it.
// return accum.concat(await pFlatten(item));
// }
// // otherwise just concat the value.
// return accum.concat(item);
// }, Promise.resolve([])); // initial value for the accumulator is []

// return Promise.all(items);
// }

// pFlatten(
// Promise.resolve([
// null,
// { foo: 1 },
// () => null,
// (ctx) => [
// null,
// ctx.arr,
// { z: 4 },
// [null, { sasa: 33 }, async () => ({ as: 2 })],
// async () => ({ dada: 22 }),
// ],
// async (ctx) => ({ a: 1, ...ctx.asyncFn }),
// [
// null,
// { qux: 2 },
// [null, { zaz: 3 }, () => ({ f: 123 })],
// async () => [null, { qw: 534 }],
// ],
// ]),
// {
// arr: { ok: 123 },
// asyncFn: { zazzz: 888 },
// },
// ).then(console.log);

const defaultsDeep = require('defaults-deep');
const mergeDeep = require('merge-deep');
const mixinDeep = require('mixin-deep');

console.log(
mixinDeep(
{
settings: {},
languageOptions: {
globals: {
sas: 'ok',
},
parser: 'str',
parserOptions: {},
linterOptions: {
reportUnusedDisableDirectives: 'string',
},
},
plugins: {
react: {
rules: {
jsx: () => {},
},
},
},
rules: {
'react/jsx': 'error',
semi: 'error',
},
},
{
settings: { foo: 123 },
processor: 'md',
languageOptions: {
globals: {
win: 1,
sas: 'yep',
},
ecmaVersion: 2020,
sourceType: 'module',
parser: 'babel-eslint',
},
plugins: {
unicorn: {
rules: {
'foo-bar': () => {},
'qux-zaz': () => {},
},
},
},
rules: {
'unicorn/foo-bar': 'off',
'unicorn/qux-zaz': 'error',
semi: 'off',
foobie: 'error',
},
},
),
);
42 changes: 23 additions & 19 deletions packages/eslint/src/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable max-statements */
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */

'use strict';

// require('v8-compile-cache');

// const fs = require('fs');
// const util = require('util');
const path = require('path');
Expand Down Expand Up @@ -75,6 +78,7 @@ async function* resolvePatternsStream(patterns, options) {
// // console.log(contexts);
// return lintFiles(contexts, opts);
// }

async function lintFiles(items, options) {
const opts = { ...constants.DEFAULT_OPTIONS, ...options, forceLoad: true };

Expand All @@ -86,13 +90,14 @@ async function lintFiles(items, options) {
contexts.map(async (ctx) => {
const meta = ctx.cacheFile && ctx.cacheFile.metadata;

if (ctx.changed === false && ctx.notFound === false) {
if (ctx.changed === false && ctx.notFound === false && meta) {
// console.log(ctx.file.path);
results.push(meta.report);
return;
}

const hrstart = process.hrtime();

const { source, messages } = utils.lint({
...opts,
filename: ctx.file.path,
Expand All @@ -106,22 +111,22 @@ async function lintFiles(items, options) {

const hrend = process.hrtime(hrstart);

console.log('#', ctx.file.path);
console.info(
'Execution time (hr): %ds %dms',
hrend[0],
hrend[1] / 1000000,
);
if (opts.verbose) {
console.log('#', ctx.file.path);
console.log('# Size:', ctx.file.size);
console.info('# Execution time:', hrend[0], hrend[1] / 1000000);

const used = process.memoryUsage();
const used = process.memoryUsage();

Object.keys(used).forEach((key) => {
console.log(
`${key} ${Math.round((used[key] / 1024 / 1024) * 100) / 100} MB`,
);
});
Object.keys(used).forEach((key) => {
console.log(
`# ${key}:`,
Math.round((used[key] / 1024 / 1024) * 100) / 100,
);
});

console.log('########');
console.log('########');
}

// const cacheReport = (meta && meta.report) || {};
// removing `source` from the meta cached,
Expand All @@ -133,9 +138,9 @@ async function lintFiles(items, options) {
const reportChanged = JSON.stringify(res) !== JSON.stringify(rep);

// TODO optionally!
// if (res.errorCount > 0 || res.warningCount > 0) {
// // console.error(utils.cleanFrame([res]));
// }
if (res.errorCount > 0 || (res.warningCount > 0 && opts.warnings)) {
console.error(utils.cleanFrame([res]));
}

// const { config, ...setts } = opts;
// console.log(setts, reportChanged, ctx.file.path);
Expand All @@ -149,7 +154,7 @@ async function lintFiles(items, options) {
}),
);

const report = utils.createReportOrResult('results', results);
const report = utils.createReportOrResult('results', results.filter(Boolean));
report.contexts = contexts;

return report;
Expand Down Expand Up @@ -207,7 +212,6 @@ async function resolvePatterns(patterns, options) {
const results = [];

for await (const ctx of iterable) {
console.log(ctx.file.path);
if (opts.forceLoad === true) {
results.push(ctx);
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable baw/no-dynamic-require */
/* eslint-disable max-statements */
/* eslint-disable no-restricted-syntax */

Expand Down
Loading

0 comments on commit cbb9119

Please sign in to comment.