@visulima/error 3.0.7 → 3.1.0
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.
- package/CHANGELOG.md +7 -0
- package/README.md +81 -0
- package/dist/chunk-EGCQFTZA.js +10 -0
- package/dist/chunk-EGCQFTZA.js.map +1 -0
- package/dist/code-frame/index.d.ts +7 -6
- package/dist/code-frame/index.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-EIN7B3HV.js +0 -10
- package/dist/chunk-EIN7B3HV.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## @visulima/error [3.1.0](https://github.com/visulima/visulima/compare/@visulima/error@3.0.7...@visulima/error@3.1.0) (2024-03-09)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **error:** added new tabWidth option to disable the tab transformer ([a115918](https://github.com/visulima/visulima/commit/a11591825f841c9ab28ebadd85d20633c5cd9e50))
|
|
7
|
+
|
|
1
8
|
## @visulima/error [3.0.7](https://github.com/visulima/visulima/compare/@visulima/error@3.0.6...@visulima/error@3.0.7) (2024-03-09)
|
|
2
9
|
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -97,6 +97,11 @@ console.log(causes);
|
|
|
97
97
|
|
|
98
98
|
## Pretty Code Frame
|
|
99
99
|
|
|
100
|
+
Display a pretty code frame with the error location.
|
|
101
|
+
|
|
102
|
+
> Note: Tabs can be used in the source code, codeFrame transforms them to spaces based on the tabWidth option.
|
|
103
|
+
> The default tabWidth is 4, to disable the transformation, set tabWidth to false.
|
|
104
|
+
|
|
100
105
|
```ts
|
|
101
106
|
import { codeFrame } from "@visulima/error";
|
|
102
107
|
|
|
@@ -111,6 +116,82 @@ console.log(frame);
|
|
|
111
116
|
// | ^
|
|
112
117
|
```
|
|
113
118
|
|
|
119
|
+
### API
|
|
120
|
+
|
|
121
|
+
#### source
|
|
122
|
+
|
|
123
|
+
Type: `string`
|
|
124
|
+
|
|
125
|
+
The source code to frame.
|
|
126
|
+
|
|
127
|
+
#### location
|
|
128
|
+
|
|
129
|
+
Type: `object`
|
|
130
|
+
|
|
131
|
+
The location of the error.
|
|
132
|
+
|
|
133
|
+
##### location.start
|
|
134
|
+
|
|
135
|
+
Type: `object`
|
|
136
|
+
|
|
137
|
+
The location of the start of the frame.
|
|
138
|
+
|
|
139
|
+
##### location.start.line
|
|
140
|
+
|
|
141
|
+
Type: `number`
|
|
142
|
+
|
|
143
|
+
The line number of the error.
|
|
144
|
+
|
|
145
|
+
##### location.start.column
|
|
146
|
+
|
|
147
|
+
Type: `number`
|
|
148
|
+
|
|
149
|
+
The column number of the error.
|
|
150
|
+
|
|
151
|
+
##### location.end
|
|
152
|
+
|
|
153
|
+
Type: `object`
|
|
154
|
+
|
|
155
|
+
The location of the end of the frame.
|
|
156
|
+
|
|
157
|
+
##### location.end.line
|
|
158
|
+
|
|
159
|
+
Type: `number`
|
|
160
|
+
|
|
161
|
+
The line number of the error.
|
|
162
|
+
|
|
163
|
+
##### location.end.column
|
|
164
|
+
|
|
165
|
+
Type: `number`
|
|
166
|
+
|
|
167
|
+
The column number of the error.
|
|
168
|
+
|
|
169
|
+
#### options
|
|
170
|
+
|
|
171
|
+
Type: `object`
|
|
172
|
+
|
|
173
|
+
##### options.linesAbove
|
|
174
|
+
|
|
175
|
+
Type: `number`
|
|
176
|
+
|
|
177
|
+
Default: `2`
|
|
178
|
+
|
|
179
|
+
The number of lines to show above the error.
|
|
180
|
+
|
|
181
|
+
##### options.linesBelow
|
|
182
|
+
|
|
183
|
+
Type: `number`
|
|
184
|
+
|
|
185
|
+
Default: `3`
|
|
186
|
+
|
|
187
|
+
The number of lines to show below the error.
|
|
188
|
+
|
|
189
|
+
##### options.tabWidth
|
|
190
|
+
|
|
191
|
+
Type: `number` | `false`
|
|
192
|
+
|
|
193
|
+
Default: `4`
|
|
194
|
+
|
|
114
195
|
## Stacktrace
|
|
115
196
|
|
|
116
197
|
> Browser older than 6 years are not supported.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
var A=s=>s.replaceAll(/\r\n|\r(?!\n)|\n/gu,`
|
|
2
|
+
`),x=A;var O=globalThis.process||Object.create(null),y={versions:{}},v=new Proxy(O,{get(s,r){if(r in s)return s[r];if(r in y)return y[r]}}),F=v;var S=(s,r,h,t)=>{let g={column:0,line:-1,...s.start},m={...g,...s.end},i=g.line,n=g.column,l=m.line,d=m.column,u=Math.max(i-(h+1),0),f=Math.min(r.length,l+t);i===-1&&(u=0),l===-1&&(f=r.length);let p=l-i,o={};if(p)for(let e=0;e<=p;e++){let a=e+i;if(!n)o[a]=!0;else if(e===0){let c=r[a-1]?.length;o[a]=[n,(c??0)-n+1];}else if(e===p)o[a]=[0,d];else {let c=r[a-e]?.length;o[a]=[0,c];}}else n===d?o[i]=n?[n,0]:!0:o[i]=[n,(d??0)-(n??0)];return {end:f,markerLines:o,start:u}},$=S;var W=F.platform==="win32"&&!F.env?.WT_SESSION?">":"\u276F",q=(s,r,h)=>{let t={color:{gutter:e=>e,marker:e=>e,message:e=>e,...h?.color},linesAbove:2,linesBelow:3,showGutter:!0,showLineNumbers:!0,tabWidth:4,...h},g=r.start&&typeof r.start.column=="number",m=x(s).split(`
|
|
3
|
+
`);typeof t?.tabWidth=="number"&&(m=m.map(e=>e.replaceAll(" "," ".repeat(t.tabWidth))));let{end:i,markerLines:n,start:l}=$(r,m,t.linesAbove,t.linesBelow),d=String(i).length,{gutter:u,marker:f,message:p}=t.color,o=m.slice(l,i).map((e,a)=>{let c=l+1+a,L=n[c],z=` ${c}`.slice(-d),N=!n[c+1],b=` ${z}${t.showGutter?" |":""}`;if(L){let C="";if(Array.isArray(L)){let P=e.replaceAll(/[^\t]/g," ").slice(0,Math.max(L[0]-1,0)),w=L[1]||1;C=[`
|
|
4
|
+
`,u(b.replaceAll(/\d/g," "))," ",P,f("^").repeat(w)].join(""),N&&t.message&&(C+=` ${p(t.message)}`);}return [f(W),u(b),e.length>0?` ${e}`:"",C].join("")}return ` ${u(b)}${e.length>0?` ${e}`:""}`}).join(`
|
|
5
|
+
`);return t.message&&!g&&(o=`${" ".repeat(d+1)}${t.message}
|
|
6
|
+
${o}`),o};
|
|
7
|
+
|
|
8
|
+
export { q as a };
|
|
9
|
+
//# sourceMappingURL=out.js.map
|
|
10
|
+
//# sourceMappingURL=chunk-EGCQFTZA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/util/normalize-lf.ts","../src/util/process.ts","../src/code-frame/get-marker-lines.ts","../src/code-frame/index.ts"],"names":["normalizeLF","code","normalize_lf_default","_process","processShims","process","target","property","process_default","getMarkerLines","loc","source","linesAbove","linesBelow","startLoc","endLoc","startLine","startColumn","endLine","endColumn","start","end","lineDiff","markerLines","index","lineNumber","sourceLength","get_marker_lines_default","POINTER","codeFrame","options","config","value","hasColumns","lines","ln","numberMaxWidth","colorizeGutter","colorizeMarker","colorizeMessage","frame","line","number","hasMarker","paddedNumber","lastMarkerLine","gutter","markerLine","markerSpacing","numberOfMarkers"],"mappings":"AAAA,IAAMA,EAAeC,GAAyBA,EAAK,WAAW,qBAAsB;AAAA,CAAI,EAEjFC,EAAQF,ECGf,IAAMG,EAAY,WAAW,SAAW,OAAO,OAAO,IAAI,EAEpDC,EAAiC,CACnC,SAAU,CAAC,CACf,EAEMC,EAAU,IAAI,MAAeF,EAAU,CACzC,IAAIG,EAAQC,EAAyB,CACjC,GAAIA,KAAYD,EAEZ,OAAOA,EAAOC,CAAQ,EAG1B,GAAIA,KAAYH,EAEZ,OAAOA,EAAaG,CAAQ,CAIpC,CACJ,CAAC,EAEMC,EAAQH,ECVf,IAAMI,EAAiB,CACnBC,EACAC,EACAC,EACAC,IAMC,CACD,IAAMC,EAA8B,CAChC,OAAQ,EAER,KAAM,GACN,GAAGJ,EAAI,KACX,EACMK,EAA4B,CAC9B,GAAGD,EACH,GAAGJ,EAAI,GACX,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AAO,CAACO,EAAeV,CAAO,EAAGS,EAAeS,CAAM,EAAGL,EAAK,OAAS,EAAI,IAAIA,CAAI,GAAK,GAAIM,CAAU,EAAE,KAAK,EAAE,CACnH,CAEA,MAAO,IAAIV,EAAeS,CAAM,CAAC,GAAGL,EAAK,OAAS,EAAI,IAAIA,CAAI,GAAK,EAAE,EACzE,CAAC,EACA,KAAK;AAAA,CAAI,EAEd,OAAIV,EAAO,SAAW,CAACE,IACnBO,EAAQ,GAAG,IAAI,OAAOJ,EAAiB,CAAC,CAAC,GAAGL,EAAO,OAAO;AAAA,EAAKS,CAAK,IAGjEA,CACX","sourcesContent":["const normalizeLF = (code: string): string => code.replaceAll(/\\r\\n|\\r(?!\\n)|\\n/gu, \"\\n\");\n\nexport default normalizeLF;\n","interface Process extends Partial<Omit<typeof globalThis.process, \"versions\">> {\n versions: Record<string, string>;\n}\n\n// eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-unnecessary-condition,no-underscore-dangle\nconst _process = (globalThis.process || Object.create(null)) as unknown as Process;\n\nconst processShims: Partial<Process> = {\n versions: {},\n};\n\nconst process = new Proxy<Process>(_process, {\n get(target, property: keyof Process) {\n if (property in target) {\n // eslint-disable-next-line security/detect-object-injection\n return target[property];\n }\n\n if (property in processShims) {\n // eslint-disable-next-line security/detect-object-injection\n return processShims[property];\n }\n\n return undefined;\n },\n});\n\nexport default process;\n","// eslint-disable-next-line no-secrets/no-secrets\n/**\n * This is a copy of the codeFrame function from Babel\n * @see https://github.com/babel/babel/blob/85e649203b61b7c908eb04c05511a0d35f893e8e/packages/babel-code-frame/src/index.ts#L68-L143\n *\n * MIT License\n *\n * Copyright (c) 2014-present Sebastian McKenzie and other contributors\n */\n\n/**\n * Extract what lines should be marked and highlighted.\n */\nimport type { CodeFrameLocation, CodeFrameNodeLocation } from \"./types\";\n\ntype MarkerLines = Record<number, true | [number | undefined, number | undefined]>;\n\nconst getMarkerLines = (\n loc: CodeFrameNodeLocation,\n source: string[],\n linesAbove: number,\n linesBelow: number,\n): {\n end: number;\n markerLines: MarkerLines;\n start: number;\n // eslint-disable-next-line sonarjs/cognitive-complexity\n} => {\n const startLoc: CodeFrameLocation = {\n column: 0,\n // @ts-expect-error Can be overwritten\n line: -1,\n ...loc.start,\n };\n const endLoc: CodeFrameLocation = {\n ...startLoc,\n ...loc.end,\n };\n const startLine = startLoc.line;\n const startColumn = startLoc.column;\n const endLine = endLoc.line;\n const endColumn = endLoc.column;\n\n let start = Math.max(startLine - (linesAbove + 1), 0);\n let end = Math.min(source.length, endLine + linesBelow);\n\n if (startLine === -1) {\n start = 0;\n }\n\n if (endLine === -1) {\n end = source.length;\n }\n\n const lineDiff = endLine - startLine;\n const markerLines: MarkerLines = {};\n\n if (lineDiff) {\n // eslint-disable-next-line no-plusplus,no-loops/no-loops\n for (let index = 0; index <= lineDiff; index++) {\n const lineNumber = index + startLine;\n\n if (!startColumn) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = true;\n } else if (index === 0) {\n const sourceLength = source[lineNumber - 1]?.length;\n\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [startColumn, (sourceLength ?? 0) - startColumn + 1];\n } else if (index === lineDiff) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [0, endColumn];\n } else {\n const sourceLength = source[lineNumber - index]?.length;\n\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [0, sourceLength];\n }\n }\n } else if (startColumn === endColumn) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[startLine] = startColumn ? [startColumn, 0] : true;\n } else {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[startLine] = [startColumn, (endColumn ?? 0) - (startColumn ?? 0)];\n }\n\n return { end, markerLines, start };\n};\n\nexport default getMarkerLines;\n","// eslint-disable-next-line no-secrets/no-secrets\n/**\n * This is a modified copy of the codeFrameColumns function from Babel\n * @see https://github.com/babel/babel/blob/85e649203b61b7c908eb04c05511a0d35f893e8e/packages/babel-code-frame/src/index.ts#L145-L217\n *\n * MIT License\n *\n * Copyright (c) 2014-present Sebastian McKenzie and other contributors\n */\n\nimport normalizeLF from \"../util/normalize-lf\";\nimport process from \"../util/process\";\nimport getMarkerLines from \"./get-marker-lines\";\nimport type { CodeFrameNodeLocation, CodeFrameOptions, ColorizeMethod } from \"./types\";\n\nconst POINTER = process.platform === \"win32\" && !process.env?.[\"WT_SESSION\"] ? \">\" : \"❯\";\n\n/** Generate a code frame from string and an error location */\nexport const codeFrame = (\n source: string,\n loc: CodeFrameNodeLocation,\n options?: CodeFrameOptions,\n // eslint-disable-next-line sonarjs/cognitive-complexity\n): string => {\n // grab 2 lines before, and 3 lines after focused line\n const config = {\n color: {\n gutter: (value: string) => value,\n marker: (value: string) => value,\n message: (value: string) => value,\n ...options?.color,\n },\n linesAbove: 2,\n linesBelow: 3,\n showGutter: true,\n showLineNumbers: true,\n tabWidth: 4,\n ...options,\n };\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const hasColumns = loc.start && typeof loc.start.column === \"number\";\n\n let lines = normalizeLF(source).split(\"\\n\");\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (typeof config?.tabWidth === \"number\") {\n lines = lines.map((ln) => ln.replaceAll(\"\\t\", \" \".repeat(config.tabWidth as number)));\n }\n\n const { end, markerLines, start } = getMarkerLines(loc, lines, config.linesAbove, config.linesBelow);\n\n const numberMaxWidth = String(end).length;\n\n const {\n gutter: colorizeGutter,\n marker: colorizeMarker,\n message: colorizeMessage,\n } = config.color as {\n gutter: ColorizeMethod;\n marker: ColorizeMethod;\n message: ColorizeMethod;\n };\n\n let frame = lines\n .slice(start, end)\n .map((line, index) => {\n const number = start + 1 + index;\n // eslint-disable-next-line security/detect-object-injection\n const hasMarker = markerLines[number];\n const paddedNumber = ` ${number}`.slice(-numberMaxWidth);\n const lastMarkerLine = !markerLines[number + 1];\n\n const gutter = ` ${paddedNumber}${config.showGutter ? ` |` : \"\"}`;\n\n if (hasMarker) {\n let markerLine = \"\";\n\n if (Array.isArray(hasMarker)) {\n const markerSpacing = line.replaceAll(/[^\\t]/g, \" \").slice(0, Math.max((hasMarker[0] as number) - 1, 0));\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const numberOfMarkers = hasMarker[1] || 1;\n\n markerLine = [\"\\n \", colorizeGutter(gutter.replaceAll(/\\d/g, \" \")), \" \", markerSpacing, colorizeMarker(\"^\").repeat(numberOfMarkers)].join(\n \"\",\n );\n\n if (lastMarkerLine && config.message) {\n markerLine += ` ${colorizeMessage(config.message)}`;\n }\n }\n\n return [colorizeMarker(POINTER), colorizeGutter(gutter), line.length > 0 ? ` ${line}` : \"\", markerLine].join(\"\");\n }\n\n return ` ${colorizeGutter(gutter)}${line.length > 0 ? ` ${line}` : \"\"}`;\n })\n .join(\"\\n\");\n\n if (config.message && !hasColumns) {\n frame = `${\" \".repeat(numberMaxWidth + 1)}${config.message}\\n${frame}`;\n }\n\n return frame;\n};\n\nexport type { CodeFrameLocation, CodeFrameNodeLocation, CodeFrameOptions, ColorizeMethod } from \"./types\";\n"]}
|
|
@@ -8,18 +8,19 @@ type CodeFrameNodeLocation = {
|
|
|
8
8
|
};
|
|
9
9
|
type ColorizeMethod = (value: string) => string;
|
|
10
10
|
type CodeFrameOptions = {
|
|
11
|
-
color?:
|
|
12
|
-
gutter
|
|
13
|
-
marker
|
|
14
|
-
message
|
|
15
|
-
}
|
|
11
|
+
color?: {
|
|
12
|
+
gutter?: ColorizeMethod;
|
|
13
|
+
marker?: ColorizeMethod;
|
|
14
|
+
message?: ColorizeMethod;
|
|
15
|
+
};
|
|
16
16
|
linesAbove?: number;
|
|
17
17
|
linesBelow?: number;
|
|
18
18
|
message?: string;
|
|
19
19
|
showGutter?: boolean;
|
|
20
20
|
showLineNumbers?: boolean;
|
|
21
|
+
tabWidth?: number | false;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
|
-
declare const codeFrame: (source: string, loc: CodeFrameNodeLocation, options?:
|
|
24
|
+
declare const codeFrame: (source: string, loc: CodeFrameNodeLocation, options?: CodeFrameOptions) => string;
|
|
24
25
|
|
|
25
26
|
export { type CodeFrameLocation, type CodeFrameNodeLocation, type CodeFrameOptions, type ColorizeMethod, codeFrame };
|
package/dist/code-frame/index.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { a as codeFrame } from './chunk-
|
|
1
|
+
export { a as codeFrame } from './chunk-EGCQFTZA.js';
|
|
2
2
|
export { c as VisulimaError, a as getErrorCauses, b as isVisulimaError } from './chunk-HAVTTG7L.js';
|
|
3
3
|
export { b as generatedPositionFor, a as loadSourceMap, c as originalPositionFor, d as sourceContentFor, e as traceSegment } from './chunk-7G4U7I4C.js';
|
|
4
4
|
export { a as parseStacktrace } from './chunk-57YC2IM6.js';
|
package/package.json
CHANGED
package/dist/chunk-EIN7B3HV.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
var A=s=>s.replaceAll(/\r\n|\r(?!\n)|\n/gu,`
|
|
2
|
-
`),x=A;var O=globalThis.process||Object.create(null),$={versions:{}},v=new Proxy(O,{get(s,r){if(r in s)return s[r];if(r in $)return $[r]}}),F=v;var S=(s,r,L,o)=>{let d={column:0,line:-1,...s.start},g={...d,...s.end},i=d.line,t=d.column,l=g.line,m=g.column,u=Math.max(i-(L+1),0),f=Math.min(r.length,l+o);i===-1&&(u=0),l===-1&&(f=r.length);let p=l-i,n={};if(p)for(let e=0;e<=p;e++){let a=e+i;if(!t)n[a]=!0;else if(e===0){let c=r[a-1]?.length;n[a]=[t,(c??0)-t+1];}else if(e===p)n[a]=[0,m];else {let c=r[a-e]?.length;n[a]=[0,c];}}else t===m?n[i]=t?[t,0]:!0:n[i]=[t,(m??0)-(t??0)];return {end:f,markerLines:n,start:u}},y=S;var j=F.platform==="win32"&&!F.env?.WT_SESSION?">":"\u276F",q=(s,r,L={})=>{let o={color:{gutter:e=>e,marker:e=>e,message:e=>e,...L.color},linesAbove:2,linesBelow:3,showGutter:!0,showLineNumbers:!0,...L},d=r.start&&typeof r.start.column=="number",g=x(s).split(`
|
|
3
|
-
`).map(e=>e.replaceAll(" "," ")),{end:i,markerLines:t,start:l}=y(r,g,o.linesAbove,o.linesBelow),m=String(i).length,{gutter:u,marker:f,message:p}=o.color,n=g.slice(l,i).map((e,a)=>{let c=l+1+a,h=t[c],z=` ${c}`.slice(-m),N=!t[c+1],b=` ${z}${o.showGutter?" |":""}`;if(h){let C="";if(Array.isArray(h)){let P=e.replaceAll(/[^\t]/g," ").slice(0,Math.max(h[0]-1,0)),w=h[1]||1;C=[`
|
|
4
|
-
`,u(b.replaceAll(/\d/g," "))," ",P,f("^").repeat(w)].join(""),N&&o.message&&(C+=` ${p(o.message)}`);}return [f(j),u(b),e.length>0?` ${e}`:"",C].join("")}return ` ${u(b)}${e.length>0?` ${e}`:""}`}).join(`
|
|
5
|
-
`);return o.message&&!d&&(n=`${" ".repeat(m+1)}${o.message}
|
|
6
|
-
${n}`),n};
|
|
7
|
-
|
|
8
|
-
export { q as a };
|
|
9
|
-
//# sourceMappingURL=out.js.map
|
|
10
|
-
//# sourceMappingURL=chunk-EIN7B3HV.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/util/normalize-lf.ts","../src/util/process.ts","../src/code-frame/get-marker-lines.ts","../src/code-frame/index.ts"],"names":["normalizeLF","code","normalize_lf_default","_process","processShims","process","target","property","process_default","getMarkerLines","loc","source","linesAbove","linesBelow","startLoc","endLoc","startLine","startColumn","endLine","endColumn","start","end","lineDiff","markerLines","index","lineNumber","sourceLength","get_marker_lines_default","POINTER","codeFrame","options","config","value","hasColumns","lines","ln","numberMaxWidth","colorizeGutter","colorizeMarker","colorizeMessage","frame","line","number","hasMarker","paddedNumber","lastMarkerLine","gutter","markerLine","markerSpacing","numberOfMarkers"],"mappings":"AAAA,IAAMA,EAAeC,GAAyBA,EAAK,WAAW,qBAAsB;AAAA,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M,QAAQJ,CAAS,EAAG,CAC1B,IAAMK,EAAgBP,EAAK,WAAW,SAAU,GAAG,EAAE,MAAM,EAAG,KAAK,IAAKE,EAAU,CAAC,EAAe,EAAG,CAAC,CAAC,EAEjGM,EAAkBN,EAAU,CAAC,GAAK,EAExCI,EAAa,CAAC;AAAA,GAAOV,EAAeS,EAAO,WAAW,MAAO,GAAG,CAAC,EAAG,IAAKE,EAAeV,EAAe,GAAG,EAAE,OAAOW,CAAe,CAAC,EAAE,KACjI,EACJ,EAEIJ,GAAkBd,EAAO,UACzBgB,GAAc,IAAIR,EAAgBR,EAAO,OAAO,CAAC,GAEzD,CAEA,MAAO,CAACO,EAAeV,CAAO,EAAGS,EAAeS,CAAM,EAAGL,EAAK,OAAS,EAAI,IAAIA,CAAI,GAAK,GAAIM,CAAU,EAAE,KAAK,EAAE,CACnH,CAEA,MAAO,IAAIV,EAAeS,CAAM,CAAC,GAAGL,EAAK,OAAS,EAAI,IAAIA,CAAI,GAAK,EAAE,EACzE,CAAC,EACA,KAAK;AAAA,CAAI,EAEd,OAAIV,EAAO,SAAW,CAACE,IACnBO,EAAQ,GAAG,IAAI,OAAOJ,EAAiB,CAAC,CAAC,GAAGL,EAAO,OAAO;AAAA,EAAKS,CAAK,IAGjEA,CACX","sourcesContent":["const normalizeLF = (code: string): string => code.replaceAll(/\\r\\n|\\r(?!\\n)|\\n/gu, \"\\n\");\n\nexport default normalizeLF;\n","interface Process extends Partial<Omit<typeof globalThis.process, \"versions\">> {\n versions: Record<string, string>;\n}\n\n// eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-unnecessary-condition,no-underscore-dangle\nconst _process = (globalThis.process || Object.create(null)) as unknown as Process;\n\nconst processShims: Partial<Process> = {\n versions: {},\n};\n\nconst process = new Proxy<Process>(_process, {\n get(target, property: keyof Process) {\n if (property in target) {\n // eslint-disable-next-line security/detect-object-injection\n return target[property];\n }\n\n if (property in processShims) {\n // eslint-disable-next-line security/detect-object-injection\n return processShims[property];\n }\n\n return undefined;\n },\n});\n\nexport default process;\n","// eslint-disable-next-line no-secrets/no-secrets\n/**\n * This is a copy of the codeFrame function from Babel\n * @see https://github.com/babel/babel/blob/85e649203b61b7c908eb04c05511a0d35f893e8e/packages/babel-code-frame/src/index.ts#L68-L143\n *\n * MIT License\n *\n * Copyright (c) 2014-present Sebastian McKenzie and other contributors\n */\n\n/**\n * Extract what lines should be marked and highlighted.\n */\nimport type { CodeFrameLocation, CodeFrameNodeLocation } from \"./types\";\n\ntype MarkerLines = Record<number, true | [number | undefined, number | undefined]>;\n\nconst getMarkerLines = (\n loc: CodeFrameNodeLocation,\n source: string[],\n linesAbove: number,\n linesBelow: number,\n): {\n end: number;\n markerLines: MarkerLines;\n start: number;\n // eslint-disable-next-line sonarjs/cognitive-complexity\n} => {\n const startLoc: CodeFrameLocation = {\n column: 0,\n // @ts-expect-error Can be overwritten\n line: -1,\n ...loc.start,\n };\n const endLoc: CodeFrameLocation = {\n ...startLoc,\n ...loc.end,\n };\n const startLine = startLoc.line;\n const startColumn = startLoc.column;\n const endLine = endLoc.line;\n const endColumn = endLoc.column;\n\n let start = Math.max(startLine - (linesAbove + 1), 0);\n let end = Math.min(source.length, endLine + linesBelow);\n\n if (startLine === -1) {\n start = 0;\n }\n\n if (endLine === -1) {\n end = source.length;\n }\n\n const lineDiff = endLine - startLine;\n const markerLines: MarkerLines = {};\n\n if (lineDiff) {\n // eslint-disable-next-line no-plusplus,no-loops/no-loops\n for (let index = 0; index <= lineDiff; index++) {\n const lineNumber = index + startLine;\n\n if (!startColumn) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = true;\n } else if (index === 0) {\n const sourceLength = source[lineNumber - 1]?.length;\n\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [startColumn, (sourceLength ?? 0) - startColumn + 1];\n } else if (index === lineDiff) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [0, endColumn];\n } else {\n const sourceLength = source[lineNumber - index]?.length;\n\n // eslint-disable-next-line security/detect-object-injection\n markerLines[lineNumber] = [0, sourceLength];\n }\n }\n } else if (startColumn === endColumn) {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[startLine] = startColumn ? [startColumn, 0] : true;\n } else {\n // eslint-disable-next-line security/detect-object-injection\n markerLines[startLine] = [startColumn, (endColumn ?? 0) - (startColumn ?? 0)];\n }\n\n return { end, markerLines, start };\n};\n\nexport default getMarkerLines;\n","// eslint-disable-next-line no-secrets/no-secrets\n/**\n * This is a modified copy of the codeFrameColumns function from Babel\n * @see https://github.com/babel/babel/blob/85e649203b61b7c908eb04c05511a0d35f893e8e/packages/babel-code-frame/src/index.ts#L145-L217\n *\n * MIT License\n *\n * Copyright (c) 2014-present Sebastian McKenzie and other contributors\n */\n\nimport normalizeLF from \"../util/normalize-lf\";\nimport process from \"../util/process\";\nimport getMarkerLines from \"./get-marker-lines\";\nimport type { CodeFrameNodeLocation, CodeFrameOptions, ColorizeMethod } from \"./types\";\n\nconst POINTER = process.platform === \"win32\" && !process.env?.[\"WT_SESSION\"] ? \">\" : \"❯\";\n\n/** Generate a code frame from string and an error location */\nexport const codeFrame = (\n source: string,\n loc: CodeFrameNodeLocation,\n options: Partial<CodeFrameOptions> = {},\n // eslint-disable-next-line sonarjs/cognitive-complexity\n): string => {\n // grab 2 lines before, and 3 lines after focused line\n const config = {\n color: {\n gutter: (value: string) => value,\n marker: (value: string) => value,\n message: (value: string) => value,\n ...options.color,\n },\n linesAbove: 2,\n linesBelow: 3,\n showGutter: true,\n showLineNumbers: true,\n ...options,\n };\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const hasColumns = loc.start && typeof loc.start.column === \"number\";\n\n const lines = normalizeLF(source)\n .split(\"\\n\")\n .map((ln) => ln.replaceAll(\"\\t\", \" \"));\n\n const { end, markerLines, start } = getMarkerLines(loc, lines, config.linesAbove, config.linesBelow);\n\n const numberMaxWidth = String(end).length;\n\n const {\n gutter: colorizeGutter,\n marker: colorizeMarker,\n message: colorizeMessage,\n } = config.color as {\n gutter: ColorizeMethod;\n marker: ColorizeMethod;\n message: ColorizeMethod;\n };\n\n let frame = lines\n .slice(start, end)\n .map((line, index) => {\n const number = start + 1 + index;\n // eslint-disable-next-line security/detect-object-injection\n const hasMarker = markerLines[number];\n const paddedNumber = ` ${number}`.slice(-numberMaxWidth);\n const lastMarkerLine = !markerLines[number + 1];\n\n const gutter = ` ${paddedNumber}${config.showGutter ? ` |` : \"\"}`;\n\n if (hasMarker) {\n let markerLine = \"\";\n\n if (Array.isArray(hasMarker)) {\n const markerSpacing = line.replaceAll(/[^\\t]/g, \" \").slice(0, Math.max((hasMarker[0] as number) - 1, 0));\n // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n const numberOfMarkers = hasMarker[1] || 1;\n\n markerLine = [\"\\n \", colorizeGutter(gutter.replaceAll(/\\d/g, \" \")), \" \", markerSpacing, colorizeMarker(\"^\").repeat(numberOfMarkers)].join(\n \"\",\n );\n\n if (lastMarkerLine && config.message) {\n markerLine += ` ${colorizeMessage(config.message)}`;\n }\n }\n\n return [colorizeMarker(POINTER), colorizeGutter(gutter), line.length > 0 ? ` ${line}` : \"\", markerLine].join(\"\");\n }\n\n return ` ${colorizeGutter(gutter)}${line.length > 0 ? ` ${line}` : \"\"}`;\n })\n .join(\"\\n\");\n\n if (config.message && !hasColumns) {\n frame = `${\" \".repeat(numberMaxWidth + 1)}${config.message}\\n${frame}`;\n }\n\n return frame;\n};\n\nexport type { CodeFrameLocation, CodeFrameNodeLocation, CodeFrameOptions, ColorizeMethod } from \"./types\";\n"]}
|