Permalink
Cannot retrieve contributors at this time
86 lines (71 sloc)
2.12 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/eslint-plugin-github/lib/formatters/stylish-fixes.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
'use strict' | |
const childProcess = require('child_process') | |
const fs = require('fs') | |
const os = require('os') | |
const path = require('path') | |
let SourceCodeFixer = null | |
try { | |
SourceCodeFixer = require('eslint/lib/linter/source-code-fixer') | |
} catch (e) { | |
SourceCodeFixer = require('eslint/lib/util/source-code-fixer') | |
} | |
const getRuleURI = require('eslint-rule-documentation') | |
module.exports = function (results) { | |
let output = '\n' | |
let errors = 0 | |
let warnings = 0 | |
const rootPath = process.cwd() | |
for (const result of results) { | |
const messages = result.messages | |
if (messages.length === 0) { | |
continue | |
} | |
errors += result.errorCount | |
warnings += result.warningCount | |
const relativePath = path.relative(rootPath, result.filePath) | |
output += `${relativePath}\n` | |
for (const message of messages) { | |
output += `${message.line}:${message.column} ${message.ruleId || ''}` | |
if (message.ruleId) { | |
const ruleURI = getRuleURI(message.ruleId) | |
if (ruleURI.found) { | |
output += ` (${ruleURI.url})` | |
} | |
} | |
output += `\n\t${message.message}\n` | |
} | |
if (messages.some(msg => msg.fix)) { | |
const fixResult = SourceCodeFixer.applyFixes(result.source, messages) | |
output += `\n\n$ eslint --fix ${relativePath}\n` | |
output += diff(result.source, fixResult.output) | |
} | |
output += '\n\n' | |
} | |
const total = errors + warnings | |
if (total > 0) { | |
output += [ | |
'\u2716 ', | |
total, | |
pluralize(' problem', total), | |
' (', | |
errors, | |
pluralize(' error', errors), | |
', ', | |
warnings, | |
pluralize(' warning', warnings), | |
')\n', | |
].join('') | |
} | |
return total > 0 ? output : '' | |
} | |
function pluralize(word, count) { | |
return count === 1 ? word : `${word}s` | |
} | |
function diff(a, b) { | |
const aPath = path.join(os.tmpdir(), 'a.js') | |
const bPath = path.join(os.tmpdir(), 'p.js') | |
fs.writeFileSync(aPath, a, {encoding: 'utf8'}) | |
fs.writeFileSync(bPath, b, {encoding: 'utf8'}) | |
const result = childProcess.spawnSync('diff', ['-U5', aPath, bPath], {encoding: 'utf8'}) | |
return result.stdout.split('\n').slice(2).join('\n') | |
} |