Skip to content

Commit

Permalink
LPD-40716 - Add mechanism to silence specific ESLint errors from SF b…
Browse files Browse the repository at this point in the history
…ut make sure they still show up in IDE
  • Loading branch information
bryceosterhaus authored and brianchandotcom committed Oct 30, 2024
1 parent aaaf741 commit bc425dc
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 13 deletions.
5 changes: 4 additions & 1 deletion modules/_node-scripts/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ const COMMANDS = {
If --check is passed no file is modified and the command just outputs what files need to be
formatted.
If --emit-suppressed is passed, the list of errors that are suppressed will be logged as well.
See this help's introduction to find the meaning of --all, --current-branch, ... parameters.
`,
parameters: '[--check] [{--all|--current-branch|--local-changes}]',
parameters:
'[--check] [--emit-suppressed] [{--all|--current-branch|--local-changes}]',
script: './format/index.mjs',
},
'format:file': {
Expand Down
27 changes: 23 additions & 4 deletions modules/_node-scripts/format/format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ const FALLBACK_FILE_PATH = '__fallback__.js';
* A string with the result of the format operation (empty if nothing was formatted or had errors)
* or undefined if no files were checked.
*/
export default async function format(fix, filesToFormat = undefined) {
export default async function format(
fix,
filesToFormat = undefined,
{emitSuppressed} = {}
) {
const suppressedErrors = await fs.readFile(
path.join(import.meta.dirname, 'suppressed_errors.txt'),
'utf-8'
);

const rootDir = await getRootDir();

const filepaths = await getFilePaths(rootDir, filesToFormat);
Expand Down Expand Up @@ -75,15 +84,25 @@ export default async function format(fix, filesToFormat = undefined) {
}

async function formatWithEslint(input, filepath) {
const relativePath = path.relative(rootDir, filepath);
const [lintResult = {}] = await eslintCLI.lintText(input, {
filePath: filepath,
});

const {messages, output} = lintResult;
const {messages = [], output} = lintResult;

const filteredErrors = emitSuppressed
? messages
: messages.filter(
(item) =>
!suppressedErrors.includes(
`${relativePath}:${item.message}\n`
)
);

if (messages?.length) {
if (filteredErrors?.length) {
errMessages[filepath] = errMessages[filepath] || [];
errMessages[filepath].push(...messages);
errMessages[filepath].push(...filteredErrors);
}

return output ?? input;
Expand Down
18 changes: 11 additions & 7 deletions modules/_node-scripts/format/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import gitUtil from '../util/gitUtil.mjs';
import format from './format.mjs';

export default async function main() {
const {all, check, currentBranch, localChanges} = getNamedArguments({
all: '--all',
check: '--check',
currentBranch: '--current-branch',
localChanges: '--local-changes',
});
const {all, check, currentBranch, emitSuppressed, localChanges} =
getNamedArguments({
all: '--all',
check: '--check',
currentBranch: '--current-branch',
emitSuppressed: '--emit-suppressed',
localChanges: '--local-changes',
});

const cwd = path.resolve('.');
const rootDir = await getRootDir();
Expand Down Expand Up @@ -64,7 +66,9 @@ export default async function main() {

console.log('📝 Running format...\n');

const formatOutput = await format(!check, files);
const formatOutput = await format(!check, files, {
emitSuppressed,
});

if (check && formatOutput) {
console.error(formatOutput);
Expand Down
Loading

0 comments on commit bc425dc

Please sign in to comment.