Skip to content

Commit d08c1ba

Browse files
committed
chore(): fix tests description
1 parent 9560bfa commit d08c1ba

3 files changed

Lines changed: 20 additions & 26 deletions

File tree

workers/javascript/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { JavaScriptEventWorkerTask } from '../types/javascript-event-worker-task
1010
import { BeautifyBacktracePayload } from '../types/beautify-backtrace-payload';
1111
import HawkCatcher from '@hawk.so/nodejs';
1212
import { BacktraceFrame, CatcherMessagePayload, CatcherMessageType, ErrorsCatcherType, SourceCodeLine, SourceMapDataExtended } from '@hawk.so/types';
13-
import { beautifyUserAgent, getBabelParserPluginsForFile, prepareSourceForParsing } from './utils';
13+
import { beautifyUserAgent, getBabelParserPluginsForFile, extractScriptFromSFC } from './utils';
1414
import { Collection } from 'mongodb';
1515
import { parse } from '@babel/parser';
1616
import traverse from '@babel/traverse';
@@ -272,7 +272,7 @@ export default class JavascriptEventWorker extends EventWorker {
272272
code: codeToParse,
273273
targetLine,
274274
hasTypeScriptLang,
275-
} = prepareSourceForParsing(sourceCode, line, sourcePath);
275+
} = extractScriptFromSFC(sourceCode, line, sourcePath);
276276

277277
let functionName: string | null = null;
278278
let className: string | null = null;

