Skip to content

Commit 983fc09

Browse files
authored
Merge pull request #63 from easyops-cn/steve/update-istanbul
chore(): update istanbul-lib-instrument to support ESM when reading coverage
2 parents 8617fae + 0d504e4 commit 983fc09

File tree

13 files changed

+121
-164
lines changed

13 files changed

+121
-164
lines changed
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// istanbul ignore file
22
import { wrap } from "comlink";
33
import type { MiniLineChartOptions } from "./draw.js";
4-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5-
// @ts-ignore
6-
import { getChartWorker } from "./worker.mjs";
74

85
export interface RemoteWorker {
96
init(id: string, canvas: OffscreenCanvas): Promise<void>;
@@ -13,12 +10,21 @@ export interface RemoteWorker {
1310

1411
let remoteWorkerPromise: Promise<RemoteWorker> | undefined;
1512

13+
let worker: Worker | undefined;
14+
1615
export function getRemoteWorker() {
1716
if (!remoteWorkerPromise) {
1817
remoteWorkerPromise = (async () => {
19-
const Remote = wrap(getChartWorker()) as any;
18+
const Remote = wrap<{ new (): RemoteWorker }>(getWorker());
2019
return await new Remote();
2120
})();
2221
}
2322
return remoteWorkerPromise;
2423
}
24+
25+
function getWorker() {
26+
if (!worker) {
27+
worker = new Worker(new URL("./chart.worker.ts", import.meta.url));
28+
}
29+
return worker;
30+
}

bricks/mini-chart/src/mini-line-chart/index.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jest.mock("./getRemoteWorker.js", () => ({
1414
});
1515
},
1616
}));
17-
jest.mock("./worker.mjs", () => ({}));
1817
jest.mock("./draw.js");
1918

