2020-12-28 23:50:12 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-09-12 20:40:44 +02:00
|
|
|
import { sanitizeDeviceOptions, toClickOptionsForSourceCode, toKeyboardModifiers, toSignalMap } from './language';
|
2025-02-07 22:54:01 +01:00
|
|
|
import { asLocator, escapeWithQuotes } from '../../utils';
|
2024-05-08 20:08:40 +02:00
|
|
|
import { deviceDescriptors } from '../deviceDescriptors';
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2025-02-07 22:54:01 +01:00
|
|
|
import type { Language, LanguageGenerator, LanguageGeneratorOptions } from './types';
|
|
|
|
|
import type { BrowserContextOptions } from '../../../types/types';
|
|
|
|
|
import type * as actions from '@recorder/actions';
|
|
|
|
|
|
2022-08-25 11:58:58 +02:00
|
|
|
type CSharpLanguageMode = 'library' | 'mstest' | 'nunit';
|
|
|
|
|
|
2020-12-28 23:50:12 +01:00
|
|
|
export class CSharpLanguageGenerator implements LanguageGenerator {
|
2022-08-25 11:58:58 +02:00
|
|
|
id: string;
|
|
|
|
|
groupName = '.NET C#';
|
|
|
|
|
name: string;
|
2022-10-06 02:59:34 +02:00
|
|
|
highlighter = 'csharp' as Language;
|
2022-08-25 11:58:58 +02:00
|
|
|
_mode: CSharpLanguageMode;
|
|
|
|
|
|
|
|
|
|
constructor(mode: CSharpLanguageMode) {
|
|
|
|
|
if (mode === 'library') {
|
|
|
|
|
this.name = 'Library';
|
|
|
|
|
this.id = 'csharp';
|
|
|
|
|
} else if (mode === 'mstest') {
|
|
|
|
|
this.name = 'MSTest';
|
|
|
|
|
this.id = 'csharp-mstest';
|
|
|
|
|
} else if (mode === 'nunit') {
|
|
|
|
|
this.name = 'NUnit';
|
|
|
|
|
this.id = 'csharp-nunit';
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Unknown C# language mode: ${mode}`);
|
|
|
|
|
}
|
|
|
|
|
this._mode = mode;
|
|
|
|
|
}
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2024-09-26 23:50:09 +02:00
|
|
|
generateAction(actionInContext: actions.ActionInContext): string {
|
2022-08-25 11:58:58 +02:00
|
|
|
const action = this._generateActionInner(actionInContext);
|
|
|
|
|
if (action)
|
2023-12-15 19:24:26 +01:00
|
|
|
return action;
|
2022-08-25 11:58:58 +02:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 23:50:09 +02:00
|
|
|
_generateActionInner(actionInContext: actions.ActionInContext): string {
|
2022-02-05 04:27:45 +01:00
|
|
|
const action = actionInContext.action;
|
2022-08-25 11:58:58 +02:00
|
|
|
if (this._mode !== 'library' && (action.name === 'openPage' || action.name === 'closePage'))
|
|
|
|
|
return '';
|
|
|
|
|
let pageAlias = actionInContext.frame.pageAlias;
|
|
|
|
|
if (this._mode !== 'library')
|
|
|
|
|
pageAlias = pageAlias.replace('page', 'Page');
|
2023-12-15 19:24:26 +01:00
|
|
|
const formatter = new CSharpFormatter(this._mode === 'library' ? 0 : 8);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
if (action.name === 'openPage') {
|
|
|
|
|
formatter.add(`var ${pageAlias} = await context.NewPageAsync();`);
|
|
|
|
|
if (action.url && action.url !== 'about:blank' && action.url !== 'chrome://newtab/')
|
2021-05-13 20:57:02 +02:00
|
|
|
formatter.add(`await ${pageAlias}.GotoAsync(${quote(action.url)});`);
|
2020-12-28 23:50:12 +01:00
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 04:11:14 +02:00
|
|
|
const locators = actionInContext.frame.framePath.map(selector => `.${this._asLocator(selector)}.ContentFrame`);
|
2024-08-26 19:28:54 +02:00
|
|
|
const subject = `${pageAlias}${locators.join('')}`;
|
2021-02-17 03:13:26 +01:00
|
|
|
const signals = toSignalMap(action);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.dialog) {
|
2021-05-21 00:47:14 +02:00
|
|
|
formatter.add(` void ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler(object sender, IDialog dialog)
|
2020-12-28 23:50:12 +01:00
|
|
|
{
|
2021-05-21 00:47:14 +02:00
|
|
|
Console.WriteLine($"Dialog message: {dialog.Message}");
|
|
|
|
|
dialog.DismissAsync();
|
2021-02-17 03:13:26 +01:00
|
|
|
${pageAlias}.Dialog -= ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
2021-02-17 03:13:26 +01:00
|
|
|
${pageAlias}.Dialog += ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler;`);
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
const lines: string[] = [];
|
2024-08-27 00:24:02 +02:00
|
|
|
lines.push(this._generateActionCall(subject, actionInContext));
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
if (signals.download) {
|
2021-05-27 00:44:40 +02:00
|
|
|
lines.unshift(`var download${signals.download.downloadAlias} = await ${pageAlias}.RunAndWaitForDownloadAsync(async () =>\n{`);
|
2021-05-21 00:47:14 +02:00
|
|
|
lines.push(`});`);
|
|
|
|
|
}
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
if (signals.popup) {
|
2021-05-27 00:44:40 +02:00
|
|
|
lines.unshift(`var ${signals.popup.popupAlias} = await ${pageAlias}.RunAndWaitForPopupAsync(async () =>\n{`);
|
2021-05-21 00:47:14 +02:00
|
|
|
lines.push(`});`);
|
|
|
|
|
}
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
for (const line of lines)
|
|
|
|
|
formatter.add(line);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 23:50:09 +02:00
|
|
|
private _generateActionCall(subject: string, actionInContext: actions.ActionInContext): string {
|
2024-08-27 00:24:02 +02:00
|
|
|
const action = actionInContext.action;
|
2020-12-28 23:50:12 +01:00
|
|
|
switch (action.name) {
|
|
|
|
|
case 'openPage':
|
|
|
|
|
throw Error('Not reached');
|
|
|
|
|
case 'closePage':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.CloseAsync();`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'click': {
|
2021-05-21 00:47:14 +02:00
|
|
|
let method = 'Click';
|
2020-12-28 23:50:12 +01:00
|
|
|
if (action.clickCount === 2)
|
2021-05-21 00:47:14 +02:00
|
|
|
method = 'DblClick';
|
2024-09-12 20:40:44 +02:00
|
|
|
const options = toClickOptionsForSourceCode(action);
|
2021-05-21 00:47:14 +02:00
|
|
|
if (!Object.entries(options).length)
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.${method}Async();`;
|
2022-02-05 04:27:45 +01:00
|
|
|
const optionsString = formatObject(options, ' ', 'Locator' + method + 'Options');
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.${method}Async(${optionsString});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
case 'check':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.CheckAsync();`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'uncheck':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.UncheckAsync();`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'fill':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.FillAsync(${quote(action.text)});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'setInputFiles':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.SetInputFilesAsync(${formatObject(action.files)});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'press': {
|
2024-08-27 00:24:02 +02:00
|
|
|
const modifiers = toKeyboardModifiers(action.modifiers);
|
2020-12-28 23:50:12 +01:00
|
|
|
const shortcut = [...modifiers, action.key].join('+');
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.PressAsync(${quote(shortcut)});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
case 'navigate':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.GotoAsync(${quote(action.url)});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'select':
|
2023-10-27 03:49:14 +02:00
|
|
|
return `await ${subject}.${this._asLocator(action.selector)}.SelectOptionAsync(${formatObject(action.options)});`;
|
|
|
|
|
case 'assertText':
|
|
|
|
|
return `await Expect(${subject}.${this._asLocator(action.selector)}).${action.substring ? 'ToContainTextAsync' : 'ToHaveTextAsync'}(${quote(action.text)});`;
|
2023-11-02 05:17:25 +01:00
|
|
|
case 'assertChecked':
|
|
|
|
|
return `await Expect(${subject}.${this._asLocator(action.selector)})${action.checked ? '' : '.Not'}.ToBeCheckedAsync();`;
|
2023-11-14 21:55:34 +01:00
|
|
|
case 'assertVisible':
|
|
|
|
|
return `await Expect(${subject}.${this._asLocator(action.selector)}).ToBeVisibleAsync();`;
|
2023-11-02 05:17:25 +01:00
|
|
|
case 'assertValue': {
|
2024-01-27 23:54:22 +01:00
|
|
|
const assertion = action.value ? `ToHaveValueAsync(${quote(action.value)})` : `ToBeEmptyAsync()`;
|
2023-11-02 05:17:25 +01:00
|
|
|
return `await Expect(${subject}.${this._asLocator(action.selector)}).${assertion};`;
|
|
|
|
|
}
|
2024-10-15 22:38:55 +02:00
|
|
|
case 'assertSnapshot':
|
|
|
|
|
return `await Expect(${subject}.${this._asLocator(action.selector)}).ToMatchAriaSnapshotAsync(${quote(action.snapshot)});`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 02:14:02 +02:00
|
|
|
private _asLocator(selector: string) {
|
2022-10-05 22:13:22 +02:00
|
|
|
return asLocator('csharp', selector);
|
2022-10-04 02:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
generateHeader(options: LanguageGeneratorOptions): string {
|
2022-08-25 11:58:58 +02:00
|
|
|
if (this._mode === 'library')
|
|
|
|
|
return this.generateStandaloneHeader(options);
|
|
|
|
|
return this.generateTestRunnerHeader(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateStandaloneHeader(options: LanguageGeneratorOptions): string {
|
2020-12-28 23:50:12 +01:00
|
|
|
const formatter = new CSharpFormatter(0);
|
|
|
|
|
formatter.add(`
|
2021-05-25 19:59:17 +02:00
|
|
|
using Microsoft.Playwright;
|
2021-05-21 00:47:14 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-12-15 19:24:26 +01:00
|
|
|
using var playwright = await Playwright.CreateAsync();
|
|
|
|
|
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
|
|
|
|
|
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
|
2024-12-18 22:34:06 +01:00
|
|
|
if (options.contextOptions.recordHar) {
|
|
|
|
|
const url = options.contextOptions.recordHar.urlFilter;
|
|
|
|
|
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)}${url ? `, ${formatObject({ url }, ' ', 'BrowserContextRouteFromHAROptions')}` : ''});`);
|
|
|
|
|
}
|
2022-08-25 11:58:58 +02:00
|
|
|
formatter.newLine();
|
|
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateTestRunnerHeader(options: LanguageGeneratorOptions): string {
|
|
|
|
|
const formatter = new CSharpFormatter(0);
|
|
|
|
|
formatter.add(`
|
|
|
|
|
using Microsoft.Playwright.${this._mode === 'nunit' ? 'NUnit' : 'MSTest'};
|
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
|
|
2022-09-08 01:44:58 +02:00
|
|
|
${this._mode === 'nunit' ? `[Parallelizable(ParallelScope.Self)]
|
|
|
|
|
[TestFixture]` : '[TestClass]'}
|
2022-08-25 11:58:58 +02:00
|
|
|
public class Tests : PageTest
|
|
|
|
|
{`);
|
|
|
|
|
const formattedContextOptions = formatContextOptions(options.contextOptions, options.deviceName);
|
|
|
|
|
if (formattedContextOptions) {
|
|
|
|
|
formatter.add(`public override BrowserNewContextOptions ContextOptions()
|
|
|
|
|
{
|
|
|
|
|
return ${formattedContextOptions};
|
|
|
|
|
}`);
|
|
|
|
|
formatter.newLine();
|
|
|
|
|
}
|
2022-09-14 22:44:38 +02:00
|
|
|
formatter.add(` [${this._mode === 'nunit' ? 'Test' : 'TestMethod'}]
|
2022-08-25 11:58:58 +02:00
|
|
|
public async Task MyTest()
|
|
|
|
|
{`);
|
2024-12-18 22:34:06 +01:00
|
|
|
if (options.contextOptions.recordHar) {
|
|
|
|
|
const url = options.contextOptions.recordHar.urlFilter;
|
|
|
|
|
formatter.add(` await Context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)}${url ? `, ${formatObject({ url }, ' ', 'BrowserContextRouteFromHAROptions')}` : ''});`);
|
|
|
|
|
}
|
2020-12-28 23:50:12 +01:00
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateFooter(saveStorage: string | undefined): string {
|
2023-12-15 19:24:26 +01:00
|
|
|
const offset = this._mode === 'library' ? '' : ' ';
|
|
|
|
|
let storageStateLine = saveStorage ? `\n${offset}await context.StorageStateAsync(new BrowserContextStorageStateOptions\n${offset}{\n${offset} Path = ${quote(saveStorage)}\n${offset}});\n` : '';
|
|
|
|
|
if (this._mode !== 'library')
|
|
|
|
|
storageStateLine += ` }\n}\n`;
|
|
|
|
|
return storageStateLine;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatObject(value: any, indent = ' ', name = ''): string {
|
|
|
|
|
if (typeof value === 'string') {
|
2022-07-05 22:30:46 +02:00
|
|
|
if (['permissions', 'colorScheme', 'modifiers', 'button', 'recordHarContent', 'recordHarMode', 'serviceWorkers'].includes(name))
|
2020-12-28 23:50:12 +01:00
|
|
|
return `${getClassName(name)}.${toPascal(value)}`;
|
|
|
|
|
return quote(value);
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(value))
|
|
|
|
|
return `new[] { ${value.map(o => formatObject(o, indent, name)).join(', ')} }`;
|
|
|
|
|
if (typeof value === 'object') {
|
2022-07-05 22:30:46 +02:00
|
|
|
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
|
2020-12-28 23:50:12 +01:00
|
|
|
if (!keys.length)
|
2021-05-21 00:47:14 +02:00
|
|
|
return name ? `new ${getClassName(name)}` : '';
|
2020-12-28 23:50:12 +01:00
|
|
|
const tokens: string[] = [];
|
2021-05-21 00:47:14 +02:00
|
|
|
for (const key of keys) {
|
|
|
|
|
const property = getPropertyName(key);
|
|
|
|
|
tokens.push(`${property} = ${formatObject(value[key], indent, key)},`);
|
|
|
|
|
}
|
2020-12-28 23:50:12 +01:00
|
|
|
if (name)
|
|
|
|
|
return `new ${getClassName(name)}\n{\n${indent}${tokens.join(`\n${indent}`)}\n${indent}}`;
|
|
|
|
|
return `{\n${indent}${tokens.join(`\n${indent}`)}\n${indent}}`;
|
|
|
|
|
}
|
|
|
|
|
if (name === 'latitude' || name === 'longitude')
|
|
|
|
|
return String(value) + 'm';
|
|
|
|
|
|
|
|
|
|
return String(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getClassName(value: string): string {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 'viewport': return 'ViewportSize';
|
|
|
|
|
case 'proxy': return 'ProxySettings';
|
|
|
|
|
case 'permissions': return 'ContextPermission';
|
2021-05-21 00:47:14 +02:00
|
|
|
case 'modifiers': return 'KeyboardModifier';
|
|
|
|
|
case 'button': return 'MouseButton';
|
2022-07-05 22:30:46 +02:00
|
|
|
case 'recordHarMode': return 'HarMode';
|
|
|
|
|
case 'recordHarContent': return 'HarContentPolicy';
|
|
|
|
|
case 'serviceWorkers': return 'ServiceWorkerPolicy';
|
2020-12-28 23:50:12 +01:00
|
|
|
default: return toPascal(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
function getPropertyName(key: string): string {
|
|
|
|
|
switch (key) {
|
|
|
|
|
case 'storageState': return 'StorageStatePath';
|
|
|
|
|
case 'viewport': return 'ViewportSize';
|
|
|
|
|
default: return toPascal(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 23:50:12 +01:00
|
|
|
function toPascal(value: string): string {
|
|
|
|
|
return value[0].toUpperCase() + value.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 20:59:20 +01:00
|
|
|
function formatContextOptions(contextOptions: BrowserContextOptions, deviceName: string | undefined): string {
|
|
|
|
|
let options = { ...contextOptions };
|
|
|
|
|
// recordHAR is replaced with routeFromHAR in the generated code.
|
|
|
|
|
delete options.recordHar;
|
2021-01-25 04:21:19 +01:00
|
|
|
const device = deviceName && deviceDescriptors[deviceName];
|
2021-05-21 00:47:14 +02:00
|
|
|
if (!device) {
|
|
|
|
|
if (!Object.entries(options).length)
|
|
|
|
|
return '';
|
2024-11-11 20:59:20 +01:00
|
|
|
return formatObject(options, ' ', 'BrowserNewContextOptions');
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 00:47:14 +02:00
|
|
|
options = sanitizeDeviceOptions(device, options);
|
|
|
|
|
if (!Object.entries(options).length)
|
|
|
|
|
return `playwright.Devices[${quote(deviceName!)}]`;
|
|
|
|
|
|
2024-11-11 20:59:20 +01:00
|
|
|
return formatObject(options, ' ', `BrowserNewContextOptions(playwright.Devices[${quote(deviceName!)}])`);
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CSharpFormatter {
|
|
|
|
|
private _baseIndent: string;
|
|
|
|
|
private _baseOffset: string;
|
|
|
|
|
private _lines: string[] = [];
|
|
|
|
|
|
|
|
|
|
constructor(offset = 0) {
|
|
|
|
|
this._baseIndent = ' '.repeat(4);
|
|
|
|
|
this._baseOffset = ' '.repeat(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prepend(text: string) {
|
|
|
|
|
this._lines = text.trim().split('\n').map(line => line.trim()).concat(this._lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(text: string) {
|
|
|
|
|
this._lines.push(...text.trim().split('\n').map(line => line.trim()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newLine() {
|
|
|
|
|
this._lines.push('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
format(): string {
|
|
|
|
|
let spaces = '';
|
|
|
|
|
let previousLine = '';
|
|
|
|
|
return this._lines.map((line: string) => {
|
|
|
|
|
if (line === '')
|
|
|
|
|
return line;
|
2021-03-15 16:07:57 +01:00
|
|
|
if (line.startsWith('}') || line.startsWith(']') || line.includes('});') || line === ');')
|
2020-12-28 23:50:12 +01:00
|
|
|
spaces = spaces.substring(this._baseIndent.length);
|
|
|
|
|
|
|
|
|
|
const extraSpaces = /^(for|while|if).*\(.*\)$/.test(previousLine) ? this._baseIndent : '';
|
|
|
|
|
previousLine = line;
|
|
|
|
|
|
|
|
|
|
line = spaces + extraSpaces + line;
|
|
|
|
|
if (line.endsWith('{') || line.endsWith('[') || line.endsWith('('))
|
|
|
|
|
spaces += this._baseIndent;
|
2021-05-21 00:47:14 +02:00
|
|
|
if (line.endsWith('));'))
|
2020-12-28 23:50:12 +01:00
|
|
|
spaces = spaces.substring(this._baseIndent.length);
|
|
|
|
|
|
|
|
|
|
return this._baseOffset + line;
|
|
|
|
|
}).join('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quote(text: string) {
|
2021-08-23 18:22:19 +02:00
|
|
|
return escapeWithQuotes(text, '\"');
|
2022-02-05 04:27:45 +01:00
|
|
|
}
|