Skip to content

Fix: Error checking of @use/@forward ... with... is too strict #2602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ final class Configuration {

bool get isEmpty => values.isEmpty;

int get length => values.length;

/// Removes a variable with [name] from this configuration, returning it.
///
/// If no such variable exists in this configuration, returns null.
Expand Down
63 changes: 36 additions & 27 deletions lib/src/visitor/async_evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -853,33 +853,8 @@ final class _EvaluateVisitor
bool namesInErrors = false,
}) async {
var url = stylesheet.span.sourceUrl;

if (_modules[url] case var alreadyLoaded?) {
var currentConfiguration = configuration ?? _configuration;
if (!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) &&
currentConfiguration is ExplicitConfiguration) {
var message = namesInErrors
? "${p.prettyUri(url)} was already loaded, so it can't be "
"configured using \"with\"."
: "This module was already loaded, so it can't be configured using "
"\"with\".";

var existingSpan = _moduleNodes[url]?.span;
var configurationSpan = configuration == null
? currentConfiguration.nodeWithSpan.span
: null;
var secondarySpans = {
if (existingSpan != null) existingSpan: "original load",
if (configurationSpan != null) configurationSpan: "configuration",
};

throw secondarySpans.isEmpty
? _exception(message)
: _multiSpanException(message, "new load", secondarySpans);
}

return alreadyLoaded;
}
var moduleUsedOverrides = false;
var currentConfiguration = configuration ?? _configuration;

var environment = AsyncEnvironment();
late CssStylesheet css;
Expand Down Expand Up @@ -916,12 +891,18 @@ final class _EvaluateVisitor
_inKeyframes = false;
if (configuration != null) _configuration = configuration;

// setup testing if the module accessed (& consumed) any configuration variables.
var configLength1 = _configuration.length;

await visitStylesheet(stylesheet);
css = _outOfOrderImports == null
? root
: CssStylesheet(_addOutOfOrderImports(), stylesheet.span);
preModuleComments = _preModuleComments;

// determine if the module accessed (& consumed) any configuration variables.
moduleUsedOverrides = configLength1 > _configuration.length;

_importer = oldImporter;
__stylesheet = oldStylesheet;
__root = oldRoot;
Expand All @@ -939,6 +920,34 @@ final class _EvaluateVisitor
_configuration = oldConfiguration;
});

if (_modules[url] case var alreadyLoaded?) {
// note: only throw an error if module used any of the config vars, otherwise treat it as if it was called without "with".
if (moduleUsedOverrides &&
!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) &&
currentConfiguration is ExplicitConfiguration) {
var message = namesInErrors
? "${p.prettyUri(url)} was already loaded, so it can't be "
"configured using \"with\"."
: "This module was already loaded, so it can't be configured using "
"\"with\".";

var existingSpan = _moduleNodes[url]?.span;
var configurationSpan = configuration == null
? currentConfiguration.nodeWithSpan.span
: null;
var secondarySpans = {
if (existingSpan != null) existingSpan: "original load",
if (configurationSpan != null) configurationSpan: "configuration",
};

throw secondarySpans.isEmpty
? _exception(message)
: _multiSpanException(message, "new load", secondarySpans);
}

return alreadyLoaded;
}

var module = environment.toModule(
css,
preModuleComments ?? const {},
Expand Down
65 changes: 37 additions & 28 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: a3068d04660dd2bed34b884aa6e1a21d423dc4e5
// Checksum: 4c0db6ff92d072b0165af02288d9d3a2782d137d
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -861,33 +861,8 @@ final class _EvaluateVisitor
bool namesInErrors = false,
}) {
var url = stylesheet.span.sourceUrl;

if (_modules[url] case var alreadyLoaded?) {
var currentConfiguration = configuration ?? _configuration;
if (!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) &&
currentConfiguration is ExplicitConfiguration) {
var message = namesInErrors
? "${p.prettyUri(url)} was already loaded, so it can't be "
"configured using \"with\"."
: "This module was already loaded, so it can't be configured using "
"\"with\".";

var existingSpan = _moduleNodes[url]?.span;
var configurationSpan = configuration == null
? currentConfiguration.nodeWithSpan.span
: null;
var secondarySpans = {
if (existingSpan != null) existingSpan: "original load",
if (configurationSpan != null) configurationSpan: "configuration",
};

throw secondarySpans.isEmpty
? _exception(message)
: _multiSpanException(message, "new load", secondarySpans);
}

return alreadyLoaded;
}
var moduleUsedOverrides = false;
var currentConfiguration = configuration ?? _configuration;

var environment = Environment();
late CssStylesheet css;
Expand Down Expand Up @@ -924,12 +899,18 @@ final class _EvaluateVisitor
_inKeyframes = false;
if (configuration != null) _configuration = configuration;

// setup testing if the module accessed (& consumed) any configuration variables.
var configLength1 = _configuration.length;

visitStylesheet(stylesheet);
css = _outOfOrderImports == null
? root
: CssStylesheet(_addOutOfOrderImports(), stylesheet.span);
preModuleComments = _preModuleComments;

// determine if the module accessed (& consumed) any configuration variables.
moduleUsedOverrides = configLength1 > _configuration.length;

_importer = oldImporter;
__stylesheet = oldStylesheet;
__root = oldRoot;
Expand All @@ -947,6 +928,34 @@ final class _EvaluateVisitor
_configuration = oldConfiguration;
});

if (_modules[url] case var alreadyLoaded?) {
// note: only throw an error if module used any of the config vars, otherwise treat it as if it was called without "with".
if (moduleUsedOverrides &&
!_moduleConfigurations[url]!.sameOriginal(currentConfiguration) &&
currentConfiguration is ExplicitConfiguration) {
var message = namesInErrors
? "${p.prettyUri(url)} was already loaded, so it can't be "
"configured using \"with\"."
: "This module was already loaded, so it can't be configured using "
"\"with\".";

var existingSpan = _moduleNodes[url]?.span;
var configurationSpan = configuration == null
? currentConfiguration.nodeWithSpan.span
: null;
var secondarySpans = {
if (existingSpan != null) existingSpan: "original load",
if (configurationSpan != null) configurationSpan: "configuration",
};

throw secondarySpans.isEmpty
? _exception(message)
: _multiSpanException(message, "new load", secondarySpans);
}

return alreadyLoaded;
}

var module = environment.toModule(
css,
preModuleComments ?? const {},
Expand Down