@rsdoctor/utils 0.0.0-next-20240620044732

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -0
  3. package/dist/cjs/build/envinfo.js +109 -0
  4. package/dist/cjs/build/file/cache.js +67 -0
  5. package/dist/cjs/build/file/index.js +44 -0
  6. package/dist/cjs/build/file/sharding.js +84 -0
  7. package/dist/cjs/build/index.js +50 -0
  8. package/dist/cjs/build/json.js +52 -0
  9. package/dist/cjs/build/process.js +49 -0
  10. package/dist/cjs/build/server.js +127 -0
  11. package/dist/cjs/common/alerts.js +49 -0
  12. package/dist/cjs/common/algorithm.js +60 -0
  13. package/dist/cjs/common/bundle.js +56 -0
  14. package/dist/cjs/common/crypto.js +53 -0
  15. package/dist/cjs/common/data/index.js +299 -0
  16. package/dist/cjs/common/graph/assets.js +322 -0
  17. package/dist/cjs/common/graph/chunk.js +67 -0
  18. package/dist/cjs/common/graph/dependency.js +40 -0
  19. package/dist/cjs/common/graph/entrypoints.js +30 -0
  20. package/dist/cjs/common/graph/index.js +30 -0
  21. package/dist/cjs/common/graph/modules.js +98 -0
  22. package/dist/cjs/common/index.js +80 -0
  23. package/dist/cjs/common/loader.js +261 -0
  24. package/dist/cjs/common/manifest.js +72 -0
  25. package/dist/cjs/common/package.js +97 -0
  26. package/dist/cjs/common/plugin.js +86 -0
  27. package/dist/cjs/common/resolver.js +82 -0
  28. package/dist/cjs/common/rspack.js +37 -0
  29. package/dist/cjs/common/summary.js +34 -0
  30. package/dist/cjs/common/time.js +91 -0
  31. package/dist/cjs/common/url.js +46 -0
  32. package/dist/cjs/error/error.js +236 -0
  33. package/dist/cjs/error/index.js +31 -0
  34. package/dist/cjs/error/transform.js +175 -0
  35. package/dist/cjs/error/utils.js +66 -0
  36. package/dist/cjs/index.js +41 -0
  37. package/dist/cjs/logger.js +51 -0
  38. package/dist/cjs/rule-utils/document/document.js +91 -0
  39. package/dist/cjs/rule-utils/document/index.js +26 -0
  40. package/dist/cjs/rule-utils/document/server.js +42 -0
  41. package/dist/cjs/rule-utils/document/types.js +16 -0
  42. package/dist/cjs/rule-utils/index.js +24 -0
  43. package/dist/cjs/rule-utils/parser/asserts.js +156 -0
  44. package/dist/cjs/rule-utils/parser/index.js +28 -0
  45. package/dist/cjs/rule-utils/parser/parser.js +92 -0
  46. package/dist/cjs/rule-utils/parser/types.js +33 -0
  47. package/dist/cjs/rule-utils/parser/utils.js +197 -0
  48. package/dist/esm/build/envinfo.js +75 -0
  49. package/dist/esm/build/file/cache.js +30 -0
  50. package/dist/esm/build/file/index.js +7 -0
  51. package/dist/esm/build/file/sharding.js +50 -0
  52. package/dist/esm/build/index.js +12 -0
  53. package/dist/esm/build/json.js +28 -0
  54. package/dist/esm/build/process.js +24 -0
  55. package/dist/esm/build/server.js +88 -0
  56. package/dist/esm/common/alerts.js +25 -0
  57. package/dist/esm/common/algorithm.js +33 -0
  58. package/dist/esm/common/bundle.js +30 -0
  59. package/dist/esm/common/crypto.js +28 -0
  60. package/dist/esm/common/data/index.js +265 -0
  61. package/dist/esm/common/graph/assets.js +286 -0
  62. package/dist/esm/common/graph/chunk.js +38 -0
  63. package/dist/esm/common/graph/dependency.js +14 -0
  64. package/dist/esm/common/graph/entrypoints.js +6 -0
  65. package/dist/esm/common/graph/index.js +5 -0
  66. package/dist/esm/common/graph/modules.js +70 -0
  67. package/dist/esm/common/index.js +32 -0
  68. package/dist/esm/common/loader.js +225 -0
  69. package/dist/esm/common/manifest.js +46 -0
  70. package/dist/esm/common/package.js +71 -0
  71. package/dist/esm/common/plugin.js +59 -0
  72. package/dist/esm/common/resolver.js +54 -0
  73. package/dist/esm/common/rspack.js +12 -0
  74. package/dist/esm/common/summary.js +10 -0
  75. package/dist/esm/common/time.js +64 -0
  76. package/dist/esm/common/url.js +20 -0
  77. package/dist/esm/error/error.js +202 -0
  78. package/dist/esm/error/index.js +5 -0
  79. package/dist/esm/error/transform.js +141 -0
  80. package/dist/esm/error/utils.js +40 -0
  81. package/dist/esm/index.js +6 -0
  82. package/dist/esm/logger.js +15 -0
  83. package/dist/esm/rule-utils/document/document.js +67 -0
  84. package/dist/esm/rule-utils/document/index.js +3 -0
  85. package/dist/esm/rule-utils/document/server.js +17 -0
  86. package/dist/esm/rule-utils/document/types.js +0 -0
  87. package/dist/esm/rule-utils/index.js +2 -0
  88. package/dist/esm/rule-utils/parser/asserts.js +132 -0
  89. package/dist/esm/rule-utils/parser/index.js +4 -0
  90. package/dist/esm/rule-utils/parser/parser.js +58 -0
  91. package/dist/esm/rule-utils/parser/types.js +9 -0
  92. package/dist/esm/rule-utils/parser/utils.js +164 -0
  93. package/dist/type/build/envinfo.d.ts +14 -0
  94. package/dist/type/build/envinfo.d.ts.map +1 -0
  95. package/dist/type/build/file/cache.d.ts +6 -0
  96. package/dist/type/build/file/cache.d.ts.map +1 -0
  97. package/dist/type/build/file/index.d.ts +4 -0
  98. package/dist/type/build/file/index.d.ts.map +1 -0
  99. package/dist/type/build/file/sharding.d.ts +23 -0
  100. package/dist/type/build/file/sharding.d.ts.map +1 -0
  101. package/dist/type/build/index.d.ts +6 -0
  102. package/dist/type/build/index.d.ts.map +1 -0
  103. package/dist/type/build/json.d.ts +2 -0
  104. package/dist/type/build/json.d.ts.map +1 -0
  105. package/dist/type/build/process.d.ts +4 -0
  106. package/dist/type/build/process.d.ts.map +1 -0
  107. package/dist/type/build/server.d.ts +16 -0
  108. package/dist/type/build/server.d.ts.map +1 -0
  109. package/dist/type/common/alerts.d.ts +3 -0
  110. package/dist/type/common/alerts.d.ts.map +1 -0
  111. package/dist/type/common/algorithm.d.ts +5 -0
  112. package/dist/type/common/algorithm.d.ts.map +1 -0
  113. package/dist/type/common/bundle.d.ts +4 -0
  114. package/dist/type/common/bundle.d.ts.map +1 -0
  115. package/dist/type/common/crypto.d.ts +3 -0
  116. package/dist/type/common/crypto.d.ts.map +1 -0
  117. package/dist/type/common/data/index.d.ts +11 -0
  118. package/dist/type/common/data/index.d.ts.map +1 -0
  119. package/dist/type/common/graph/assets.d.ts +53 -0
  120. package/dist/type/common/graph/assets.d.ts.map +1 -0
  121. package/dist/type/common/graph/chunk.d.ts +8 -0
  122. package/dist/type/common/graph/chunk.d.ts.map +1 -0
  123. package/dist/type/common/graph/dependency.d.ts +5 -0
  124. package/dist/type/common/graph/dependency.d.ts.map +1 -0
  125. package/dist/type/common/graph/entrypoints.d.ts +3 -0
  126. package/dist/type/common/graph/entrypoints.d.ts.map +1 -0
  127. package/dist/type/common/graph/index.d.ts +6 -0
  128. package/dist/type/common/graph/index.d.ts.map +1 -0
  129. package/dist/type/common/graph/modules.d.ts +10 -0
  130. package/dist/type/common/graph/modules.d.ts.map +1 -0
  131. package/dist/type/common/index.d.ts +16 -0
  132. package/dist/type/common/index.d.ts.map +1 -0
  133. package/dist/type/common/loader.d.ts +19 -0
  134. package/dist/type/common/loader.d.ts.map +1 -0
  135. package/dist/type/common/manifest.d.ts +5 -0
  136. package/dist/type/common/manifest.d.ts.map +1 -0
  137. package/dist/type/common/package.d.ts +8 -0
  138. package/dist/type/common/package.d.ts.map +1 -0
  139. package/dist/type/common/plugin.d.ts +6 -0
  140. package/dist/type/common/plugin.d.ts.map +1 -0
  141. package/dist/type/common/resolver.d.ts +7 -0
  142. package/dist/type/common/resolver.d.ts.map +1 -0
  143. package/dist/type/common/rspack.d.ts +8 -0
  144. package/dist/type/common/rspack.d.ts.map +1 -0
  145. package/dist/type/common/summary.d.ts +7 -0
  146. package/dist/type/common/summary.d.ts.map +1 -0
  147. package/dist/type/common/time.d.ts +5 -0
  148. package/dist/type/common/time.d.ts.map +1 -0
  149. package/dist/type/common/url.d.ts +4 -0
  150. package/dist/type/common/url.d.ts.map +1 -0
  151. package/dist/type/error/error.d.ts +36 -0
  152. package/dist/type/error/error.d.ts.map +1 -0
  153. package/dist/type/error/index.d.ts +3 -0
  154. package/dist/type/error/index.d.ts.map +1 -0
  155. package/dist/type/error/transform.d.ts +4 -0
  156. package/dist/type/error/transform.d.ts.map +1 -0
  157. package/dist/type/error/utils.d.ts +5 -0
  158. package/dist/type/error/utils.d.ts.map +1 -0
  159. package/dist/type/index.d.ts +3 -0
  160. package/dist/type/index.d.ts.map +1 -0
  161. package/dist/type/logger.d.ts +8 -0
  162. package/dist/type/logger.d.ts.map +1 -0
  163. package/dist/type/rule-utils/document/document.d.ts +17 -0
  164. package/dist/type/rule-utils/document/document.d.ts.map +1 -0
  165. package/dist/type/rule-utils/document/index.d.ts +4 -0
  166. package/dist/type/rule-utils/document/index.d.ts.map +1 -0
  167. package/dist/type/rule-utils/document/server.d.ts +5 -0
  168. package/dist/type/rule-utils/document/server.d.ts.map +1 -0
  169. package/dist/type/rule-utils/document/types.d.ts +32 -0
  170. package/dist/type/rule-utils/document/types.d.ts.map +1 -0
  171. package/dist/type/rule-utils/index.d.ts +3 -0
  172. package/dist/type/rule-utils/index.d.ts.map +1 -0
  173. package/dist/type/rule-utils/parser/asserts.d.ts +82 -0
  174. package/dist/type/rule-utils/parser/asserts.d.ts.map +1 -0
  175. package/dist/type/rule-utils/parser/index.d.ts +5 -0
  176. package/dist/type/rule-utils/parser/index.d.ts.map +1 -0
  177. package/dist/type/rule-utils/parser/parser.d.ts +119 -0
  178. package/dist/type/rule-utils/parser/parser.d.ts.map +1 -0
  179. package/dist/type/rule-utils/parser/types.d.ts +31 -0
  180. package/dist/type/rule-utils/parser/types.d.ts.map +1 -0
  181. package/dist/type/rule-utils/parser/utils.d.ts +31 -0
  182. package/dist/type/rule-utils/parser/utils.d.ts.map +1 -0
  183. package/package.json +111 -0
