Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial port to new ESLint API #275

Merged
merged 20 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: 'linting'

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
runs-on: 'ubuntu-latest'

strategy:
matrix:
node-version: ['lts/*']
fail-fast: false

steps:
- name: 'Checkout Project'
uses: 'actions/checkout@v2.4.0'
with:
fetch-depth: 0

- name: Use Node.js ${{ matrix.node-version }}
uses: 'actions/setup-node@v2.4.1'
with:
node-version: ${{ matrix.node-version }}

- name: 'Cache Node dependencies'
uses: 'actions/cache@v2.1.6'
with:
path: '~/.npm'
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: 'Install Dependencies'
run: 'npm install'

- name: 'Run Tests'
run: 'npm run check'
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
run: 'npm install'

- name: 'Run Tests'
run: 'npm test'
run: 'npm run test-ci'
16 changes: 8 additions & 8 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Flags (advanced):
}

Promise.resolve(argv.stdin ? getStdin() : '').then(async stdinText => {
/** @type {import('eslint').CLIEngine.LintReport} */
/** @type {import('eslint').ESLint.LintResult[]} */
let result

try {
Expand All @@ -144,24 +144,24 @@ Flags (advanced):
if (!result) throw new Error('expected a result')

if (outputFixed) {
if (result.results[0] && result.results[0].output) {
if (result[0] && result[0].output) {
// Code contained fixable errors, so print the fixed code
process.stdout.write(result.results[0].output)
process.stdout.write(result[0].output)
} else {
// Code did not contain fixable errors, so print original code
process.stdout.write(stdinText)
}
}

if (!result.errorCount && !result.warningCount) {
if (result.some(result => result.errorCount || result.warningCount) === false) {
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
process.exitCode = 0
return
}

console.error('%s: %s (%s)', opts.cmd, opts.tagline, opts.homepage)

// Are any warnings present?
const isSomeWarnings = result.results.some(item => item.messages.some(message => message.severity === 1))
const isSomeWarnings = result.some(item => item.messages.some(message => message.severity === 1))

if (isSomeWarnings) {
const homepage = opts.homepage != null ? ` (${opts.homepage})` : ''
Expand All @@ -173,7 +173,7 @@ Flags (advanced):
}

// Are any fixable rules present?
const isSomeFixable = result.results.some(item => item.messages.some(message => !!message.fix))
const isSomeFixable = result.some(item => item.messages.some(message => !!message.fix))

if (isSomeFixable) {
console.error(
Expand All @@ -183,7 +183,7 @@ Flags (advanced):
)
}

for (const item of result.results) {
for (const item of result) {
for (const message of item.messages) {
log(
' %s:%d:%d: %s%s%s',
Expand All @@ -197,7 +197,7 @@ Flags (advanced):
}
}

process.exitCode = result.errorCount ? 1 : 0
process.exitCode = result.some(result => result.errorCount) ? 1 : 0
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
})
.catch(err => process.nextTick(() => { throw err }))
}
Expand Down
46 changes: 18 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ const CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()

const { resolveEslintConfig } = require('./lib/resolve-eslint-config')

/** @typedef {ConstructorParameters<typeof import('eslint').CLIEngine>[0]} CLIEngineOptions */
// TODO: Remove
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
/** @typedef {ConstructorParameters<typeof import('eslint').CLIEngine>[0]} FooTemp */
/** @typedef {import('eslint').ESLint.Options} EslintOptions */
/** @typedef {Omit<import('./lib/resolve-eslint-config').ResolveOptions, 'cmd'|'cwd'>} BaseLintOptions */

/**
* @typedef LinterOptions
* @property {string} cmd
* @property {import('eslint')} eslint
* @property {string} [cwd]
* @property {CLIEngineOptions} [eslintConfig]
* @property {EslintOptions} [eslintConfig]
* @property {import('./lib/resolve-eslint-config').CustomEslintConfigResolver} [resolveEslintConfig]
* @property {string} [version]
*/
Expand All @@ -42,23 +44,19 @@ class Linter {
// Example cache location: ~/.cache/standard/v12/
const cacheLocation = path.join(CACHE_HOME, this.cmd, `v${majorVersion}/`)

/** @type {CLIEngineOptions} */
/** @type {EslintOptions} */
this.eslintConfig = {
cache: true,
cacheLocation,
envs: [],
fix: false,
globals: [],
plugins: [],
ignorePattern: [],
extensions: [],
useEslintrc: false,
...(opts.eslintConfig || {})
}

if (this.eslintConfig.configFile != null) {
if (this.eslintConfig.overrideConfigFile != null) {
this.eslintConfig.resolvePluginsRelativeTo = path.dirname(
this.eslintConfig.configFile
this.eslintConfig.overrideConfigFile
)
}
}
Expand All @@ -68,50 +66,41 @@ class Linter {
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @returns {import('eslint').CLIEngine.LintReport}
* @returns {Promise<import('eslint').ESLint.LintResult[]>}
*/
lintTextSync (text, opts) {
async lintText (text, { filename: filePath, ...opts } = {}) {
const eslintConfig = this.resolveEslintConfig(opts)
const engine = new this.eslint.CLIEngine(eslintConfig)
return engine.executeOnText(text, (opts || {}).filename)
}

/**
* Lint text to enforce JavaScript Style.
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @returns {Promise<import('eslint').CLIEngine.LintReport>}
*/
async lintText (text, opts) {
return this.lintTextSync(text, opts)
const engine = new this.eslint.ESLint(eslintConfig)
return engine.lintText(text, { filePath })
}

/**
* Lint files to enforce JavaScript Style.
*
* @param {Array<string>} files file globs to lint
* @param {BaseLintOptions & { cwd?: string }} [opts] base options + file globs to ignore (has sane defaults) + current working directory (default: process.cwd())
* @returns {Promise<import('eslint').CLIEngine.LintReport>}
* @returns {Promise<import('eslint').ESLint.LintResult[]>}
*/
async lintFiles (files, opts) {
const eslintConfig = this.resolveEslintConfig(opts)

if (typeof files === 'string') files = [files]
if (files.length === 0) files = ['.']

const result = new this.eslint.CLIEngine(eslintConfig).executeOnFiles(files)
const eslintInstance = new this.eslint.ESLint(eslintConfig)
// const result = new this.eslint.CLIEngine(eslintConfig).executeOnFiles(files)
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
const result = await eslintInstance.lintFiles(files)

if (eslintConfig.fix) {
this.eslint.CLIEngine.outputFixes(result)
this.eslint.ESLint.outputFixes(result)
}

return result
}

/**
* @param {BaseLintOptions & { cwd?: string }} [opts]
* @returns {CLIEngineOptions}
* @returns {EslintOptions}
*/
resolveEslintConfig (opts) {
const eslintConfig = resolveEslintConfig(
Expand All @@ -129,4 +118,5 @@ class Linter {
}

module.exports.cli = require('./bin/cmd')
// FIXME: Rename to avoid confusion with ESLint provided "Linter"
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
module.exports.Linter = Linter