ronantakizawa/codereview-bench huggingface dataset:
Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/cpu/tensor/grid_sample.cc (C++)

Code before:
    }
    if (x1 >= 0 && x1 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomLeftMask;
    }
    if (x2 >= 0 && x2 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomRightMask;
    }

    plan.x1 = x1;
    plan.x2 = x2;
    plan.y1 = y1;
    plan.y2 = y2;
    plan.w11 = dy2 * dx2;
    plan.w12 = dy2 * dx1;
    plan.w21 = dy1 * dx2;
    plan.w22 = dy1 * dx1;
    plan.mask = mask;
  }
}

template <typename T>
void EvaluatePlanForChannel(const T* input_data,
                            T* output_data,
                            int64_t W_in,
                            const BilinearSamplePlan2D<T>* plan_data,
                            int64_t point_count) {
  for (size_t idx = 0; idx < static_cast<size_t>(point_count); ++idx) {
    const auto& plan = plan_data[idx];
    if (plan.mask == 0) {
      output_data[idx] = T{};
      continue;
    }

    T p11 = T{};
    T p12 = T{};
    T p21 = T{};
    T p22 = T{};

    if (plan.mask == kAllNeighborsMask) {
      const int64_t row1 = plan.y1 * W_in;
      const int64_t row2 = plan.y2 * W_in;
      p11 = input_data[row1 + plan.x1];
      p12 = input_data[row1 + plan.x2];
      p21 = input_data[row2 + plan.x1];
      p22 = input_data[row2 + plan.x2];
    } else {
      if (plan.mask & kTopLeftMask) {
        p11 = input_data[plan.y1 * W_in + plan.x1];
      }
      if (plan.mask & kTopRightMask) {
        p12 = input_data[plan.y1 * W_in + plan.x2];

Review comment (suggestion):
```suggestion
                            size_t point_count) {
```

Code after:
    }
    if (x1 >= 0 && x1 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomLeftMask;
    }
    if (x2 >= 0 && x2 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomRightMask;
    }

    plan.x1 = x1;
    plan.x2 = x2;
    plan.y1 = y1;
    plan.y2 = y2;
    plan.w11 = dy2 * dx2;
    plan.w12 = dy2 * dx1;
    plan.w21 = dy1 * dx2;
    plan.w22 = dy1 * dx1;
    plan.mask = mask;
  }
}

template <typename T>
void EvaluatePlanForChannel(const T* input_data,
                            T* output_data,
                            int64_t W_in,
                            const BilinearSamplePlan2D<T>* plan_data,
                            size_t point_count) {
  for (size_t idx = 0; idx < point_count; ++idx) {
    const auto& plan = plan_data[idx];
    if (plan.mask == 0) {
      output_data[idx] = T{};
      continue;
    }

    T p11 = T{};
    T p12 = T{};
    T p21 = T{};
    T p22 = T{};

    if (plan.mask == kAllNeighborsMask) {
      const int64_t row1 = plan.y1 * W_in;
      const int64_t row2 = plan.y2 * W_in;
      p11 = input_data[row1 + plan.x1];
      p12 = input_data[row1 + plan.x2];
      p21 = input_data[row2 + plan.x1];
      p22 = input_data[row2 + plan.x2];
    } else {
      if (plan.mask & kTopLeftMask) {
        p11 = input_data[plan.y1 * W_in + plan.x1];
      }
      if (plan.mask & kTopRightMask) {
        p12 = input_data[plan.y1 * W_in + plan.x2];
Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/cpu/tensor/grid_sample.cc (C++)

Code before:
    if (x1 >= 0 && x1 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomLeftMask;
    }
    if (x2 >= 0 && x2 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomRightMask;
    }

    plan.x1 = x1;
    plan.x2 = x2;
    plan.y1 = y1;
    plan.y2 = y2;
    plan.w11 = dy2 * dx2;
    plan.w12 = dy2 * dx1;
    plan.w21 = dy1 * dx2;
    plan.w22 = dy1 * dx1;
    plan.mask = mask;
  }
}

template <typename T>
void EvaluatePlanForChannel(const T* input_data,
                            T* output_data,
                            int64_t W_in,
                            const BilinearSamplePlan2D<T>* plan_data,
                            int64_t point_count) {
  for (size_t idx = 0; idx < static_cast<size_t>(point_count); ++idx) {
    const auto& plan = plan_data[idx];
    if (plan.mask == 0) {
      output_data[idx] = T{};
      continue;
    }

    T p11 = T{};
    T p12 = T{};
    T p21 = T{};
    T p22 = T{};

    if (plan.mask == kAllNeighborsMask) {
      const int64_t row1 = plan.y1 * W_in;
      const int64_t row2 = plan.y2 * W_in;
      p11 = input_data[row1 + plan.x1];
      p12 = input_data[row1 + plan.x2];
      p21 = input_data[row2 + plan.x1];
      p22 = input_data[row2 + plan.x2];
    } else {
      if (plan.mask & kTopLeftMask) {
        p11 = input_data[plan.y1 * W_in + plan.x1];
      }
      if (plan.mask & kTopRightMask) {
        p12 = input_data[plan.y1 * W_in + plan.x2];
      }

Review comment (suggestion):
```suggestion
  for (size_t idx = 0; idx < point_count; ++idx) {
```

Code after:
    if (x1 >= 0 && x1 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomLeftMask;
    }
    if (x2 >= 0 && x2 < W_in && y2 >= 0 && y2 < H_in) {
      mask |= kBottomRightMask;
    }

    plan.x1 = x1;
    plan.x2 = x2;
    plan.y1 = y1;
    plan.y2 = y2;
    plan.w11 = dy2 * dx2;
    plan.w12 = dy2 * dx1;
    plan.w21 = dy1 * dx2;
    plan.w22 = dy1 * dx1;
    plan.mask = mask;
  }
}

template <typename T>
void EvaluatePlanForChannel(const T* input_data,
                            T* output_data,
                            int64_t W_in,
                            const BilinearSamplePlan2D<T>* plan_data,
                            size_t point_count) {
  for (size_t idx = 0; idx < point_count; ++idx) {
    const auto& plan = plan_data[idx];
    if (plan.mask == 0) {
      output_data[idx] = T{};
      continue;
    }

    T p11 = T{};
    T p12 = T{};
    T p21 = T{};
    T p22 = T{};

    if (plan.mask == kAllNeighborsMask) {
      const int64_t row1 = plan.y1 * W_in;
      const int64_t row2 = plan.y2 * W_in;
      p11 = input_data[row1 + plan.x1];
      p12 = input_data[row1 + plan.x2];
      p21 = input_data[row2 + plan.x1];
      p22 = input_data[row2 + plan.x2];
    } else {
      if (plan.mask & kTopLeftMask) {
        p11 = input_data[plan.y1 * W_in + plan.x1];
      }
      if (plan.mask & kTopRightMask) {
        p12 = input_data[plan.y1 * W_in + plan.x2];
      }
Repository: microsoft/playwright
File: tests/playwright-test/reporter-attachment.spec.ts (TypeScript)

Code before:
    'a.test.ts': `
      import { test as base } from '@playwright/test';

      const test = base.extend<{ myFixture: void }>({
        myFixture: [async ({}, use, testInfo) => {
          await testInfo.attach('my attachment', {
            body: 'foo',
            contentType: 'text/plain',
          });
          await use();
        }, { box: true }],
      });

      test('my test', ({ myFixture }) => {
        expect(1).toBe(0);
      });
    `,
  }, { reporter: 'line' }, {});
  const text = result.output;
  expect(text).toContain('    attachment #1: my attachment (text/plain) ──────────────────────────────────────────────────────');
  expect(text).toContain('    foo');
  expect(text).toContain('    ────────────────────────────────────────────────────────────────────────────────────────────────');
  expect(result.exitCode).toBe(1);
});

test('trace attaching should not log error', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37747' } }, async ({ runInlineTest }) => {
  const result = await runInlineTest({
    'setup.test.ts': `
      import { test as setup } from "@playwright/test";

      setup("setup", async ({ page }) => {
        await page.context().close();
      });
    `,
    'a.test.ts': `
      import { test, expect } from '@playwright/test';

      test('my test', ({ page }) => {
        expect(1).toBe(0);
      });
    `,
    'playwright.config.ts': `
      import { defineConfig } from '@playwright/test';
      export default defineConfig({
        use: {
          screenshot: 'on',
          trace: 'on',
        },
        projects: [
          { name: 'setup', testMatch: /setup\\.test\\.ts/ },
          { name: 'frontend', testMatch: /a\\.test\\.ts/, dependencies: ['setup'] },

Review comment (bug):
I don't really follow how this test is related to the bugfix :) I thought you just need to call `testInfo.attachments.push()` from inside a boxed fixture.

Code after:
    'a.test.ts': `
      import { test as base } from '@playwright/test';

      const test = base.extend<{ myFixture: void }>({
        myFixture: [async ({}, use, testInfo) => {
          await testInfo.attach('my attachment', {
            body: 'foo',
            contentType: 'text/plain',
          });
          testInfo.attachments.push({
            name: 'my attachment 2',
            body: Buffer.from('bar'),
            contentType: 'text/plain',
          });
          await use();
        }, { box: true }],
      });

      test('my test', ({ myFixture }) => {
        expect(1).toBe(0);
      });
    `,
  }, { reporter: 'line' }, {});
  expect(result.output).not.toContain('step id not found');
  expect(result.exitCode).toBe(1);
});
Repository: microsoft/playwright
File: packages/playwright/src/common/validators.ts (TypeScript)

Code before:
    return testAnnotationSchema.parse(annotation);
  } catch (error) {
    throwZodError(error);
  }
}

type ValidTestDetails = {
  tags: string[];
  annotations: (TestDetailsAnnotation & { location: Location })[];
  location: Location;
};

export function validateTestDetails(details: unknown, location: Location): ValidTestDetails {
  try {
    const parsedDetails = testDetailsSchema.parse(details);
    return {
      annotations: parsedDetails.annotation.map(a => ({ ...a, location })),
      tags: parsedDetails.tag,
      location,
    };
  } catch (error) {
    throwZodError(error);
  }
}

export function throwZodError(error: any): never {
  throw new Error((error as ZodError).issues.map(i => i.message).join('\n'));
}


Review comment (suggestion):
Check that error is `instanceof ZodError` if you export this function.

Code after:
    return testAnnotationSchema.parse(annotation);
  } catch (error) {
    throwZodError(error);
  }
}

type ValidTestDetails = {
  tags: string[];
  annotations: (TestDetailsAnnotation & { location: Location })[];
  location: Location;
};

export function validateTestDetails(details: unknown, location: Location): ValidTestDetails {
  try {
    const parsedDetails = testDetailsSchema.parse(details);
    return {
      annotations: parsedDetails.annotation.map(a => ({ ...a, location })),
      tags: parsedDetails.tag,
      location,
    };
  } catch (error) {
    throwZodError(error);
  }
}

function throwZodError(error: any): never {
  throw new Error((error as ZodError).issues.map(i => i.message).join('\n'));
}
Repository: microsoft/playwright
File: packages/html-reporter/src/utils.ts (TypeScript)

Code before:
    return minutes.toFixed(1) + 'm';

  const hours = minutes / 60;
  if (hours < 24)
    return hours.toFixed(1) + 'h';

  const days = hours / 24;
  return days.toFixed(1) + 'd';
}

// hash string to integer in range [0, 6] for color index, to get same color for same tag
export function hashStringToInt(str: string) {
  let hash = 0;
  for (let i = 0; i < str.length; i++)
    hash = str.charCodeAt(i) + ((hash << 8) - hash);
  return Math.abs(hash % 6);
}

export function formatUrl(url: string): string;
export function formatUrl(url: string | undefined): string | undefined;
export function formatUrl(url: string | undefined): string | undefined {
  if (!url)
    return url;
  try {
    const parsed = new URL(url, window.location.href);
    if (parsed.protocol === window.location.protocol && parsed.origin === window.location.origin) {
      for (const [key, value] of new URLSearchParams(window.location.search))
        parsed.searchParams.append(key, value);
      return parsed.toString();
    }
    return url;
  } catch {
    return url;
  }
}


Review comment (suggestion):
origin includes protocol
```suggestion
    if (parsed.origin === window.location.origin) {
```

Code after:
    return minutes.toFixed(1) + 'm';

  const hours = minutes / 60;
  if (hours < 24)
    return hours.toFixed(1) + 'h';

  const days = hours / 24;
  return days.toFixed(1) + 'd';
}

// hash string to integer in range [0, 6] for color index, to get same color for same tag
export function hashStringToInt(str: string) {
  let hash = 0;
  for (let i = 0; i < str.length; i++)
    hash = str.charCodeAt(i) + ((hash << 8) - hash);
  return Math.abs(hash % 6);
}

// Preserves any query parameters that could be used for serving the report from storage,
// or for anything else. We don't use any query params in the report anyway.
export function formatUrl(url: string): string;
export function formatUrl(url: string | undefined): string | undefined;
export function formatUrl(url: string | undefined): string | undefined {
  if (!url)
    return url;
  try {
    const parsed = new URL(url, window.location.href);
    if (parsed.origin === window.location.origin) {
      for (const [key, value] of new URLSearchParams(window.location.search))
        parsed.searchParams.append(key, value);
      return parsed.toString();
    }
    return url;
  } catch {
    return url;
  }
}
Repository: microsoft/playwright
File: packages/html-reporter/src/utils.ts (TypeScript)

Code before:

  const minutes = seconds / 60;
  if (minutes < 60)
    return minutes.toFixed(1) + 'm';

  const hours = minutes / 60;
  if (hours < 24)
    return hours.toFixed(1) + 'h';

  const days = hours / 24;
  return days.toFixed(1) + 'd';
}

// hash string to integer in range [0, 6] for color index, to get same color for same tag
export function hashStringToInt(str: string) {
  let hash = 0;
  for (let i = 0; i < str.length; i++)
    hash = str.charCodeAt(i) + ((hash << 8) - hash);
  return Math.abs(hash % 6);
}

export function formatUrl(url: string): string;
export function formatUrl(url: string | undefined): string | undefined;
export function formatUrl(url: string | undefined): string | undefined {
  if (!url)
    return url;
  try {
    const parsed = new URL(url, window.location.href);
    if (parsed.protocol === window.location.protocol && parsed.origin === window.location.origin) {
      for (const [key, value] of new URLSearchParams(window.location.search))
        parsed.searchParams.append(key, value);
      return parsed.toString();
    }
    return url;
  } catch {
    return url;
  }
}


Review comment (security):
```suggestion
// we don't use query params for our own behaviour, but users might be using them to authenticate their blobstore for example.
// ensure we don't drop them on links
export function formatUrl(url: string): string;
export function formatUrl(url: string | undefined): string | undefined;
export function formatUrl(url: string | undefined): string | undefined {
```

Code after:

  const minutes = seconds / 60;
  if (minutes < 60)
    return minutes.toFixed(1) + 'm';

  const hours = minutes / 60;
  if (hours < 24)
    return hours.toFixed(1) + 'h';

  const days = hours / 24;
  return days.toFixed(1) + 'd';
}

// hash string to integer in range [0, 6] for color index, to get same color for same tag
export function hashStringToInt(str: string) {
  let hash = 0;
  for (let i = 0; i < str.length; i++)
    hash = str.charCodeAt(i) + ((hash << 8) - hash);
  return Math.abs(hash % 6);
}

// Preserves any query parameters that could be used for serving the report from storage,
// or for anything else. We don't use any query params in the report anyway.
export function formatUrl(url: string): string;
export function formatUrl(url: string | undefined): string | undefined;
export function formatUrl(url: string | undefined): string | undefined {
  if (!url)
    return url;
  try {
    const parsed = new URL(url, window.location.href);
    if (parsed.origin === window.location.origin) {
      for (const [key, value] of new URLSearchParams(window.location.search))
        parsed.searchParams.append(key, value);
      return parsed.toString();
    }
    return url;
  } catch {
    return url;
  }
}
Repository: microsoft/playwright
File: tests/page/frame-frame-element.spec.ts (TypeScript)

Code before:
      const iframe = document.createElement('iframe');
      iframe.setAttribute('name', 'myframe');
      iframe.setAttribute('srcdoc', 'find me');
      const div = document.getElementById('framecontainer');
      const host = div.attachShadow({ mode: 'closed' });
      host.appendChild(iframe);
    </script>
  `);

  const frame = await findFrameByName(page, 'myframe');
  const element = await frame.frameElement();
  expect(await element.getAttribute('name')).toBe('myframe');
});

it('should work inside declarative shadow root', async ({ page, server, browserName }) => {
  await page.goto(server.EMPTY_PAGE);
  await page.setContent(`
    <div>
      <template shadowrootmode="open">
        <iframe name="myframe" srcdoc="<h1>Hi!</h1>"></iframe>
        <slot></slot>
      </template>
      <span>footer</span>
    </div>
  `);
  const frame = await findFrameByName(page, 'myframe');
  const element = await frame.frameElement();
  expect(await element.getAttribute('name')).toBe('myframe');
});


Review comment (suggestion):
```suggestion
  const frame = page.frames()[1];
```

Code after:
      const iframe = document.createElement('iframe');
      iframe.setAttribute('name', 'myframe');
      iframe.setAttribute('srcdoc', 'find me');
      const div = document.getElementById('framecontainer');
      const host = div.attachShadow({ mode: 'closed' });
      host.appendChild(iframe);
    </script>
  `);

  const frame = page.frames()[1];
  const element = await frame.frameElement();
  expect(await element.getAttribute('name')).toBe('myframe');
});

it('should work inside declarative shadow root', async ({ page, server, browserName }) => {
  await page.goto(server.EMPTY_PAGE);
  await page.setContent(`
    <div>
      <template shadowrootmode="open">
        <iframe name="myframe" srcdoc="<h1>Hi!</h1>"></iframe>
        <slot></slot>
      </template>
      <span>footer</span>
    </div>
  `);
  const frame = page.frames()[1];
  const element = await frame.frameElement();
  expect(await element.getAttribute('name')).toBe('myframe');
});
Repository: microsoft/playwright
File: packages/playwright/src/mcp/browser/config.ts (TypeScript)

Code before:
      ...pickDefined(overrides.browser?.launchOptions),
      ...{ assistantMode: true },
    },
    contextOptions: {
      ...pickDefined(base.browser?.contextOptions),
      ...pickDefined(overrides.browser?.contextOptions),
    },
  };

  if (browser.browserName !== 'chromium' && browser.launchOptions)
    delete browser.launchOptions.channel;

  return {
    ...pickDefined(base),
    ...pickDefined(overrides),
    browser,
    network: {
      ...pickDefined(base.network),
      ...pickDefined(overrides.network),
    },
    server: {
      ...pickDefined(base.server),
      ...pickDefined(overrides.server),
    },
    snapshot: {
      ...pickDefined(base?.snapshot),
      ...pickDefined(overrides?.snapshot),
    },
    timeouts: {
      ...pickDefined(base.timeouts),
      ...pickDefined(overrides.timeouts),
    },
  } as FullConfig;
}

export function semicolonSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(';').map(v => v.trim());
}

export function commaSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(',').map(v => v.trim());
}

export function dotenvFileLoader(value: string | undefined): Record<string, string> | undefined {
  if (!value)
    return undefined;
  return dotenv.parse(fs.readFileSync(value, 'utf8'));

Review comment (suggestion):
```suggestion
      ...pickDefined(base.snapshot),
```

Code after:
      ...pickDefined(overrides.browser?.launchOptions),
      ...{ assistantMode: true },
    },
    contextOptions: {
      ...pickDefined(base.browser?.contextOptions),
      ...pickDefined(overrides.browser?.contextOptions),
    },
  };

  if (browser.browserName !== 'chromium' && browser.launchOptions)
    delete browser.launchOptions.channel;

  return {
    ...pickDefined(base),
    ...pickDefined(overrides),
    browser,
    network: {
      ...pickDefined(base.network),
      ...pickDefined(overrides.network),
    },
    server: {
      ...pickDefined(base.server),
      ...pickDefined(overrides.server),
    },
    snapshot: {
      ...pickDefined(base.snapshot),
      ...pickDefined(overrides.snapshot),
    },
    timeouts: {
      ...pickDefined(base.timeouts),
      ...pickDefined(overrides.timeouts),
    },
  } as FullConfig;
}

export function semicolonSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(';').map(v => v.trim());
}

export function commaSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(',').map(v => v.trim());
}

export function dotenvFileLoader(value: string | undefined): Record<string, string> | undefined {
  if (!value)
    return undefined;
  return dotenv.parse(fs.readFileSync(value, 'utf8'));
Repository: microsoft/playwright
File: packages/playwright/src/mcp/browser/config.ts (TypeScript)

Code before:
      ...{ assistantMode: true },
    },
    contextOptions: {
      ...pickDefined(base.browser?.contextOptions),
      ...pickDefined(overrides.browser?.contextOptions),
    },
  };

  if (browser.browserName !== 'chromium' && browser.launchOptions)
    delete browser.launchOptions.channel;

  return {
    ...pickDefined(base),
    ...pickDefined(overrides),
    browser,
    network: {
      ...pickDefined(base.network),
      ...pickDefined(overrides.network),
    },
    server: {
      ...pickDefined(base.server),
      ...pickDefined(overrides.server),
    },
    snapshot: {
      ...pickDefined(base?.snapshot),
      ...pickDefined(overrides?.snapshot),
    },
    timeouts: {
      ...pickDefined(base.timeouts),
      ...pickDefined(overrides.timeouts),
    },
  } as FullConfig;
}

export function semicolonSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(';').map(v => v.trim());
}

export function commaSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(',').map(v => v.trim());
}

export function dotenvFileLoader(value: string | undefined): Record<string, string> | undefined {
  if (!value)
    return undefined;
  return dotenv.parse(fs.readFileSync(value, 'utf8'));
}

Review comment (suggestion):
```suggestion
      ...pickDefined(overrides.snapshot),
```

Code after:
      ...{ assistantMode: true },
    },
    contextOptions: {
      ...pickDefined(base.browser?.contextOptions),
      ...pickDefined(overrides.browser?.contextOptions),
    },
  };

  if (browser.browserName !== 'chromium' && browser.launchOptions)
    delete browser.launchOptions.channel;

  return {
    ...pickDefined(base),
    ...pickDefined(overrides),
    browser,
    network: {
      ...pickDefined(base.network),
      ...pickDefined(overrides.network),
    },
    server: {
      ...pickDefined(base.server),
      ...pickDefined(overrides.server),
    },
    snapshot: {
      ...pickDefined(base.snapshot),
      ...pickDefined(overrides.snapshot),
    },
    timeouts: {
      ...pickDefined(base.timeouts),
      ...pickDefined(overrides.timeouts),
    },
  } as FullConfig;
}

export function semicolonSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(';').map(v => v.trim());
}

export function commaSeparatedList(value: string | undefined): string[] | undefined {
  if (!value)
    return undefined;
  return value.split(',').map(v => v.trim());
}

export function dotenvFileLoader(value: string | undefined): Record<string, string> | undefined {
  if (!value)
    return undefined;
  return dotenv.parse(fs.readFileSync(value, 'utf8'));
}