-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start: resolve, lint, process, constnts, utils
Signed-off-by: Charlike Mike Reagent <[email protected]>
- Loading branch information
1 parent
c84e97f
commit 366c619
Showing
9 changed files
with
437 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
parsers: { | ||
'eslint-esnext': require('babel-eslint'), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.