Skip to content

Parsing import allows "as" before "if" #56641

Open
@munificent

Description

@munificent

I don't get any errors in:

import 'foo.dart' as foo if (dart.library.html) 'other_foo.dart';

main() {
  print(foo.bar);
}

This runs if I run it with dart temp.dart and analyzes without error using dart analyze. The grammar for imports is:

⟨importSpecification⟩ ::=
import ⟨configurableUri⟩ (deferred? as ⟨typeIdentifier⟩)? ⟨combinator⟩*
‘;’

That means that the as clause, if present, must be after any if configurations.

I ran into this issue because dart format will always output the as clause after the if configurations, which then trips up its sanity check because it made a non-whitespace change by reordering those clauses.

I did find at least two libraries in the wild (dotlottie_loader-0.0.4/lib/src/loaders/network_loader.dart and sentry-8.3.0/lib/src/environment/environment_variables.dart) that has the clauses in the wrong order, so fixing this is both nominally and actually a breaking change (though likely a very minor one given the rarity of conditional imports).

Metadata

Metadata

Assignees

Labels

area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-parserParser issues in the analyzer/CFEtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions