Skip to content

Commit

Permalink
start: resolve, lint, process, constnts, utils
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 7, 2020
1 parent c84e97f commit 366c619
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ module.exports = {
],
rules: {
'import/no-unresolved': 'off',
'import/no-dynamic-require': 'off',
'global-require': 'off',
},
};
5 changes: 5 additions & 0 deletions custom-eslint-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
parsers: {
'eslint-esnext': require('babel-eslint'),
},
};
99 changes: 99 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

// module.exports = {
// name: "name",
// files: ["*.js"],
// ignores: ["*.test.js"],
// settings: {},
// languageOptions: {
// ecmaVersion: 2020,
// sourceType: "module",
// globals: {},
// parser: object || "string",
// parserOptions: {},
// linterOptions: {
// reportUnusedDisableDirectives: "string"
// }
// }
// processor: object || "string",
// plugins: {}
// rules: {}
// };

const {
DEFAULT_FILES,
DEFAULT_IGNORE,
} = require('./packages/eslint/src/constants');

module.exports = [
/*
gets converted to
{
plugins: {
'eslint:recommended': require('somehow-load-eslint-internal-rules')
}
}
*/
// 'eslint:recommended',
{
name: 'loading-babel-eslint-parser-through-custom-plugin',
plugins: {
'custom-plugin': require('./custom-eslint-plugin'),
},
},

// consider linting src/index.jsx - both configs should apply for it
// so, what will the ConfigArray#getConfig(filename) return??
{
files: '**/*.{js,jsx}',
plugins: {
unicorn: require('eslint-plugin-unicorn'),
},
rules: {
'unicorn/no-process-exit': ['error'],
'unicorn/consistent-function-scoping': 'error',
semi: 'error',
},
},
{
files: '**/*.jsx',
plugins: {
react: require('eslint-plugin-react'),
},
rules: {
'react/jsx-uses-react': 'error',
'global-require': 'error',
},
},

{
name: 'some-tunnckocore-config',
files: DEFAULT_FILES,
ignores: DEFAULT_IGNORE,
languageOptions: {
globals: {},
// directly requiring the parser
// parser: require('babel-eslint'),

// or: through the loaded plugin
parser: 'custom-plugin/eslint-esnext',

// or require-ing of the plugin directly
// parser: require('./custom-eslint-plugin').parsers['eslint-esnext'],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
generators: false,
objectLiteralDuplicateProperties: false,
},
},
},
plugins: {},
rules: {
semi: ['error', 'always'],
'global-require': ['error', 'always'],
},
},
];
6 changes: 3 additions & 3 deletions packages/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"src"
],
"bin": {
"eslint": "src/bin.js",
"eslint-cli": "src/bin.js",
"hela-eslint": "src/bin.js"
},
"engines": {
Expand All @@ -37,8 +35,10 @@
"dependencies": {
"@hela/core": "^3.2.2",
"eslint": "^6.8.0",
"find-pkg": "^2.0.0",
"find-file-up": "^2.0.1",
"glob-cache": "^1.0.1",
"import-fresh": "^3.2.1",
"is-glob": "^4.0.1",
"memoize-fs": "^2.1.0"
},
"keywords": [
Expand Down
154 changes: 154 additions & 0 deletions packages/eslint/src/api.js
Original file line number Diff line number Diff line change
@@ -1 +1,155 @@
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */

'use strict';

const fs = require('fs');
const util = require('util');
const path = require('path');
const cacache = require('cacache');
const fastGlob = require('fast-glob');
const globCache = require('glob-cache');
const utils = require('./utils');

async function* resolveFilesStream(patterns, options) {
const opts = { ...utils.constants.DEFAULT_OPTIONS, ...options };
const iterable = await globCache(patterns, opts);

for await (const ctx of iterable) {
if (opts.forceLoad === true) {
yield ctx;
continue;
}
if (ctx.changed) {
yield ctx;
}
}
}

async function resolveFiles(patterns, options) {
const opts = { ...utils.constants.DEFAULT_OPTIONS, ...options };

const iterable = await globCache(patterns, opts);
const results = [];

for await (const ctx of iterable) {
if (opts.forceLoad === true) {
results.push(ctx);
continue;
}
if (ctx.changed) {
results.push(ctx);
}
}

return results;
}

/**
* Resolve and lint given glob patterns.
*
* @param {string|string[]} patterns - glob patterns to match
* @param {*} options
*/
async function processFiles(patterns, options) {
const opts = { ...utils.constants.DEFAULT_OPTIONS, ...options };
const files = await resolveFiles(patterns, opts);

return lintFiles(files, opts);
}

async function lintText(contents, options) {
const opts = { ...utils.constants.DEFAULT_OPTIONS, ...options };
const source = contents;

return {
source,
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
};
}

/**
* Lint already resolved Files, Contexts or full filepaths, or an array of them
*
* @param {File|File[]|Context|Context[]|string|string[]} items
* @param {*} options
*/
async function lintItems(items, options) {
const opts = { ...utils.constants.DEFAULT_OPTIONS, ...options };
const report = {
results: [],
errorCount: 0,
warningCount: 0,
};

await Promise.all(
[]
.concat(items)
.filter(Boolean)
.map(async (item) => {
if (opts.type === 'paths' || opts.type === 'files') {
const file = await utils.toFile(await item);

const repFromText = await lintText(file.contents, opts);

// calcReport(report, repFromText);
report.results.push(file.path);

await cacache.put(opts.cacheLocation, file.path, repFromText.source, {
metadata: {
report: repFromText,
config: opts.config,
},
});

return;
}
if (opts.type === 'contexts') {
const { file, cacheFile } = await item;
const meta = cacheFile && cacheFile.metadata;
const { source, ...repFromText } = await lintText(
file.contents,
opts,
);

const reportChanged =
JSON.stringify(repFromText) !== JSON.stringify(meta && meta.report);

if (opts.forceLoad === true || reportChanged) {
// calcReport(report, repFromText);
report.results.push(file.path);

await cacache.put(opts.cacheLocation, file.path, source, {
metadata: {
report: { ...repFromText, source },
config: opts.config,
},
});
}
}
}),
);

return report;
}

async function lintFiles(files, options) {
return lintItems(files, { ...options, type: 'files' });
}

// resolveFiles('modules/*/src/**/*.js', { forceLoad: true }).then(console.log);

lintFiles([
// 'foobar.js',
// { path: 'foobar.js' },
// { path: 'foobar.js', contents: Buffer.from('var foobar = 1;')},
// { file: { path: 'foobar.js' } },
// { file: { path: 'foobar.js', contents: Buffer.from('sasa') } },

// and promises resolving to one of above

Promise.resolve(path.join(process.cwd(), 'packages/eslint/src/api.js')),
]).then(console.log);
32 changes: 32 additions & 0 deletions packages/eslint/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/bower_components/**',
'flow-typed/**',
'coverage/**',
'**/*fixture*/**',
'{tmp,temp}/**',
'**/*.min.js',
'**/bundle.js',
'**/vendor/**',
'**/dist/**',
];

const DEFAULT_EXTENSIONS = ['js', 'jsx', 'cjs', 'mjs', 'ts', 'tsx'];
const DEFAULT_FILES = [
`**/src/**/*.{${DEFAULT_EXTENSIONS.join(',')}}`,
`**/*test*/**/*.{${DEFAULT_EXTENSIONS.join(',')}}`,
];

const DEFAULT_OPTIONS = {
forceReload: false,
cacheLocation: '.cache/hela-eslint-cache',
};

module.exports = {
DEFAULT_IGNORE,
DEFAULT_FILES,
DEFAULT_EXTENSIONS,
DEFAULT_OPTIONS,
};
2 changes: 2 additions & 0 deletions packages/eslint/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function wrapper(prog) {
.action(async (...args) => {
const files = args.slice(0, -2);
const argv = args[args.length - 2];

console.log('files', files);
});
}

Expand Down
Loading

0 comments on commit 366c619

Please sign in to comment.