Skip to content

[code_builder] Scoped DartEmitter emits lint ignore after library annotations/directives #2404

Closed
dart-lang/web
#576

Description

@kevmoo

When generating a Library that contains top-level annotations (such as @JS()) using DartEmitter.scoped, code_builder automatically injects // ignore_for_file: no_leading_underscores_for_library_prefixes. However, because it injects this comment at the head of the directives block rather than at the absolute head of the file, the lint comment appears after the library annotations and directive. dart_style then collapses the comment onto the same line as library;.

Reproduction:

import 'package:code_builder/code_builder.dart';

void main() {
  final lib = Library((l) {
    l.annotations.add(refer('JS', 'dart:js_interop').call([literalString('vscode')]));
    l.body.add(Method((m) => m..name = 'foo'..external = true));
  });

  final emitter = DartEmitter.scoped();
  print(lib.accept(emitter));
}

Actual Output:

@_i1.JS('vscode')
library; // ignore_for_file: no_leading_underscores_for_library_prefixes

import 'dart:js_interop' as _i1;

external void foo();

Expected Output:

// ignore_for_file: no_leading_underscores_for_library_prefixes

@_i1.JS('vscode')
library;

import 'dart:js_interop' as _i1;

external void foo();

Suggested Fix:
Update DartEmitter.visitLibrary so that scoped lint ignores are injected at the very top of the emitted buffer before visiting library annotations and the library directive.


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions