Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 6, 2023
1 parent a972424 commit 2a50612
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
68 changes: 1 addition & 67 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('eslint').ESLint.LintResult} LintResult
* @typedef {import('eslint').Linter.LintMessage} LintMessage
*/

import {statistics} from 'vfile-statistics'

/**
* Turn virtual files into a ESLint results.
*
* @param {Array<VFile>} files
* Virtual files.
* @returns {Array<LintResult>}
* Lint results.
*/
export function toESLint(files) {
return files.map((x) => file(x))
}

/**
* Map a file.
*
* @param {VFile} file
* Virtual file.
* @returns {LintResult}
* ESLint result.
*/
function file(file) {
const stats = statistics(file)

return {
filePath: file.path,
messages: file.messages.map((x) => message(x)),
fatalErrorCount: stats.fatal,
errorCount: stats.fatal,
warningCount: stats.nonfatal,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: [],
suppressedMessages: []
}
}

/**
* Map a message.
*
* @param {VFileMessage} x
* Virtual message.
* @returns {LintMessage}
* ESLint message.
*/
function message(x) {
/* c8 ignore next */
const end = x.position ? x.position.end : {line: undefined, column: undefined}
return {
fatal: x.fatal === true ? true : undefined,
severity: x.fatal ? 2 : 1,
ruleId: [x.source, x.ruleId].filter(Boolean).join(':') || null,
line: x.line || 0,
column: x.column || 0,
endLine: end.line || undefined,
endColumn: end.column || undefined,
message: x.reason
}
}
export {toESLint} from './lib/index.js'
67 changes: 67 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('eslint').ESLint.LintResult} LintResult
* @typedef {import('eslint').Linter.LintMessage} LintMessage
*/

import {statistics} from 'vfile-statistics'

/**
* Turn virtual files into a ESLint results.
*
* @param {Array<VFile>} files
* Virtual files.
* @returns {Array<LintResult>}
* Lint results.
*/
export function toESLint(files) {
return files.map((x) => file(x))
}

/**
* Map a file.
*
* @param {VFile} file
* Virtual file.
* @returns {LintResult}
* ESLint result.
*/
function file(file) {
const stats = statistics(file)

return {
filePath: file.path,
messages: file.messages.map((x) => message(x)),
fatalErrorCount: stats.fatal,
errorCount: stats.fatal,
warningCount: stats.nonfatal,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: [],
suppressedMessages: []
}
}

/**
* Map a message.
*
* @param {VFileMessage} x
* Virtual message.
* @returns {LintMessage}
* ESLint message.
*/
function message(x) {
/* c8 ignore next */
const end = x.position ? x.position.end : {line: undefined, column: undefined}
return {
fatal: x.fatal === true ? true : undefined,
severity: x.fatal ? 2 : 1,
ruleId: [x.source, x.ruleId].filter(Boolean).join(':') || null,
line: x.line || 0,
column: x.column || 0,
endLine: end.line || undefined,
endColumn: end.column || undefined,
message: x.reason
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 2a50612

Please sign in to comment.