Files
oxc-project__oxc/crates/oxc_transformer/tests/integrations/comments.rs
Dunqing 0126aba1d9 fix(codegen): preserve orphaned file coverage comments (#24815)
File-level V8 and Istanbul coverage directives attach to the first AST statement. When TypeScript import removal or another upstream pass deletes that statement, codegen never visits the comment attachment position and silently drops the directive. Coverage tooling then instruments a file that was explicitly excluded.

The lexer now classifies `v8 ignore file` and `istanbul ignore file` separately, including line-comment forms and explanatory suffixes. Codegen lets those directives share the existing legal-comment orphan flush, preserving source order without scanning source text during printing. Position-sensitive directives such as `ignore next`, `start`, and `stop` remain unchanged.

The recovery path is named and documented around orphaned attachments. Hashbang ordering remains intact, and disabling annotation comments still suppresses these directives. The transformer regression stays in the Rust integration harness because transform conformance deliberately clears comments before codegen.

## Example

Input:

```ts
/* v8 ignore file */
import type { Foo } from "./types";

export default { typed: "stuff" } satisfies Foo;
```

Before:

```js
export default { typed: "stuff" };
```

After:

```js
/* v8 ignore file */
export default { typed: "stuff" };
```

Covered by direct codegen regressions for removed anchors, hashbang ordering, disabled annotation comments, and position-sensitive negative cases, plus a TypeScript transformer integration test. Verified with `env -u NO_COLOR just ready`, including all-feature tests, linting, documentation, generated-code checks, formatting, and a clean-tree check.

closes #23667

This PR was assisted by OpenAI Codex.
2026-07-24 15:26:48 +00:00

29 lines
1.1 KiB
Rust

use oxc_span::SourceType;
use oxc_transformer::TransformOptions;
use crate::test_with_source_type;
#[test]
fn preserve_file_coverage_comment_when_typescript_import_is_removed() {
// https://github.com/oxc-project/oxc/issues/23667
// Transform conformance clears comments before codegen, so this cross-cutting
// parser + transformer + codegen behavior needs the integration harness.
let cases = [
(
"/* v8 ignore file */\nimport unusedDefault, { unusedNamed } from './side-effects';\nexport default {};",
"/* v8 ignore file */\nexport default {};\n",
),
(
"/* v8 ignore file */\nimport type { Foo } from './types';\nexport default { typed: 'stuff' } satisfies Foo;",
"/* v8 ignore file */\nexport default { typed: 'stuff' };\n",
),
];
for (source_text, expected) in cases {
let output =
test_with_source_type(source_text, SourceType::ts(), &TransformOptions::default())
.expect("transform should succeed");
assert_eq!(output, expected);
}
}