@@ -0,0 +1,202 @@
1
+ import { codeFrameColumns } from "@babel/code-frame";
2
+ import { Err } from "@rsdoctor/types";
3
+ import { Instance } from "chalk";
4
+ import deepEql from "deep-eql";
5
+ import { isNil } from "lodash";
6
+ import stripAnsi from "strip-ansi";
7
+ import { transform } from "./transform";
8
+ import { insertSpace, toLevel } from "./utils";
9
+ let id = 1;
10
+ class DevToolError extends Error {
11
+ constructor(title, message, opts) {
12
+ super(message);
13
+ this._controller = {
14
+ noStack: true,
15
+ noColor: false
16
+ };
17
+ this.id = id++;
18
+ this.title = title;
19
+ this.code = opts?.code;
20
+ this.hint = opts?.hint;
21
+ this.stack = opts?.stack;
22
+ this.detail = opts?.detail;
23
+ this.fixData = opts?.fixData;
24
+ this.category = opts?.category;
25
+ this.referenceUrl = opts?.referenceUrl;
26
+ this._level = opts?.level ? toLevel(opts.level) : Err.ErrorLevel.Error;
27
+ this._codeFrame = opts?.codeFrame;
28
+ this.setControllerOption(opts?.controller ?? {});
29
+ }
30
+ static from(err, opt) {
31
+ if (err instanceof DevToolError) {
32
+ return err;
33
+ }
34
+ return transform(err, opt);
35
+ }
36
+ get level() {
37
+ return Err.ErrorLevel[this._level];
38
+ }
39
+ get path() {
40
+ return this._codeFrame?.filePath;
41
+ }
42
+ set path(file) {
43
+ if (!file) {
44
+ return;
45
+ }
46
+ if (this._codeFrame) {
47
+ this._codeFrame.filePath = file;
48
+ return;
49
+ }
50
+ this._codeFrame = {
51
+ filePath: file
52
+ };
53
+ }
54
+ get codeFrame() {
55
+ return this._codeFrame;
56
+ }
57
+ printCodeFrame(print) {
58
+ const msgs = [];
59
+ const { _codeFrame: codeFrameOpt, _controller: controller } = this;
60
+ if (!codeFrameOpt) {
61
+ return msgs;
62
+ }
63
+ if ("start" in codeFrameOpt && codeFrameOpt.start) {
64
+ const { filePath, start } = codeFrameOpt;
65
+ msgs.push(
66
+ `
67
+ ${print.red(print.bold("File: "))}${print.bold(filePath)}:${start.line}${start.column ? `:${start.column}` : ""}`
68
+ );
69
+ if ("fileContent" in codeFrameOpt) {
70
+ const { end, fileContent } = codeFrameOpt;
71
+ msgs.push(
72
+ codeFrameColumns(
73
+ fileContent,
74
+ {
75
+ start,
76
+ end
77
+ },
78
+ {
79
+ highlightCode: !controller.noColor
80
+ }
81
+ )
82
+ );
83
+ } else if ("lineText" in codeFrameOpt) {
84
+ const { length, lineText } = codeFrameOpt;
85
+ let lineCodeFrame = codeFrameColumns(
86
+ lineText,
87
+ {
88
+ start: {
89
+ line: 1,
90
+ column: start.column
91
+ },
92
+ end: {
93
+ line: 1,
94
+ column: isNil(start.column) || isNil(length) ? void 0 : start.column + length
95
+ }
96
+ },
97
+ {
98
+ highlightCode: !controller.noColor
99
+ }
100
+ );
101
+ if (start.line > 1) {
102
+ lineCodeFrame = lineCodeFrame.replace(" 1 |", ` ${start.line} |`);
103
+ if (start.line >= 10) {
104
+ lineCodeFrame = insertSpace(
105
+ lineCodeFrame,
106
+ 2,
107
+ String(start.line).length - 1
108
+ );
109
+ }
110
+ }
111
+ msgs.push(lineCodeFrame);
112
+ }
113
+ } else {
114
+ msgs.push(
115
+ `
116
+ ${print.red(print.bold("File: "))}${print.bold(
117
+ codeFrameOpt.filePath
118
+ )}
119
+ `
120
+ );
121
+ }
122
+ return msgs;
123
+ }
124
+ toString() {
125
+ const msgs = [];
126
+ const {
127
+ code,
128
+ title,
129
+ message,
130
+ hint,
131
+ referenceUrl,
132
+ _controller: controller
133
+ } = this;
134
+ const print = controller.noColor ? new Instance({ level: 0 }) : new Instance({ level: 3 });
135
+ const mainColorPrint = this._level === Err.ErrorLevel.Error ? print.red : print.yellow;
136
+ const codeText = code ? `${mainColorPrint.blue(code)}:` : "";
137
+ msgs.push(
138
+ mainColorPrint.bold(
139
+ `[${codeText}${this.level}:${title.toUpperCase()}] `
140
+ ) + message
141
+ );
142
+ msgs.push(...this.printCodeFrame(print));
143
+ if (hint || referenceUrl) {
144
+ msgs.push("");
145
+ }
146
+ if (hint) {
147
+ msgs.push(` ${print.blue(`HINT: ${hint}`)}`);
148
+ }
149
+ if (referenceUrl) {
150
+ msgs.push(print.magenta.bold(` See: ${referenceUrl}`));
151
+ }
152
+ if (!controller.noStack && this.stack) {
153
+ msgs.push(print.red.bold(` Error Stack:
154
+ ${this.stack}
155
+ `));
156
+ }
157
+ return msgs.join("\n");
158
+ }
159
+ toData() {
160
+ return {
161
+ ...this.detail,
162
+ id: this.id,
163
+ category: this.category,
164
+ description: stripAnsi(this.detail?.description ?? this.message),
165
+ title: this.title.toUpperCase(),
166
+ code: this.code,
167
+ level: this.level.toLowerCase()
168
+ };
169
+ }
170
+ toError() {
171
+ const error = new Error();
172
+ error.message = this.toString();
173
+ error.name = this.name;
174
+ error.stack = this.stack;
175
+ return error;
176
+ }
177
+ /**
178
+ * for json stringify
179
+ */
180
+ toJSON() {
181
+ return {
182
+ message: this.toString(),
183
+ name: this.name,
184
+ stack: this.stack
185
+ };
186
+ }
187
+ setControllerOption(opt) {
188
+ this._controller = {
189
+ noStack: opt.noStack ?? this._controller.noStack ?? true,
190
+ noColor: opt.noColor ?? this._controller.noColor ?? false
191
+ };
192
+ }
193
+ setCodeFrame(opt) {
194
+ this._codeFrame = opt;
195
+ }
196
+ isSame(error) {
197
+ return this.code === error.code && this.message === error.message && this.hint === error.hint && this.level === error.level && this.title === error.title && this.referenceUrl === error.referenceUrl && this.code === error.code && deepEql(this.codeFrame, error.codeFrame);
198
+ }
199
+ }
200
+ export {
201
+ DevToolError
202
+ };
@@ -0,0 +1,5 @@
1
+ export * from "./error";
2
+ import { printErrors } from "./utils";
3
+ export {
4
+ printErrors
5
+ };
@@ -0,0 +1,141 @@
1
+ import stripAnsi from "strip-ansi";
2
+ import { Linter } from "@rsdoctor/types";
3
+ import { DevToolError } from "./error";
4
+ function isEsbuildError(err) {
5
+ return "pluginName" in err && "text" in err && "location" in err;
6
+ }
7
+ function isBabelError(err) {
8
+ return "code" in err && "reasonCode" in err;
9
+ }
10
+ function isDiagnosticError(err) {
11
+ return "severity" in err && "title" in err;
12
+ }
13
+ function parseBabelErrorMessage(input) {
14
+ const lines = stripAnsi(input).split("\n");
15
+ const filePath = lines[0].replace(/^([^:]+):.*/, "$1");
16
+ const message = lines[0].replace(/.*: (.*) \(\d+:\d+\)*/, "$1");
17
+ const lineText = lines.find((line) => line.startsWith("> "))?.replace(/> \d+ \| /, "") ?? "";
18
+ return {
19
+ message,
20
+ filePath,
21
+ lineText
22
+ };
23
+ }
24
+ function clearMessage(str) {
25
+ return stripAnsi(str).replace(/.*: (.*)\n\n[\s\S]*/g, "$1");
26
+ }
27
+ function clearStack(str) {
28
+ return str.slice(str.indexOf(" at")).replace(/\s*at(.*) \((.*)\)/g, "$1\n$2\n");
29
+ }
30
+ function transformEsbuildError(err, opt) {
31
+ if (isEsbuildError(err)) {
32
+ const errorCode = opt?.code ?? "ESBUILD_ERROR";
33
+ const speedyError = typeof err.detail === "object" ? DevToolError.from(err.detail) : new DevToolError(errorCode, clearMessage(err.text), {
34
+ ...opt,
35
+ hint: err.location?.suggestion,
36
+ codeFrame: {
37
+ filePath: err.text.split(":")[0]
38
+ }
39
+ });
40
+ if (err.location) {
41
+ speedyError.setCodeFrame({
42
+ filePath: err.location.file,
43
+ lineText: err.location.lineText,
44
+ length: err.location.length,
45
+ start: {
46
+ line: err.location.line,
47
+ column: err.location.column + 1
48
+ }
49
+ });
50
+ }
51
+ return speedyError;
52
+ }
53
+ }
54
+ function transformBabelError(err, opt) {
55
+ if (isBabelError(err)) {
56
+ const errorCode = opt?.code ?? err.code ?? "BABEL";
57
+ const title = err.reasonCode;
58
+ const errorParsed = parseBabelErrorMessage(err.message);
59
+ const speedyError = new DevToolError(title, errorParsed.message, {
60
+ ...opt,
61
+ code: errorCode,
62
+ stack: err.stack && clearStack(err.stack)
63
+ });
64
+ if (err.loc) {
65
+ speedyError.setCodeFrame({
66
+ ...errorParsed,
67
+ start: {
68
+ line: err.loc.line,
69
+ column: err.loc.column + 1
70
+ }
71
+ });
72
+ }
73
+ return speedyError;
74
+ }
75
+ }
76
+ function transformNormalError(err, opt) {
77
+ if (err instanceof Error) {
78
+ const stacks = [];
79
+ const filePath = stacks?.[0]?.getFileName?.();
80
+ return new DevToolError(err.name, clearMessage(err.message), {
81
+ ...opt,
82
+ codeFrame: filePath ? { filePath } : void 0,
83
+ stack: err.stack && clearStack(err.stack)
84
+ });
85
+ }
86
+ }
87
+ function transformErrorLike(err, opt) {
88
+ const isErrorLike = err && typeof err === "object" && err.message;
89
+ if (isErrorLike) {
90
+ const stacks = [];
91
+ const filePath = stacks?.[0]?.getFileName?.();
92
+ return new DevToolError(
93
+ err.name || "UNKNOWN_ERROR",
94
+ clearMessage(err.message),
95
+ {
96
+ ...opt,
97
+ codeFrame: filePath ? { filePath } : void 0,
98
+ stack: err.stack && clearStack(err.stack)
99
+ }
100
+ );
101
+ }
102
+ }
103
+ function transformDiagnostic(err, opt) {
104
+ if (isDiagnosticError(err)) {
105
+ return new DevToolError(err.title, err.message, {
106
+ ...err,
107
+ ...opt,
108
+ hint: err.suggestions?.description,
109
+ fixData: err.suggestions?.fixData,
110
+ codeFrame: err.document ? {
111
+ filePath: err.document.path,
112
+ fileContent: err.document.content,
113
+ start: err.document.range.start,
114
+ end: err.document.range.end
115
+ } : void 0,
116
+ level: Linter.Severity[err.severity]
117
+ });
118
+ }
119
+ }
120
+ function defaultError(err, opt) {
121
+ return new DevToolError("UNKNOWN_ERROR", JSON.stringify(err), opt);
122
+ }
123
+ function transform(err, opt) {
124
+ const transformers = [
125
+ transformEsbuildError,
126
+ transformBabelError,
127
+ transformDiagnostic,
128
+ transformNormalError,
129
+ transformErrorLike
130
+ ];
131
+ for (const fn of transformers) {
132
+ const result = fn(err, opt);
133
+ if (result) {
134
+ return result;
135
+ }
136
+ }
137
+ return defaultError(err, opt);
138
+ }
139
+ export {
140
+ transform
141
+ };
@@ -0,0 +1,40 @@
1
+ import { Err } from "@rsdoctor/types";
2
+ function toLevel(level) {
3
+ return Err.ErrorLevel[level];
4
+ }
5
+ function insertSpace(rawLines, line, width) {
6
+ const lines = rawLines.split("\n");
7
+ lines[line - 1] = Array(width).fill(" ").join("") + lines[line - 1];
8
+ return lines.join("\n");
9
+ }
10
+ function printErrors({ errors, warnings }, logLevel = "Error") {
11
+ const onlyError = logLevel === "Error";
12
+ if (logLevel === "Silent") {
13
+ return "";
14
+ }
15
+ if (onlyError && errors.length === 0 || !onlyError && errors.length === 0 && warnings.length === 0) {
16
+ return "";
17
+ }
18
+ const msgs = [];
19
+ if (onlyError) {
20
+ msgs.push(
21
+ `Build failed with ${errors.length} error:`,
22
+ ...errors.map((item) => item.toString()),
23
+ ""
24
+ );
25
+ } else {
26
+ const title = errors.length === 0 ? `Build succuss with ${warnings.length} warning:` : `Build failed with ${errors.length} error, ${warnings.length} warning:`;
27
+ msgs.push(
28
+ title,
29
+ ...errors.map((item) => item.toString()),
30
+ ...warnings.map((item) => item.toString()),
31
+ ""
32
+ );
33
+ }
34
+ return msgs.join("\n\n");
35
+ }
36
+ export {
37
+ insertSpace,
38
+ printErrors,
39
+ toLevel
40
+ };
@@ -0,0 +1,6 @@
1
+ import * as RuleUtils from "./rule-utils";
2
+ import * as Logger from "./logger";
3
+ export {
4
+ Logger,
5
+ RuleUtils
6
+ };
@@ -0,0 +1,15 @@
1
+ import chalk from "chalk";
2
+ import { logger } from "rslog";
3
+ import { Constants } from "@rsdoctor/types";
4
+ function debug(getMsg, prefix = "") {
5
+ if (!process.env[Constants.RsdoctorProcessEnvDebugKey]) {
6
+ return;
7
+ }
8
+ logger.level = "verbose";
9
+ logger.debug(`${prefix} ${getMsg()}`);
10
+ }
11
+ export {
12
+ chalk,
13
+ debug,
14
+ logger
15
+ };
@@ -0,0 +1,67 @@
1
+ import { LinesAndColumns } from "lines-and-columns";
2
+ import { isUndefined, isNumber } from "lodash";
3
+ class Document {
4
+ constructor(content) {
5
+ /** Actual document content. */
6
+ this._text = "";
7
+ this._text;
8
+ this._text = content;
9
+ this.createFinder();
10
+ }
11
+ /** Generate location search */
12
+ createFinder() {
13
+ const find = new LinesAndColumns(this._text);
14
+ this.positionAt = (offset) => {
15
+ if (offset >= this._text.length) {
16
+ offset = this._text.length - 1;
17
+ }
18
+ if (offset < 0) {
19
+ offset = 0;
20
+ }
21
+ const result = find.locationForIndex(offset);
22
+ if (!result) {
23
+ return;
24
+ }
25
+ return {
26
+ line: result.line + 1,
27
+ column: result.column
28
+ };
29
+ };
30
+ this.offsetAt = (position) => {
31
+ return find.indexForLocation({
32
+ line: position.line - 1,
33
+ column: position.column
34
+ }) ?? void 0;
35
+ };
36
+ }
37
+ getText(range) {
38
+ if (!range) {
39
+ return this._text;
40
+ }
41
+ const start = typeof range.start === "number" ? range.start : this.offsetAt(range.start);
42
+ const end = typeof range.end === "number" ? range.end : this.offsetAt(range.end);
43
+ if (isUndefined(start)) {
44
+ throw new Error(`Location ${JSON.stringify(start)} is illegal`);
45
+ }
46
+ if (isUndefined(end)) {
47
+ throw new Error(`Location ${JSON.stringify(end)} is illegal`);
48
+ }
49
+ return this._text.slice(start, end);
50
+ }
51
+ /** Edit document data */
52
+ edit(data) {
53
+ let { _text: content } = this;
54
+ const startOffset = isNumber(data.start) ? data.start : this.offsetAt(data.start);
55
+ const endOffset = isNumber(data.end) ? data.end : this.offsetAt(data.end);
56
+ if (isUndefined(startOffset) || isUndefined(endOffset)) {
57
+ return;
58
+ }
59
+ const startTxt = content.substring(0, startOffset);
60
+ const endTxt = content.substring(endOffset, content.length);
61
+ content = startTxt + data.newText + endTxt;
62
+ return content;
63
+ }
64
+ }
65
+ export {
66
+ Document
67
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./document";
2
+ export * from "./types";
3
+ export * from "./server";
@@ -0,0 +1,17 @@
1
+ import { Document } from "./document";
2
+ const store = /* @__PURE__ */ new Map();
3
+ function getDocument(content) {
4
+ if (store.has(content)) {
5
+ return store.get(content);
6
+ }
7
+ const doc = new Document(content);
8
+ store.set(content, doc);
9
+ return doc;
10
+ }
11
+ function clearDocument() {
12
+ store.clear();
13
+ }
14
+ export {
15
+ clearDocument,
16
+ getDocument
17
+ };
File without changes
@@ -0,0 +1,2 @@
1
+ export * from "./document";
2
+ export * from "./parser";
@@ -0,0 +1,132 @@
1
+ import { isObject } from "lodash";
2
+ function isSyntaxNode(node) {
3
+ return isObject(node) && "type" in node;
4
+ }
5
+ function assertCreator(type) {
6
+ return (node) => {
7
+ return isSyntaxNode(node) && node.type === type;
8
+ };
9
+ }
10
+ const asserts = {
11
+ isProgram: assertCreator("Program"),
12
+ isEmptyStatement: assertCreator("EmptyStatement"),
13
+ isBlockStatement: assertCreator("BlockStatement"),
14
+ isStaticBlock: assertCreator("StaticBlock"),
15
+ isExpressionStatement: assertCreator(
16
+ "ExpressionStatement"
17
+ ),
18
+ isIfStatement: assertCreator("IfStatement"),
19
+ isLabeledStatement: assertCreator("LabeledStatement"),
20
+ isBreakStatement: assertCreator("BreakStatement"),
21
+ isContinueStatement: assertCreator("ContinueStatement"),
22
+ isWithStatement: assertCreator("WithStatement"),
23
+ isSwitchStatement: assertCreator("SwitchStatement"),
24
+ isReturnStatement: assertCreator("ReturnStatement"),
25
+ isThrowStatement: assertCreator("ThrowStatement"),
26
+ isTryStatement: assertCreator("TryStatement"),
27
+ isWhileStatement: assertCreator("WhileStatement"),
28
+ isDoWhileStatement: assertCreator("DoWhileStatement"),
29
+ isForStatement: assertCreator("ForStatement"),
30
+ isForInStatement: assertCreator("ForInStatement"),
31
+ isForOfStatement: assertCreator("ForOfStatement"),
32
+ isDebuggerStatement: assertCreator("DebuggerStatement"),
33
+ isFunctionDeclaration: assertCreator(
34
+ "FunctionDeclaration"
35
+ ),
36
+ isVariableDeclaration: assertCreator(
37
+ "VariableDeclaration"
38
+ ),
39
+ isVariableDeclarator: assertCreator("VariableDeclarator"),
40
+ isChainExpression: assertCreator("ChainExpression"),
41
+ isThisExpression: assertCreator("ThisExpression"),
42
+ isArrayExpression: assertCreator("ArrayExpression"),
43
+ isObjectExpression: assertCreator("ObjectExpression"),
44
+ isPrivateIdentifier: assertCreator("PrivateIdentifier"),
45
+ isProperty: assertCreator("Property"),
46
+ isPropertyDefinition: assertCreator("PropertyDefinition"),
47
+ isFunctionExpression: assertCreator("FunctionExpression"),
48
+ isSequenceExpression: assertCreator("SequenceExpression"),
49
+ isUnaryExpression: assertCreator("UnaryExpression"),
50
+ isBinaryExpression: assertCreator("BinaryExpression"),
51
+ isAssignmentExpression: assertCreator(
52
+ "AssignmentExpression"
53
+ ),
54
+ isUpdateExpression: assertCreator("UpdateExpression"),
55
+ isLogicalExpression: assertCreator("LogicalExpression"),
56
+ isConditionalExpression: assertCreator(
57
+ "ConditionalExpression"
58
+ ),
59
+ isNewExpression: assertCreator("NewExpression"),
60
+ isSwitchCase: assertCreator("SwitchCase"),
61
+ isCatchClause: assertCreator("CatchClause"),
62
+ isIdentifier: assertCreator("Identifier"),
63
+ isLiteral: assertCreator("Literal"),
64
+ isSuper: assertCreator("Super"),
65
+ isSpreadElement: assertCreator("SpreadElement"),
66
+ isArrowFunctionExpression: assertCreator(
67
+ "ArrowFunctionExpression"
68
+ ),
69
+ isYieldExpression: assertCreator("YieldExpression"),
70
+ isTemplateLiteral: assertCreator("TemplateLiteral"),
71
+ isTaggedTemplateExpression: assertCreator(
72
+ "TaggedTemplateExpression"
73
+ ),
74
+ isTemplateElement: assertCreator("TemplateElement"),
75
+ isObjectPattern: assertCreator("ObjectPattern"),
76
+ isArrayPattern: assertCreator("ArrayPattern"),
77
+ isRestElement: assertCreator("RestElement"),
78
+ isAssignmentPattern: assertCreator("AssignmentPattern"),
79
+ isClassBody: assertCreator("ClassBody"),
80
+ isClassDeclaration: assertCreator("ClassDeclaration"),
81
+ isClassExpression: assertCreator("ClassExpression"),
82
+ isMetaProperty: assertCreator("MetaProperty"),
83
+ isImportDeclaration: assertCreator("ImportDeclaration"),
84
+ isImportSpecifier: assertCreator("ImportSpecifier"),
85
+ isImportExpression: assertCreator("ImportExpression"),
86
+ isImportDefaultSpecifier: assertCreator(
87
+ "ImportDefaultSpecifier"
88
+ ),
89
+ isImportNamespaceSpecifier: assertCreator(
90
+ "ImportNamespaceSpecifier"
91
+ ),
92
+ isExportNamedDeclaration: assertCreator(
93
+ "ExportNamedDeclaration"
94
+ ),
95
+ isExportSpecifier: assertCreator("ExportSpecifier"),
96
+ isExportDefaultDeclaration: assertCreator(
97
+ "ExportDefaultDeclaration"
98
+ ),
99
+ isExportAllDeclaration: assertCreator(
100
+ "ExportAllDeclaration"
101
+ ),
102
+ isAwaitExpression: assertCreator("AwaitExpression"),
103
+ isMethodDefinition: assertCreator("MethodDefinition"),
104
+ isMemberExpression: assertCreator("MemberExpression"),
105
+ isComment(node) {
106
+ return isSyntaxNode(node) && (node.type === "Line" || node.type === "Block");
107
+ },
108
+ isDirective(node) {
109
+ return asserts.isExpressionStatement(node) && "directive" in node;
110
+ },
111
+ isSimpleCallExpression(node) {
112
+ return isSyntaxNode(node) && node.type === "CallExpression";
113
+ },
114
+ isAssignmentProperty(node) {
115
+ return asserts.isProperty(node) && node.kind === "init";
116
+ },
117
+ isSimpleLiteral(node) {
118
+ return asserts.isLiteral(node) && !asserts.isRegExpLiteral(node) && !asserts.isBigIntLiteral(node);
119
+ },
120
+ isRegExpLiteral(node) {
121
+ return asserts.isLiteral(node) && "regex" in node;
122
+ },
123
+ isBigIntLiteral(node) {
124
+ return asserts.isLiteral(node) && "bigint" in node;
125
+ },
126
+ isExportStatement(node) {
127
+ return asserts.isExportAllDeclaration(node) || asserts.isExportDefaultDeclaration(node) || asserts.isExportNamedDeclaration(node);
128
+ }
129
+ };
130
+ export {
131
+ asserts
132
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./asserts";
2
+ export * from "./parser";
3
+ export * from "./utils";
4
+ export * from "./types";