Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: write own codeframe (#36156)

Simon Knott02862442cf5b
diff --git a/package-lock.json b/package-lock.json
index fb1ce32e09..48621156fe 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1672,13 +1672,6 @@
"node": ">=10"
}
},
- "node_modules/@types/babel__code-frame": {
- "version": "7.0.6",
- "resolved": "https://registry.npmjs.org/@types/babel__code-frame/-/babel__code-frame-7.0.6.tgz",
- "integrity": "sha512-Anitqkl3+KrzcW2k77lRlg/GfLZLWXBuNgbEcIOU6M92yw42vsd3xV/Z/yAHEj8m+KUjL6bWOVOFqX8PFPJ4LA==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/@types/babel__core": {
"version": "7.20.5",
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",
@@ -8772,11 +8765,7 @@
"packages/trace-viewer": {
"version": "0.0.0",
"dependencies": {
- "@babel/code-frame": "^7.26.2",
"yaml": "^2.6.0"
- },
- "devDependencies": {
- "@types/babel__code-frame": "^7.0.6"
}
},
"packages/web": {
diff --git a/packages/trace-viewer/package.json b/packages/trace-viewer/package.json
index a52d5d66fe..57a3dfd409 100644
--- a/packages/trace-viewer/package.json
+++ b/packages/trace-viewer/package.json
@@ -4,10 +4,6 @@
"version": "0.0.0",
"type": "module",
"dependencies": {
- "@babel/code-frame": "^7.26.2",
"yaml": "^2.6.0"
- },
- "devDependencies": {
- "@types/babel__code-frame": "^7.0.6"
}
}
diff --git a/packages/trace-viewer/src/ui/errorsTab.tsx b/packages/trace-viewer/src/ui/errorsTab.tsx
index 379b58d8ab..3b67545af8 100644
--- a/packages/trace-viewer/src/ui/errorsTab.tsx
+++ b/packages/trace-viewer/src/ui/errorsTab.tsx
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import { codeFrameColumns } from '@babel/code-frame';
import { ErrorMessage } from '@web/components/errorMessage';
import * as React from 'react';
import type * as modelUtil from './modelUtil';
@@ -27,6 +26,7 @@ import { attachmentURL } from './attachmentsTab';
import { copyPrompt, stripAnsiEscapes } from '@web/shared/prompts';
import { MetadataWithCommitInfo } from '@testIsomorphic/types';
import { calculateSha1 } from './sourceTab';
+import type { StackFrame } from '@protocol/channels';
const CopyPromptButton: React.FC<{ prompt: string }> = ({ prompt }) => {
return (
@@ -111,21 +111,13 @@ export const ErrorsTab: React.FunctionComponent<{
const source = await response.text();
- return codeFrameColumns(
- source,
- {
- start: {
- line: location.line,
- column: location.column,
- },
- },
- {
- highlightCode: false,
- linesAbove: 100,
- linesBelow: 100,
- message: stripAnsiEscapes(error.message).split('\n')[0] || undefined,
- }
- );
+ return codeFrame({
+ source,
+ message: stripAnsiEscapes(error.message).split('\n')[0] || undefined,
+ location,
+ linesAbove: 100,
+ linesBelow: 100,
+ });
}, []);
const prompt = useAsyncMemo(
@@ -155,3 +147,15 @@ export const ErrorsTab: React.FunctionComponent<{
})}
</div>;
};
+
+function codeFrame({ source, message, location, linesAbove, linesBelow }: { source: string, message?: string, location: StackFrame, linesAbove: number, linesBelow: number }): string {
+ const lines = source.split('\n').slice();
+ const start = Math.max(0, location.line - linesAbove - 1);
+ const end = Math.min(lines.length, location.line + linesBelow);
+ const scope = lines.slice(start, end);
+ const lineNumberWidth = String(end).length;
+ const frame = scope.map((line, index) => `${(start + index + 1) === location.line ? '> ' : ' '}${(start + index + 1).toString().padEnd(lineNumberWidth, ' ')} | ${line}`);
+ if (message)
+ frame.splice(location.line - start, 0, `${' '.repeat(lineNumberWidth + 2)} | ${' '.repeat(location.column - 2)} ^ ${message}`);
+ return frame.join('\n');
+}