workers/javascript/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function cleanSourcePath(sourcePath: string): string {
6464
* @param sourcePath - original source path from the source map (used to pick parser plugins)
6565
* @returns - object with source code, target line and if it has TypeScript language specifier
6666
*/
67-
export function prepareSourceForParsing(
67+
export function extractScriptFromSFC(
6868
sourceCode: string,
6969
originalLine: number,
7070
sourcePath?: string
@@ -149,7 +149,7 @@ export function getBabelParserPluginsForFile(sourcePath?: string, hasTypeScriptL
149149
} else {
150150
enableTypeScript = enableTypeScript || isTypeScript;
151151

152-
if (isJavaScriptWithJsx || isJavaScript || isFrameworkFile || !enableTypeScript) {
152+
if (!enableTypeScript && (isJavaScriptWithJsx || isJavaScript || isFrameworkFile)) {
153153
enableJSX = true;
154154
}
155155
}

workers/javascript/tests/parser.test.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { prepareSourceForParsing, getBabelParserPluginsForFile } from '../src/utils';
1+
import { extractScriptFromSFC, getBabelParserPluginsForFile } from '../src/utils';
22

3-
describe('prepareSourceForParsing', () => {
4-
it('returns default result when sourcePath is not provided', () => {
3+
describe('extractScriptFromSFC', () => {
4+
it('returns passed source code and line with hasTypeScriptLang false when sourcePath is not provided', () => {
55
const sourceCode = `line1\nline2\nline3`;
66

7-
expect(prepareSourceForParsing(sourceCode, 2)).toEqual({
7+
expect(extractScriptFromSFC(sourceCode, 2)).toEqual({
88
code: sourceCode,
99
targetLine: 2,
1010
hasTypeScriptLang: false,
@@ -14,7 +14,7 @@ describe('prepareSourceForParsing', () => {
1414
it('returns default result when sourcePath is not a framework file (.vue/.svelte)', () => {
1515
const sourceCode = `<script>console.log(1)</script>`;
1616

17-
expect(prepareSourceForParsing(sourceCode, 1, 'src/app.ts')).toEqual({
17+
expect(extractScriptFromSFC(sourceCode, 1, 'src/app.ts')).toEqual({
1818
code: sourceCode,
1919
targetLine: 1,
2020
hasTypeScriptLang: false,
@@ -35,7 +35,7 @@ describe('prepareSourceForParsing', () => {
3535
`const b = 2;\n` +
3636
`</script>\n`;
3737

38-
const res = prepareSourceForParsing(sourceCode, 4, 'src/App.vue');
38+
const res = extractScriptFromSFC(sourceCode, 4, 'src/App.vue');
3939

4040
// startLine = 2 (line with <script>)
4141
// originalLine = 4 => relativeLine = 4 - 2 + 1 = 3
@@ -51,7 +51,7 @@ describe('prepareSourceForParsing', () => {
5151
`let x: number = 1;\n` +
5252
`</script>\n`;
5353

54-
const res = prepareSourceForParsing(sourceCode, 3, 'src/App.vue');
54+
const res = extractScriptFromSFC(sourceCode, 3, 'src/App.vue');
5555

5656
expect(res.hasTypeScriptLang).toBe(true);
5757
});
@@ -62,12 +62,12 @@ describe('prepareSourceForParsing', () => {
6262
`let y: number = 2;\n` +
6363
`</script>\n`;
6464

65-
const res = prepareSourceForParsing(sourceCode, 2, 'src/App.svelte');
65+
const res = extractScriptFromSFC(sourceCode, 2, 'src/App.svelte');
6666

6767
expect(res.hasTypeScriptLang).toBe(true);
6868
});
6969

70-
it('picks the correct script block when there are multiple <script> tags', () => {
70+
it('picks the script block related to the target line when there are multiple <script> tags', () => {
7171
const targetLine = 5;
7272
const startLine = 4;
7373

@@ -86,7 +86,7 @@ describe('prepareSourceForParsing', () => {
8686
`let z: number = 3;\n` +
8787
`</script>\n`;
8888

89-
const res = prepareSourceForParsing(sourceCode, targetLine, 'src/App.vue');
89+
const res = extractScriptFromSFC(sourceCode, targetLine, 'src/App.vue');
9090

9191
expect(res.code).toBe(`\nlet z: number = 3;\n`);
9292
expect(res.targetLine).toBe(targetLine - startLine + 1);
@@ -103,7 +103,7 @@ describe('prepareSourceForParsing', () => {
103103
`</script>\n`;
104104

105105
// line 2 is inside template, not script
106-
const res = prepareSourceForParsing(sourceCode, 2, 'src/App.vue');
106+
const res = extractScriptFromSFC(sourceCode, 2, 'src/App.vue');
107107

108108
expect(res).toEqual({
109109
code: sourceCode,
@@ -115,7 +115,7 @@ describe('prepareSourceForParsing', () => {
115115
it('handles script tag with attributes and inline content', () => {
116116
const sourceCode = `<script setup lang="ts">const a: number = 1;</script>\n`;
117117
// originalLine 1 is the line containing <script ...> and content is inline
118-
const res = prepareSourceForParsing(sourceCode, 1, 'src/App.vue');
118+
const res = extractScriptFromSFC(sourceCode, 1, 'src/App.vue');
119119

120120
expect(res.code).toBe(`const a: number = 1;`);
121121
expect(res.targetLine).toBe(1);
@@ -164,30 +164,24 @@ describe('getBabelParserPluginsForFile', () => {
164164
expect(plugins).toEqual([...base, 'jsx']);
165165
});
166166

167-
it('for .vue enables JSX (and TypeScript only if hasTypeScriptLang=true)', () => {
167+
it('for .vue enables one of JSX or TypeScript based on hasTypeScriptLang value', () => {
168168
const pluginsNoTs = getBabelParserPluginsForFile('src/App.vue', false);
169169

170170
expect(pluginsNoTs).toEqual([...base, 'jsx']);
171171

172172
const pluginsWithTs = getBabelParserPluginsForFile('src/App.vue', true);
173173

174-
expect(pluginsWithTs).toEqual([...base, 'typescript', 'jsx']);
174+
expect(pluginsWithTs).toEqual([...base, 'typescript']);
175175
});
176176

177-
it('for .svelte enables JSX (and TypeScript only if hasTypeScriptLang=true)', () => {
177+
it('for .svelte enables one of JSX or TypeScript based on hasTypeScriptLang value', () => {
178178
const pluginsNoTs = getBabelParserPluginsForFile('src/App.svelte', false);
179179

180180
expect(pluginsNoTs).toEqual([...base, 'jsx']);
181181

182182
const pluginsWithTs = getBabelParserPluginsForFile('src/App.svelte', true);
183183

184-
expect(pluginsWithTs).toEqual([...base, 'typescript', 'jsx']);
185-
});
186-
187-
it('for .js enables JSX and respects hasTypeScriptLang', () => {
188-
const plugins = getBabelParserPluginsForFile('src/App.js', true);
189-
190-
expect(plugins).toEqual([...base, 'typescript', 'jsx']);
184+
expect(pluginsWithTs).toEqual([...base, 'typescript']);
191185
});
192186

193187
it('for .js enables JSX without TypeScript when hasTypeScriptLang is false', () => {

0 commit comments

Comments
 (0)