Skip to content

Commit

Permalink
eslint improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
DE7924 committed Dec 13, 2024
1 parent 0bb66e4 commit 8edd47f
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 8 deletions.
97 changes: 93 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { cwd, chdir } from "process";
import minimist from "minimist";
import { setup } from "./scripts/setup";

export type Command = { label: string; command: string };

export type stepResponse = { output: string; error: boolean };
export const failedEmoji = "❌";
export const passedEmoji = "✅";
Expand Down Expand Up @@ -114,6 +116,10 @@ export async function run(): Promise<void> {
? await analyze()
: undefined;

const eslintStr: stepResponse | undefined = doStaticAnalysis
? await eslint({ label: "ESLint", command: "npm run lint" })
: undefined;

// run Code Formatting
const codeFormattingStr: stepResponse | undefined = doCodeFormatting
? await formatting()
Expand All @@ -136,6 +142,7 @@ export async function run(): Promise<void> {
context,
setupStr,
analyzeStr,
eslintStr,
codeFormattingStr,
testingStr,
);
Expand All @@ -145,3 +152,49 @@ export async function run(): Promise<void> {
if (error instanceof Error) setFailed(error.message);
}
}

const eslint = async (command: Command): Promise<stepResponse> => {
let response: stepResponse = { output: "", error: false };

Check failure on line 157 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'response' is never reassigned. Use 'const' instead
let outputStr = "";
let error = false;

Check failure on line 159 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'error' is never reassigned. Use 'const' instead
try {
await exec(command.command, [], {
listeners: {
stdout: (data) => {
outputStr += data.toString();
},
stderr: (data) => {
outputStr += data.toString();
},
},
});
} catch (error) {
error = true;

Check failure on line 172 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

Do not assign to the exception parameter
setFailed(`Failed ${command.label}: ${error}`);

Check failure on line 173 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

Invalid type "unknown" of template literal expression
}

if (error) {
response.error = true;
// Parse the output to find errors and problems
const lines = outputStr.split("\n");
const table = lines
.map((line) => {
const match = line.match(/^(.*?):(\d+):(\d+): (.*)$/);
if (match) {
const [_, file, line, column, message] = match;

Check failure on line 184 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'_' is assigned a value but never used
return `<tr><td>${file}</td><td>${line}</td><td>${column}</td><td>${message}</td></tr>`;
}
return "";
})
.join("");

const problemCount = lines.filter((line) =>
line.match(/^(.*?):(\d+):(\d+): (.*)$/),
).length;
response.output = `<p>${problemCount} problem${problemCount !== 1 ? "s" : ""} found</p><details><summary>See Details</summary><table><tr><th>File</th><th>Line</th><th>Column</th><th>Message</th></tr>${table}</table></details>`;
return response;
} else {
response.output = `<li>${passedEmoji} - ${command.label}\n</li>`;
return response;
}
};
49 changes: 47 additions & 2 deletions src/scripts/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { setFailed } from "@actions/core";
import { stepResponse, buildComment } from "src/main";
import { exec } from "@actions/exec";

Check failure on line 2 in src/scripts/analyze.ts

View workflow job for this annotation

GitHub Actions / code-quality

'exec' is defined but never used
import { stepResponse, buildComment, Command, passedEmoji } from "src/main";

Check failure on line 3 in src/scripts/analyze.ts

View workflow job for this annotation

GitHub Actions / code-quality

'Command' is defined but never used

Check failure on line 3 in src/scripts/analyze.ts

View workflow job for this annotation

GitHub Actions / code-quality

'passedEmoji' is defined but never used

export const analyze = async (): Promise<stepResponse> => {
const commands = [
{ label: "Custom Elements Manifest Analyzer", command: "npm run analyze" },
{ label: "ESLint", command: "npm run lint" },
{ label: "Lit Analyzer", command: "npm run lint:lit-analyzer" },
];

Expand All @@ -17,3 +17,48 @@ export const analyze = async (): Promise<stepResponse> => {
return { output: commentBody.trim(), error: false };
}
};

// export const buildComment = async (
// commands: { label: string; command: string }[],
// ): Promise<string[]> => {
// let commentBody = "\n";
// let errorMessages = "";
// for (const { label, command } of commands) {
// const result = await runCommand(command, label);
// if (result) {
// commentBody += `<li>${failedEmoji} - ${label}
// <details><summary>See details</summary>${result}</details></li>`;
// errorMessages += `${result}`;
// } else {
// commentBody += `<li>${passedEmoji} - ${label}\n</li>`;
// }
// }
// return [commentBody, errorMessages];
// };

// export const runCommand = async (
// command: string,
// label: string,
// ): Promise<string | boolean> => {
// let output = "";
// try {
// await exec(command, [], {
// listeners: {
// stdout: (data) => {
// output += data.toString();
// },
// },
// });
// return false;
// } catch (error: unknown) {
// if (error instanceof Error) {
// debug(`${label} failed: ${error.message}`);
// return output;
// } else if (typeof error === "string") {
// debug(`${label} failed: ${error}`);
// return output;
// } else {
// return true;
// }
// }
// };
2 changes: 2 additions & 0 deletions src/scripts/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const comment = async (
context: Context,
setupStr: stepResponse | undefined,
analyzeStr: stepResponse | undefined,
eslintStr: stepResponse | undefined,
codeFormattingStr: stepResponse | undefined,
testingStr: stepResponse | undefined,
): Promise<stepResponse> => {
Expand All @@ -17,6 +18,7 @@ export const comment = async (
<ul>
${setupStr?.output}
${analyzeStr?.output}
${eslintStr?.output}
${codeFormattingStr?.output}
${testingStr?.output}
</ul>`;
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const testing = async (): Promise<stepResponse> => {
label: "Install PlayWright Browsers",
command: "npx playwright install --with-deps",
},
{ label: "Testing", command: "npm run test" },
{ label: "Testing", command: "npm run test -- --coverage" },
{ label: "TSDoc", command: "npm run docs" },
];

Expand Down

0 comments on commit 8edd47f

Please sign in to comment.