2019
jest.mock(

bricks/mini-chart/src/mini-line-chart/worker.mjs

Lines changed: 0 additions & 20 deletions
This file was deleted.

bricks/vs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@next-core/theme": "^1.5.4",
6767
"@next-core/utils": "^1.7.32",
6868
"@next-shared/form": "^0.7.9",
69+
"comlink": "^4.4.2",
6970
"monaco-editor": "^0.50.0",
7071
"monaco-editor-webpack-plugin": "^7.1.0",
7172
"react": "0.0.0-experimental-ee8509801-20230117",

bricks/vs/src/code-editor/index.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
77

88
jest.mock("@next-core/theme", () => ({}));
99
jest.mock("@next-core/react-runtime");
10-
jest.mock("./workers/yamlLinter", () => ({}));
10+
jest.mock("./workers/yamlLinter.js", () => ({}));
1111

1212
global.ResizeObserver = ResizeObserver as any;
1313

bricks/vs/src/code-editor/index.tsx

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ import classNames from "classnames";
5252
import "./index.css";
5353
import { EmbeddedModelContext } from "./utils/embeddedModelState.js";
5454
import { PlaceholderContentWidget } from "./widget/Placeholder.js";
55-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
56-
// @ts-ignore
57-
import { getYamlLinterWorker } from "./workers/yamlLinter.mjs";
58-
import type { LintResponse } from "./workers/lintYaml.js";
55+
import { getRemoteYamlLinterWorker } from "./workers/yamlLinter.js";
5956
import { register as registerCel } from "./languages/cel.js";
6057
import { register as registerCelYaml } from "./languages/cel-yaml.js";
6158
import { register as registerCelStr } from "./languages/cel-str.js";
@@ -955,67 +952,65 @@ export function CodeEditorComponent({
955952
// istanbul ignore next
956953
useEffect(() => {
957954
const editor = editorRef.current;
958-
if (editor && language === "brick_next_yaml") {
959-
const yamlLinter = getYamlLinterWorker();
960-
const handleChange = (): void => {
961-
const value = editor.getValue();
962-
yamlLinter.postMessage({
963-
id: workerId,
964-
source: value,
965-
links,
966-
markers,
967-
});
968-
};
969-
const debounceChange = debounce(handleChange, 200);
970-
const handleLintResponse = (event: MessageEvent<LintResponse>): void => {
971-
const { id, lintMarkers, lintDecorations } = event.data;
972-
if (id !== workerId) {
973-
return;
974-
}
975-
const model = editor.getModel();
976-
if (!model) {
977-
return;
978-
}
979-
monaco.editor.setModelMarkers(
980-
model,
981-
"brick_next_yaml_lint",
982-
lintMarkers.map(({ start, end, message, severity, code }) => {
983-
const startPos = model.getPositionAt(start);
984-
const endPos = model.getPositionAt(end);
985-
return {
986-
startLineNumber: startPos.lineNumber,
987-
startColumn: startPos.column,
988-
endLineNumber: endPos.lineNumber,
989-
endColumn: endPos.column,
990-
severity: monaco.MarkerSeverity[severity],
991-
message,
992-
code: code as monaco.editor.IMarkerData["code"],
993-
};
994-
})
995-
);
996-
decorationsCollection.current?.set(
997-
lintDecorations.map(({ start, end, options }) => ({
998-
range: monaco.Range.fromPositions(
999-
model.getPositionAt(start),
1000-
model.getPositionAt(end)
1001-
),
1002-
options,
1003-
}))
1004-
);
1005-
};
1006-
yamlLinter.addEventListener("message", handleLintResponse);
1007-
const change = editor.onDidChangeModelContent(debounceChange);
1008-
debounceChange();
1009-
return () => {
1010-
change.dispose();
1011-
monaco.editor.setModelMarkers(
1012-
editor.getModel()!,
1013-
"brick_next_yaml_lint",
1014-
[]
1015-
);
1016-
yamlLinter.removeEventListener("message", handleLintResponse);
1017-
};
955+
if (!(editor && language === "brick_next_yaml")) {
956+
return;
1018957
}
958+
959+
let ignore = false;
960+
const handleChange = async () => {
961+
const worker = await getRemoteYamlLinterWorker();
962+
if (ignore) {
963+
return;
964+
}
965+
const { lintMarkers, lintDecorations } = await worker.lint({
966+
source: editor.getValue(),
967+
links,
968+
markers,
969+
});
970+
const model = editor.getModel();
971+
if (ignore || !model) {
972+
return;
973+
}
974+
monaco.editor.setModelMarkers(
975+
model,
976+
"brick_next_yaml_lint",
977+
lintMarkers.map(({ start, end, message, severity, code }) => {
978+
const startPos = model.getPositionAt(start);
979+
const endPos = model.getPositionAt(end);
980+
return {
981+
startLineNumber: startPos.lineNumber,
982+
startColumn: startPos.column,
983+
endLineNumber: endPos.lineNumber,
984+
endColumn: endPos.column,
985+
severity: monaco.MarkerSeverity[severity],
986+
message,
987+
code: code as monaco.editor.IMarkerData["code"],
988+
};
989+
})
990+
);
991+
decorationsCollection.current?.set(
992+
lintDecorations.map(({ start, end, options }) => ({
993+
range: monaco.Range.fromPositions(
994+
model.getPositionAt(start),
995+
model.getPositionAt(end)
996+
),
997+
options,
998+
}))
999+
);
1000+
};
1001+
const debounceChange = debounce(handleChange, 200);
1002+
const change = editor.onDidChangeModelContent(debounceChange);
1003+
debounceChange();
1004+
1005+
return () => {
1006+
ignore = true;
1007+
change.dispose();
1008+
monaco.editor.setModelMarkers(
1009+
editor.getModel()!,
1010+
"brick_next_yaml_lint",
1011+
[]
1012+
);
1013+
};
10191014
}, [language, links, markers, theme, workerId]);
10201015

10211016
return (

bricks/vs/src/code-editor/workers/lintYaml.spec.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,39 @@ import { lintYaml } from "./lintYaml";
44
describe("lintYaml", () => {
55
test("plain with no expressions", async () => {
66
const result = await lintYaml({
7-
id: "1",
87
source: "a",
98
});
109
expect(result).toEqual({
11-
id: "1",
1210
lintMarkers: [],
1311
lintDecorations: [],
1412
});
1513
});
1614

1715
test("plain with not string value", async () => {
1816
const result = await lintYaml({
19-
id: "1",
2017
source: "1",
2118
});
2219
expect(result).toEqual({
23-
id: "1",
2420
lintMarkers: [],
2521
lintDecorations: [],
2622
});
2723
});
2824

2925
test("plain with correct expression", async () => {
3026
const result = await lintYaml({
31-
id: "1",
3227
source: "<% a %>",
3328
});
3429
expect(result).toEqual({
35-
id: "1",
3630
lintMarkers: [],
3731
lintDecorations: [],
3832
});
3933
});
4034

4135
test("plain with incorrect expression", async () => {
4236
const result = await lintYaml({
43-
id: "1",
4437
source: "<% a. %>",
4538
});
4639
expect(result).toEqual({
47-
id: "1",
4840
lintMarkers: [
4941
{
5042
start: 5,
@@ -59,12 +51,10 @@ describe("lintYaml", () => {
5951

6052
test("block with CTX of links", async () => {
6153
const result = await lintYaml({
62-
id: "1",
6354
source: "|\n <%\n STATE.b,\n CTX.abc\n %>\n",
6455
links: ["CTX"],
6556
});
6657
expect(result).toEqual({
67-
id: "1",
6858
lintMarkers: [],
6959
lintDecorations: [
7060
{
@@ -80,7 +70,6 @@ describe("lintYaml", () => {
8070

8171
test("block with STATE of markers", async () => {
8272
const result = await lintYaml({
83-
id: "1",
8473
source: "|-\n <%\n STATE.b,\n CTX.abc\n %>\n",
8574
markers: [
8675
{
@@ -91,7 +80,6 @@ describe("lintYaml", () => {
9180
],
9281
});
9382
expect(result).toEqual({
94-
id: "1",
9583
lintMarkers: [
9684
{
9785
start: 12,
@@ -106,7 +94,6 @@ describe("lintYaml", () => {
10694

10795
test("single-quote with non-expressions", async () => {
10896
const result = await lintYaml({
109-
id: "1",
11097
source: "'CTX.a'",
11198
links: ["CTX"],
11299
markers: [
@@ -118,15 +105,13 @@ describe("lintYaml", () => {
118105
],
119106
});
120107
expect(result).toEqual({
121-
id: "1",
122108
lintMarkers: [],
123109
lintDecorations: [],
124110
});
125111
});
126112

127113
test("single-quote with CTX", async () => {
128114
const result = await lintYaml({
129-
id: "1",
130115
source: "'<% CTX(), CTX.b, STATE.c %>'",
131116
links: ["CTX"],
132117
markers: [
@@ -138,7 +123,6 @@ describe("lintYaml", () => {
138123
],
139124
});
140125
expect(result).toEqual({
141-
id: "1",
142126
lintMarkers: [
143127
{
144128
start: 18,
@@ -161,12 +145,10 @@ describe("lintYaml", () => {
161145

162146
test("single-quote incorrect expression", async () => {
163147
const result = await lintYaml({
164-
id: "1",
165148
source: "'<% CTX[''a''], CTX'' %>'",
166149
links: ["CTX"],
167150
});
168151
expect(result).toEqual({
169-
id: "1",
170152
lintMarkers: [
171153
{
172154
start: 19,
@@ -181,12 +163,10 @@ describe("lintYaml", () => {
181163

182164
test("double-quote with CTX", async () => {
183165
const result = await lintYaml({
184-
id: "1",
185166
source: '"<% CTX[\\"a\\"], CTX.b, CTX[\\"c\\"] %>"',
186167
links: ["CTX"],
187168
});
188169
expect(result).toEqual({
189-
id: "1",
190170
lintMarkers: [],
191171
lintDecorations: [
192172
{
@@ -202,12 +182,10 @@ describe("lintYaml", () => {
202182

203183
test("double-quote incorrect expression", async () => {
204184
const result = await lintYaml({
205-
id: "1",
206185
source: '"<% CTX[\\"a\\"], CTX) %>"',
207186
links: ["CTX"],
208187
});
209188
expect(result).toEqual({
210-
id: "1",
211189
lintMarkers: [
212190
{
213191
start: 19,

0 commit comments

Comments
 (0)