-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
worker-codegen.ts
206 lines (202 loc) · 6.67 KB
/
worker-codegen.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
204
205
206
import { TextlintConfigDescriptor } from "@textlint/config-loader";
import type { TextlintFixResult, TextlintResult } from "@textlint/types";
import { TextlintScriptMetadata } from "@textlint/script-parser";
export type MessageId = string;
export type TextlintWorkerCommandLint = {
id?: MessageId;
command: "lint";
text: string;
ext: string;
ruleId?: string;
};
export type TextlintWorkerCommandFix = {
id?: MessageId;
command: "fix";
text: string;
ext: string;
ruleId?: string;
};
export type TextlintWorkerCommandMergeConfig = {
command: "merge-config";
textlintrc: string;
};
export type TextlintWorkerCommand =
| TextlintWorkerCommandLint
| TextlintWorkerCommandFix
| TextlintWorkerCommandMergeConfig;
export type TextlintWorkerCommandResponseInit = {
command: "init";
metadata: TextlintScriptMetadata;
};
export type TextlintWorkerCommandResponseLint = {
id: MessageId | undefined;
command: "lint:result";
result: TextlintResult;
};
export type TextlintWorkerCommandResponseFix = {
id: MessageId | undefined;
command: "fix:result";
result: TextlintFixResult;
};
export type TextlintWorkerCommandResponseError = {
id?: MessageId | undefined;
command: "error";
error: Error;
};
export type TextlintWorkerCommandResponse =
| TextlintWorkerCommandResponseInit
| TextlintWorkerCommandResponseLint
| TextlintWorkerCommandResponseFix
| TextlintWorkerCommandResponseError;
export const generateCode = async (config: TextlintConfigDescriptor) => {
// macro replacement
// !__moduleInterop(require('${rule.moduleName}').rules['${ruleName}'])__! -> moduleInterop(require('${rule.moduleName}').rules['${ruleName}'])
const stringify = (item: any[]): string => {
// unwrap code
return JSON.stringify(item, null, 4).replace(/"!__(.*)__!"/g, "$1");
};
return `// Generated webworker code by textlint-script-compiler
import { TextlintKernel } from "@textlint/kernel";
import { moduleInterop } from "@textlint/module-interop";
import { parseOptionsFromConfig } from "@textlint/config-partial-parser"
const kernel = new TextlintKernel();
const rules = ${stringify(
config.rules.flatMap((rule) => {
return rule.type === "Rule"
? {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.filePath}'))__!`,
options: rule.options
}
: {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.moduleName}').rules['${rule.ruleKey}'])__!`,
options: rule.options
};
})
)};
const filterRules = ${stringify(
config.filterRules.map((rule) => {
return {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.moduleName}'))__!`,
options: rule.options
};
})
)};
const plugins = ${stringify(
config.plugins.map((plugin) => {
return {
pluginId: plugin.pluginId,
plugin: `!__moduleInterop(require('${plugin.moduleName}'))__!`,
options: plugin.options
};
})
)};
const config = {
rules: rules,
filterRules: filterRules,
plugins: plugins
};
// merge config
const assignConfig = (textlintrc) => {
const userDefinedConfig = parseOptionsFromConfig(textlintrc);
if (userDefinedConfig.rules) {
config.rules = config.rules.map(rule => {
const override = userDefinedConfig.rules.find(o => o.ruleId === rule.ruleId);
return { ...rule, ...override };
});
}
if (userDefinedConfig.filterRules) {
config.filterRules = config.filterRules.map(rule => {
const override = userDefinedConfig.filterRules.find(o => o.ruleId === rule.ruleId);
return { ...rule, ...override };
});
}
if (userDefinedConfig.plugins) {
config.plugins = config.plugins.map(rule => {
const override = userDefinedConfig.plugins.find(o => o.pluginId === rule.pluginId);
return { ...rule, ...override };
});
}
};
self.addEventListener('error', (event) => {
self.postMessage({
command: "error",
// wrapping any type error with Error
error: new Error("unexpected error", {
cause: event.error
})
})
});
self.addEventListener('unhandledrejection', (event) => {
self.postMessage({
command: "error",
// wrapping any type error with Error
error: new Error("unexpected unhandled promise rejection", {
cause: event.reason
})
})
});
self.addEventListener('message', (event) => {
const data = event.data;
const rules = data.ruleId === undefined
? config.rules
: config.rules.filter(rule => rule.ruleId === data.ruleId);
switch (data.command) {
case "merge-config":
return assignConfig(data.textlintrc);
case "lint":
return kernel.lintText(data.text, {
rules: rules,
filterRules: config.filterRules,
plugins: config.plugins,
filePath: "/path/to/README" + data.ext,
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "lint:result",
result
});
}).catch(error => {
return self.postMessage({
id: data.id,
command: "error",
// wrapping any type error with Error
error: new Error("failed to lint text", {
cause: error
})
})
});
case "fix":
return kernel.fixText(data.text, {
rules: rules,
filterRules: config.filterRules,
plugins: config.plugins,
filePath: "/path/to/README" + data.ext,
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "fix:result",
result
});
}).catch(error => {
return self.postMessage({
id: data.id,
command: "error",
error
})
});
default:
console.log("Unknown command: " + data.command);
}
});
// ====
self.postMessage({
command: "init",
metadata: process.env.TEXTLINT_SCRIPT_METADATA
});
`;
};