Skip to content

Support import-only files with include rules#300

Open
jathak wants to merge 3 commits into
mainfrom
import-only-includes
Open

Support import-only files with include rules#300
jathak wants to merge 3 commits into
mainfrom
import-only-includes

Conversation

@jathak
Copy link
Copy Markdown
Member

@jathak jathak commented May 20, 2026

This enables migration of some nested/late imports that the migrator couldn't previously handle by first wrapping the file that's depended on via nested import in a mixin and then @includeing that mixin in the import-only file.

@jathak jathak requested a review from nex3 May 20, 2026 18:26
@jathak
Copy link
Copy Markdown
Member Author

jathak commented May 20, 2026

#301 should fix the CI here

This enables migration of some nested/late imports that the migrator
couldn't previously handle by first wrapping the file that's depended on
via nested import in a mixin and then `@include`ing that mixin in the
import-only file.
@jathak jathak force-pushed the import-only-includes branch from 094755e to c2f4ecf Compare May 21, 2026 13:56
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The tests that are exercising nested imports should probably mention that in their titles.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment on lines +714 to +717
throw MigrationSourceSpanException(
"Found a second @include rule in an import-only file. "
"Import-only files should contain at most one @include.",
node.span);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be possible to support multiple rules if we stored _importOnlyIncludes' values as lists?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I initially avoided it because I thought it would be complicated turning one @import rule into multiple @include rules, but I forgot we already had logic for turning one rule into multiple because an @import rule can contain multiple imports. Done.

Comment thread lib/src/migrators/module.dart Outdated
'${_namespaceForDeclaration(references.variables[arg]!)}.\$$name',
VariableExpression(:var name) => '\$$name',
_ => throw MigrationSourceSpanException(
'Import-only @include rules may only pass variables as arguments.',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this restriction? It seems like at the very least it wouldn't hurt to allow literal values, and really the only semantically difficult argument I can think of would be user-defined functions (which function more or less like variables anyway).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I initially restricted it to avoid having to recursively serialize all expressions (making sure to add any additional @use rules as necessary), but I ended up writing a serializer that can add/change namespaces as necessary, so that eliminates the need for this restriction.

jathak added 2 commits May 22, 2026 11:33
This adds a `NamespacedSerializer` utility class, which allows us to
copy the include rules with arbitrary arguments while still ensuring
that we use the right namespaces and add any additional use rules when
migrating.
Copy link
Copy Markdown
Member Author

@jathak jathak left a comment

Choose a reason for hiding this comment

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

Added NamespacedSerializer utility class to allow for arbitrary arguments to be copied from the @include rule in the import-only file. It does copy a fair bit of logic from toString() methods on some of the AST nodes, so let me know if there's a better way to implement this to avoid the duplicated code.

Comment on lines +714 to +717
throw MigrationSourceSpanException(
"Found a second @include rule in an import-only file. "
"Import-only files should contain at most one @include.",
node.span);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I initially avoided it because I thought it would be complicated turning one @import rule into multiple @include rules, but I forgot we already had logic for turning one rule into multiple because an @import rule can contain multiple imports. Done.

Comment thread lib/src/migrators/module.dart Outdated
'${_namespaceForDeclaration(references.variables[arg]!)}.\$$name',
VariableExpression(:var name) => '\$$name',
_ => throw MigrationSourceSpanException(
'Import-only @include rules may only pass variables as arguments.',
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I initially restricted it to avoid having to recursively serialize all expressions (making sure to add any additional @use rules as necessary), but I ended up writing a serializer that can add/change namespaces as necessary, so that eliminates the need for this restriction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants