Permalink
Cannot retrieve contributors at this time
executable file
50 lines (44 sloc)
1.72 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/bin/eslint-ignore-errors.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
#!/usr/bin/env node | |
// Disables eslint rules in a JavaScript file with next-line comments. This is | |
// useful when introducing a new rule that causes many failures. The comments | |
// can be fixed and removed at while updating the file later. | |
// | |
// Usage: | |
// | |
// eslint-ignore-errors app/assets/javascripts/something.js | |
const fs = require('fs') | |
const execFile = require('child_process').execFile | |
execFile('eslint', ['--format', 'json', process.argv[2]], (error, stdout) => { | |
for (const result of JSON.parse(stdout)) { | |
const filename = result.filePath | |
const jsLines = fs.readFileSync(filename, 'utf8').split('\n') | |
const offensesByLine = {} | |
let addedLines = 0 | |
// Produces {47: ['github/no-d-none', 'github/no-blur'], 83: ['github/no-blur']} | |
for (const message of result.messages) { | |
if (offensesByLine[message.line]) { | |
offensesByLine[message.line].push(message.ruleId) | |
} else { | |
offensesByLine[message.line] = [message.ruleId] | |
} | |
} | |
for (const line of Object.keys(offensesByLine)) { | |
const lineIndex = line - 1 + addedLines | |
const previousLine = jsLines[lineIndex - 1] | |
const ruleIds = offensesByLine[line].join(', ') | |
if (isDisableComment(previousLine)) { | |
jsLines[lineIndex - 1] = previousLine.replace(/\s?\*\/$/, `, ${ruleIds} */`) | |
} else { | |
const leftPad = ' '.repeat(jsLines[lineIndex].match(/^\s*/g)[0].length) | |
jsLines.splice(lineIndex, 0, `${leftPad}/* eslint-disable-next-line ${ruleIds} */`) | |
} | |
addedLines += 1 | |
} | |
if (result.messages.length !== 0) { | |
fs.writeFileSync(filename, jsLines.join('\n'), 'utf8') | |
} | |
} | |
}) | |
function isDisableComment(line) { | |
return line.match(/\/\* eslint-disable-next-line .+\*\//) | |
} |