Skip to content

Commit bab04f9

Browse files
committed
DartPad の静的解析 APIの呼び出し処理を追加
`performAnalysis` の追加 - 解析結果に含まれるエラー・警告・情報(`issues`)を、行・列番号および修正提案(`correction`)を含めたフォーマットで出力コールバック (`onOutput`) に渡します。
1 parent 63c6ad2 commit bab04f9

1 file changed

Lines changed: 64 additions & 7 deletions

File tree

packages/runtime/src/dart/runtime.tsx

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ interface DartVersionResponse {
2626
flutterVersion?: string;
2727
}
2828

29+
interface AnalysisIssue {
30+
kind: "error" | "warning" | "info";
31+
message: string;
32+
location: {
33+
charStart: number;
34+
charLength: number;
35+
line: number;
36+
column: number;
37+
};
38+
code?: string;
39+
correction?: string;
40+
url?: string;
41+
}
42+
43+
interface AnalysisResponse {
44+
issues: AnalysisIssue[];
45+
imports?: unknown[];
46+
}
47+
2948
const versionFetcher = async (url: string): Promise<DartVersionResponse> => {
3049
const res = await fetch(url);
3150
if (!res.ok) {
@@ -74,6 +93,41 @@ export function DartProvider({ children }: { children: ReactNode }) {
7493
);
7594
}
7695

96+
async function performAnalysis(
97+
source: string,
98+
onOutput: (output: ReplOutput | UpdatedFile) => void
99+
): Promise<void> {
100+
try {
101+
const res = await fetch(`${DART_PAD_API_BASE}/analyze`, {
102+
method: "POST",
103+
headers: {
104+
"Content-Type": "application/json",
105+
},
106+
body: JSON.stringify({ source }),
107+
});
108+
109+
if (!res.ok) return;
110+
111+
const data: AnalysisResponse = await res.json();
112+
if (Array.isArray(data.issues)) {
113+
for (const issue of data.issues) {
114+
const line = issue.location?.line ?? 1;
115+
const column = issue.location?.column ?? 1;
116+
const kindStr = (issue.kind || "info").toUpperCase();
117+
const correctionStr = issue.correction ? ` (${issue.correction})` : "";
118+
const formattedMsg = `[ANALYZER ${kindStr}] line ${line}:${column} - ${issue.message}${correctionStr}`;
119+
120+
onOutput({
121+
type: issue.kind === "error" ? "error" : "stderr",
122+
message: formattedMsg,
123+
});
124+
}
125+
}
126+
} catch (err) {
127+
console.warn("Failed to perform Dart static analysis:", err);
128+
}
129+
}
130+
77131
export function useDart(): RuntimeContext {
78132
const { init: dartInit, ready, dartVersion } = useContext(DartContext);
79133
const onErrorRef = useRef<RuntimeErrorHandler | undefined>(undefined);
@@ -109,13 +163,16 @@ export function useDart(): RuntimeContext {
109163
}
110164

111165
try {
112-
const response = await fetch(`${DART_PAD_API_BASE}/compileNewDDC`, {
113-
method: "POST",
114-
headers: {
115-
"Content-Type": "application/json",
116-
},
117-
body: JSON.stringify({ source }),
118-
});
166+
const [_, response] = await Promise.all([
167+
performAnalysis(source, onOutput),
168+
fetch(`${DART_PAD_API_BASE}/compileNewDDC`, {
169+
method: "POST",
170+
headers: {
171+
"Content-Type": "application/json",
172+
},
173+
body: JSON.stringify({ source }),
174+
}),
175+
]);
119176

120177
if (!response.ok) {
121178
const errorText = await response.text();

0 commit comments

Comments
 (0)