-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.ts
203 lines (190 loc) · 6.44 KB
/
diagnostics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import ts from "typescript";
// @ts-ignore
import { ESLint } from "eslint";
import prettier from "prettier";
const eslint = new ESLint({
extensions: [".ts"],
useEslintrc: false,
overrideConfig: {
parser: "@typescript-eslint/parser",
extends: [
"eslint:recommended", // ESLint's recommended rules
"plugin:@typescript-eslint/recommended", // Recommended rules from the TypeScript plugin
],
parserOptions: {
ecmaVersion: 2022,
sourceType: "module",
loggerFn: () => {},
},
rules: {
"no-unused-expressions": "error",
},
env: {
es6: true,
node: true,
},
plugins: ["@typescript-eslint"],
},
});
const getLinterDiagnostics = async (code: string) => {
const messages = await eslint.lintText(code);
return messages.filter((x: ESLint.LintMessage) => x.line !== undefined).map((
el: ESLint.LintMessage,
) => prettifyLinterDiagnostic(code, el));
};
const compilerOptions = {
module: ts.ModuleKind.NodeNext,
target: ts.ScriptTarget.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
noEmit: true, // Don't output files
};
const getTSDiagnostics = (code: string) => {
const host = ts.createCompilerHost(compilerOptions);
const getSourceFile = host.getSourceFile;
host.getSourceFile = (filename: string) =>
filename === "file.ts"
? ts.createSourceFile(filename, code, ts.ScriptTarget.ESNext)
: getSourceFile(filename, ts.ScriptTarget.ESNext);
const program = ts.createProgram(["file.ts"], compilerOptions, host);
const diagnostics = ts.getPreEmitDiagnostics(program).filter((d) =>
d.file!.fileName === "file.ts"
);
return diagnostics.map((el) => prettifyTSDiagnostic(code, el));
};
const prettifyTSDiagnostic = (code: string, diagnostic: ts.Diagnostic) => {
const message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n",
);
const { line } = diagnostic.file!.getLineAndCharacterOfPosition(
diagnostic.start!,
);
//return `${line}: "${code.split("\n")[line]}" ${message}`;
// also saves the severity
return { line, issue: message, codeLine: code.split("\n")[line] };
};
const prettifyLinterDiagnostic = (
code: string,
diagnostic: ESLint.LintMessage,
) => {
const { line } = diagnostic;
//return `${line}: "${code.split("\n")[line]}" ${diagnostic.message}`;
// also saves the severity
return { line, issue: diagnostic.message, codeLine: code.split("\n")[line] };
};
const processDiagnostics = (
line: string,
lineNumber: number,
tsDiagnostics: any[],
linterDiagnostics: any[],
): string => {
const tsDiagnostic = tsDiagnostics.find(({ line: lineNum }) =>
lineNum === lineNumber
);
const linterDiagnostic = linterDiagnostics.find(({ line: lineNum }) => {
lineNum === lineNumber;
});
let diagnosticInfo = "";
if (tsDiagnostic) {
diagnosticInfo += `ERROR (TS): ${tsDiagnostic.issue}`;
}
if (linterDiagnostic) {
diagnosticInfo += `ERROR (ESLint): ${linterDiagnostic.issue}`;
}
return diagnosticInfo
? `${line.split("//")[0]} // ${diagnosticInfo.trim()}`
: line;
};
const getAllDiagnostics = async (
code: string,
outputSchema?: Record<string, any>,
): Promise<
{ diagnostics: any[]; lintedCode: string; diagnosticsString: string }
> => {
const tsDiagnostics = getTSDiagnostics(code);
const linterDiagnostics = await getLinterDiagnostics(code);
const outputSchemaMatches = matches(code, outputSchema);
if (outputSchemaMatches !== true) {
linterDiagnostics.push({
line: code.split("\n").length - 1,
issue: outputSchemaMatches,
codeLine: code.split("\n")[code.split("\n").length - 1],
});
}
const todos = noTODOs(code);
const lintedCode = code.split("\n").map((line, i) =>
processDiagnostics(line, i, tsDiagnostics, linterDiagnostics)
).join("\n");
const diagnostics = [...tsDiagnostics, ...linterDiagnostics, ...todos];
const diagnosticsString = diagnostics.reduce((acc, diagnostic) => {
return `${acc}\n- ${diagnostic.codeLine}: ${diagnostic.issue}`;
}, "");
return {
diagnostics,
lintedCode,
diagnosticsString,
};
};
const noTODOs = (code: string) => {
const lines = code.split("\n");
const todos = lines.map((x, i) => [x, i] as [string, number]).filter((x) =>
x[0].includes("TODO")
).map((x) => ({
line: x[1],
issue: "TODOs are not allowed in the code",
codeLine: x[0],
}));
return todos;
};
function extractDefaultExport(script: string): string {
const match = script.match(/export default\s+([\s\S]*?)(?=\/\/|export|$)/);
return (match ? match[1].trim() : "").trim();
}
const matches = (s: string, schema?: Record<string, any>) => {
const defaultExport = extractDefaultExport(s).trim();
console.log(defaultExport);
if (!schema) {
if (!defaultExport) {
return true;
}
return "Export error. No schema is provided but there is a default export. No schema means the code should only execute, NO EXPORT OF FUNCTIONS OR ANYTHING.";
}
if (!defaultExport) {
return "No default export found in file but schema is provided. The code should always export a default export that matches the schema.";
}
if (defaultExport.startsWith("{") && defaultExport.endsWith("}")) {
const inner = defaultExport.slice(1, -1);
const props = inner.split(",").map((s) => s.trim().split(":")[0]);
const properties = Object.keys(schema.properties);
const missing = properties.filter((p) => !props.includes(p));
if (missing.length) {
return `The following properties are missing in the default export: ${
missing.join(", ")
}.`;
}
return true;
} else if (defaultExport.startsWith('"') && defaultExport.endsWith('"')) {
console.log("her");
const type = schema.type;
if (type === "string") {
return true;
}
return `The default export is a string but the schema is not. Schema type is ${type}.`;
} else if (defaultExport.startsWith("[") && defaultExport.endsWith("]")) {
const type = schema.type;
if (type === "array") {
return true;
}
return `The default export is an array but the schema is not. Schema type is ${type}.`;
} else if (
defaultExport.startsWith("function") && defaultExport.endsWith("}")
) {
const type = schema.type;
if (type === "function") {
return true;
}
return `The default export is a function but the schema is not. Schema type is ${type}.`;
}
return "The default export is not a string, object or function. It should be one of those.";
};
export default getAllDiagnostics;