Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header is arguably one place where we'd want there to be a space after.


import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

Expand Down
1 change: 0 additions & 1 deletion src/codegen/browser/__snapshots__/browser/check-element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
1 change: 0 additions & 1 deletion src/codegen/browser/__snapshots__/browser/click-element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

export const options = {
scenarios: { default: { executor: "shared-iterations" } },
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
1 change: 0 additions & 1 deletion src/codegen/browser/__snapshots__/browser/goto-url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
1 change: 0 additions & 1 deletion src/codegen/browser/__snapshots__/browser/reload-page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z

import { browser } from "k6/browser";

export const options = {
Expand Down
6 changes: 5 additions & 1 deletion src/codegen/browser/code/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { AST_NODE_TYPES, TSESTree as ts } from '@typescript-eslint/types'

import { baseProps } from '@/codegen/estree/nodes'

export function comment(value: string): ts.BlockStatement {
export function comment(
value: string,
newLine: ts.NewLine = { after: 'never' }
): ts.BlockStatement {
// The type of node here is not important since it's just being used as
// a placeholder for the comment. A BlockStatement just happens to be one
// of the simplest nodes to build.
Expand All @@ -11,5 +14,6 @@ export function comment(value: string): ts.BlockStatement {
type: AST_NODE_TYPES.BlockStatement,
comment: value,
body: [],
newLine,
}
}
18 changes: 5 additions & 13 deletions src/codegen/browser/formatting/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,11 @@ declare module 'prettier/plugins/estree' {
}

function applySpacing(node: ts.Node | null, doc: builders.Doc): builders.Doc {
if (node?.newLine === 'before') {
return [hardline, doc]
}

if (node?.newLine === 'after') {
return [doc, hardline]
}

if (node?.newLine === 'both') {
return [hardline, doc, hardline]
}

return doc
return [
node?.newLine?.before === true && hardline,
doc,
node?.newLine?.after === true && hardline,
].filter((item) => item !== false)
}

/**
Expand Down
55 changes: 40 additions & 15 deletions src/codegen/browser/formatting/spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,59 @@ import type { TSESTree as ts } from '@typescript-eslint/types'
export function trimBefore(
newLine: ts.NewLine | undefined
): ts.NewLine | undefined {
return newLine === 'both' ? 'after' : undefined
if (newLine === undefined) {
return undefined
}

return {
...newLine,
before: newLine.before === true ? false : newLine.before,
}
}

export function trimAfter(
newLine: ts.NewLine | undefined
): ts.NewLine | undefined {
return newLine === 'both' ? 'before' : undefined
if (newLine === undefined) {
return undefined
}

return {
...newLine,
after: newLine.after === true ? false : newLine.after,
}
}

export function mergeNewLine(
newLine: ts.NewLine | undefined,
target: ts.NewLine
target: ts.NewLine | undefined,
newLine: ts.NewLine
): ts.NewLine {
if (newLine === target) {
if (target === undefined) {
return newLine
}

if (newLine === undefined) {
return target
}
const before =
newLine.before !== undefined && target?.before === 'never'
? target?.before
: newLine.before

const after =
newLine.after !== undefined && target?.after === 'never'
? target?.after
: newLine.after

return 'both'
return { before, after }
Comment on lines +37 to +51
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merging logic doesn't preserve target properties when newLine doesn't specify them. For example, if target has before: 'never' and newLine is { after: true } (with before undefined), the result will have before=undefined, losing the 'never' constraint. The logic should preserve target values when newLine doesn't explicitly override them. Consider using: const before = newLine.before !== undefined ? (target.before === 'never' ? 'never' : newLine.before) : target.before and similarly for after. This ensures that 'never' constraints are always respected, and target values are preserved when not explicitly overridden.

Copilot uses AI. Check for mistakes.
}

export function spaceBetween<T extends ts.Node>(nodes: T[]) {
return nodes.map((node, index) => {
if (index === nodes.length - 1) {
const nextNode = nodes[index + 1]

if (nextNode === undefined || nextNode.newLine?.before === 'never') {
return node
}

return { ...node, newLine: mergeNewLine(node.newLine, 'after') }
return { ...node, newLine: mergeNewLine(node.newLine, { after: true }) }
})
}

Expand All @@ -46,7 +68,7 @@ export function spaceAfter<T extends ts.Node>(nodes: T[]): T[] {

return [
...nodes.slice(0, -1),
{ ...last, newLine: mergeNewLine(last.newLine, 'after') },
{ ...last, newLine: mergeNewLine(last.newLine, { after: true }) },
]
}

Expand All @@ -64,9 +86,9 @@ export function spaceAround<T extends ts.Node>([first, ...rest]: T[]): T[] {
const middle = rest.slice(0, -1)

return [
{ ...first, newLine: mergeNewLine(first.newLine, 'before') },
{ ...first, newLine: mergeNewLine(first.newLine, { before: true }) },
...middle,
{ ...last, newLine: mergeNewLine(last.newLine, 'after') },
{ ...last, newLine: mergeNewLine(last.newLine, { after: false }) },
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be { after: true } not { after: false }. The spaceAround function is supposed to add spacing before the first element and after the last element, but this line sets after to false, which would prevent adding a newline after the last element. This is inconsistent with the function's intended behavior and with line 89 which correctly sets before to true.

Copilot uses AI. Check for mistakes.
]
}

Expand All @@ -75,7 +97,10 @@ export function spaceBefore<T extends ts.Node>([first, ...rest]: T[]): T[] {
return []
}

return [{ ...first, newLine: mergeNewLine(first.newLine, 'before') }, ...rest]
return [
{ ...first, newLine: mergeNewLine(first.newLine, { before: true }) },
...rest,
]
}

export function trimSpacing<T extends ts.Node>([first, ...rest]: T[]): T[] {
Expand Down
12 changes: 11 additions & 1 deletion src/codegen/estree/typescript-estree.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
declare module '@typescript-eslint/types' {
namespace TSESTree {
type NewLine = 'before' | 'after' | 'both'
/**
* Options for specifying new line behavior around a node. Specifying
* `"never"` will prevent any new lines from being added in that position,
* even if other formatting rules would normally add them.
*/
type NewLineOption = boolean | 'never'

type NewLine = {
before?: NewLineOption
after?: NewLineOption
}

interface NodeOrTokenData {
/**
Expand Down
Loading