fix: filter out comments inside fixture destructuring (#20989)

Fixes #20944
This commit is contained in:
Andrey Lushnikov 2023-02-21 12:51:40 -08:00 committed by GitHub
parent 1cf1f53076
commit ce692830b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -227,7 +227,7 @@ export function fixtureParameterNames(fn: Function | any, location: Location, on
}
function innerFixtureParameterNames(fn: Function, location: Location, onError: LoadErrorSink): string[] {
const text = fn.toString();
const text = filterOutComments(fn.toString());
const match = text.match(/(?:async)?(?:\s+function)?[^(]*\(([^)]*)/);
if (!match)
return [];
@ -241,11 +241,35 @@ function innerFixtureParameterNames(fn: Function, location: Location, onError: L
}
const props = splitByComma(firstParam.substring(1, firstParam.length - 1)).map(prop => {
const colon = prop.indexOf(':');
return colon === -1 ? prop : prop.substring(0, colon).trim();
return colon === -1 ? prop.trim() : prop.substring(0, colon).trim();
});
return props;
}
function filterOutComments(s: string): string {
const result: string[] = [];
let commentState: 'none'|'singleline'|'multiline' = 'none';
for (let i = 0; i < s.length; ++i) {
if (commentState === 'singleline') {
if (s[i] === '\n')
commentState = 'none';
} else if (commentState === 'multiline') {
if (s[i - 1] === '*' && s[i] === '/')
commentState = 'none';
} else if (commentState === 'none') {
if (s[i] === '/' && s[i + 1] === '/') {
commentState = 'singleline';
} else if (s[i] === '/' && s[i + 1] === '*') {
commentState = 'multiline';
i += 2;
} else {
result.push(s[i]);
}
}
}
return result.join('');
}
function splitByComma(s: string) {
const result: string[] = [];
const stack: string[] = [];

View File

@ -32,6 +32,32 @@ test('should work', async ({ runInlineTest }) => {
expect(results[0].status).toBe('passed');
});
test('should work with comments inside fixtures', async ({ runInlineTest }) => {
const { results } = await runInlineTest({
'a.test.ts': `
import { test as base, expect } from '@playwright/test';
const test = base.extend({
asdf: async ({}, test) => await test(123),
foo: async ({}, test) => await test('foo'),
bar: async ({}, test) => await test('bar'),
});
test('should use asdf', async ({ // }) {,,, /*
asdf, // a comment
/*/aa* /* */ // line // //
/* // */ foo, /* what // */ bar // whoa
/* some // comment */ : //
/* // /* // */ barbar /* /* /* */
}) => {
expect(asdf).toBe(123);
expect(foo).toBe('foo');
expect(barbar).toBe('bar');
});
`,
});
expect(results[0].status).toBe('passed');
});
test('should work with a sync test function', async ({ runInlineTest }) => {
const { results } = await runInlineTest({
'a.test.ts': `