Skip to content

Commit

Permalink
dart run grind
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodwine committed Oct 16, 2023
1 parent 97d6111 commit 5783eac
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 76 deletions.
18 changes: 9 additions & 9 deletions lib/src/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import 'visitor/serialize.dart';
CompileResult compile(String path,
{Syntax? syntax,
Logger? logger,
ImportCache? importCache,
AsyncImportCache? importCache,
NodeImporter? nodeImporter,
Iterable<Callable>? functions,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool useSpaces = true,
int? indentWidth,
Expand Down Expand Up @@ -99,12 +99,12 @@ CompileResult compile(String path,
CompileResult compileString(String source,
{Syntax? syntax,
Logger? logger,
ImportCache? importCache,
AsyncImportCache? importCache,
NodeImporter? nodeImporter,
Iterable<Importer>? importers,
Iterable<AsyncImporter>? importers,
Iterable<String>? loadPaths,
Importer? importer,
Iterable<Callable>? functions,
AsyncImporter? importer,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool useSpaces = true,
int? indentWidth,
Expand Down Expand Up @@ -150,10 +150,10 @@ CompileResult compileString(String source,
CompileResult _compileStylesheet(
Stylesheet stylesheet,
Logger? logger,
ImportCache? importCache,
AsyncImportCache? importCache,
NodeImporter? nodeImporter,
Importer importer,
Iterable<Callable>? functions,
AsyncImporter importer,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool useSpaces,
int? indentWidth,
Expand Down
37 changes: 19 additions & 18 deletions lib/src/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class Environment {
///
/// The first element is the global scope, and each successive element is
/// deeper in the tree.
final List<Map<String, Callable>> _functions;
final List<Map<String, AsyncCallable>> _functions;

/// A map of function names to their indices in [_functions].
///
Expand All @@ -116,7 +116,7 @@ final class Environment {
///
/// The first element is the global scope, and each successive element is
/// deeper in the tree.
final List<Map<String, Callable>> _mixins;
final List<Map<String, AsyncCallable>> _mixins;

/// A map of mixin names to their indices in [_mixins].
///
Expand All @@ -125,8 +125,8 @@ final class Environment {

/// The content block passed to the lexically-enclosing mixin, or `null` if
/// this is not in a mixin, or if no content block was passed.
UserDefinedCallable<Environment>? get content => _content;
UserDefinedCallable<Environment>? _content;
UserDefinedCallable<AsyncEnvironment>? get content => _content;
UserDefinedCallable<AsyncEnvironment>? _content;

/// Whether the environment is lexically at the root of the document.
bool get atRoot => _variables.length == 1;
Expand Down Expand Up @@ -195,7 +195,7 @@ final class Environment {
/// Any scope changes in this environment will not affect the closure.
/// However, any new declarations or assignments in scopes that are visible
/// when the closure was created will be reflected.
Environment closure() => Environment._(
AsyncEnvironment closure() => Environment._(
_modules,
_namespaceNodes,
_globalModules,
Expand All @@ -214,7 +214,7 @@ final class Environment {
/// The returned environment shares this environment's variables, functions,
/// and mixins, but excludes most modules (except for global modules that
/// result from importing a file with forwards).
Environment forImport() => Environment._(
AsyncEnvironment forImport() => Environment._(
{},
{},
{},
Expand Down Expand Up @@ -620,7 +620,7 @@ final class Environment {
///
/// Throws a [SassScriptException] if there is no module named [namespace], or
/// if multiple global modules expose functions named [name].
Callable? getFunction(String name, {String? namespace}) {
AsyncCallable? getFunction(String name, {String? namespace}) {
if (namespace != null) return _getModule(namespace).functions[name];

if (_functionIndices[name] case var index?) {
Expand All @@ -636,7 +636,7 @@ final class Environment {
/// Returns the value of the function named [name] from a namespaceless
/// module, or `null` if no such function is declared in any namespaceless
/// module.
Callable? _getFunctionFromGlobalModule(String name) =>
AsyncCallable? _getFunctionFromGlobalModule(String name) =>
_fromOneModule(name, "function", (module) => module.functions[name]);

/// Returns the index of the last map in [_functions] that has a [name] key,
Expand All @@ -656,7 +656,7 @@ final class Environment {
getFunction(name, namespace: namespace) != null;

/// Sets the variable named [name] to [value] in the current scope.
void setFunction(Callable callable) {
void setFunction(AsyncCallable callable) {
var index = _functions.length - 1;
_functionIndices[callable.name] = index;
_functions[index][callable.name] = callable;
Expand All @@ -667,7 +667,7 @@ final class Environment {
///
/// Throws a [SassScriptException] if there is no module named [namespace], or
/// if multiple global modules expose mixins named [name].
Callable? getMixin(String name, {String? namespace}) {
AsyncCallable? getMixin(String name, {String? namespace}) {
if (namespace != null) return _getModule(namespace).mixins[name];

if (_mixinIndices[name] case var index?) {
Expand All @@ -683,7 +683,7 @@ final class Environment {
/// Returns the value of the mixin named [name] from a namespaceless
/// module, or `null` if no such mixin is declared in any namespaceless
/// module.
Callable? _getMixinFromGlobalModule(String name) =>
AsyncCallable? _getMixinFromGlobalModule(String name) =>
_fromOneModule(name, "mixin", (module) => module.mixins[name]);

/// Returns the index of the last map in [_mixins] that has a [name] key, or
Expand All @@ -703,14 +703,15 @@ final class Environment {
getMixin(name, namespace: namespace) != null;

/// Sets the variable named [name] to [value] in the current scope.
void setMixin(Callable callable) {
void setMixin(AsyncCallable callable) {
var index = _mixins.length - 1;
_mixinIndices[callable.name] = index;
_mixins[index][callable.name] = callable;
}

/// Sets [content] as [this.content] for the duration of [callback].
void withContent(UserDefinedCallable<Environment>? content, void callback()) {
void withContent(
UserDefinedCallable<AsyncEnvironment>? content, void callback()) {
var oldContent = _content;
_content = content;
callback();
Expand Down Expand Up @@ -860,7 +861,7 @@ final class Environment {
var valueInModule = callback(module);
if (valueInModule == null) continue;

Object? identityFromModule = valueInModule is Callable
Object? identityFromModule = valueInModule is AsyncCallable
? valueInModule
: module.variableIdentity(name);
if (identityFromModule == identity) continue;
Expand Down Expand Up @@ -888,16 +889,16 @@ final class _EnvironmentModule implements Module<Callable> {
final List<Module<Callable>> upstream;
final Map<String, Value> variables;
final Map<String, AstNode> variableNodes;
final Map<String, Callable> functions;
final Map<String, Callable> mixins;
final Map<String, AsyncCallable> functions;
final Map<String, AsyncCallable> mixins;
final ExtensionStore extensionStore;
final CssStylesheet css;
final Map<Module<Callable>, List<CssComment>> preModuleComments;
final bool transitivelyContainsCss;
final bool transitivelyContainsExtensions;

/// The environment that defines this module's members.
final Environment _environment;
final AsyncEnvironment _environment;

/// A map from variable names to the modules in which those variables appear,
/// used to determine where variables should be set.
Expand All @@ -908,7 +909,7 @@ final class _EnvironmentModule implements Module<Callable> {
final Map<String, Module<Callable>> _modulesByVariable;

factory _EnvironmentModule(
Environment environment,
AsyncEnvironment environment,
CssStylesheet css,
Map<Module<Callable>, List<CssComment>> preModuleComments,
ExtensionStore extensionStore,
Expand Down
33 changes: 19 additions & 14 deletions lib/src/import_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ import 'utils.dart';
///
/// This also includes the URL that was originally passed to the importer, which
/// may be resolved relative to a base URL.
typedef CanonicalizeResult = (Importer, Uri canonicalUrl, {Uri originalUrl});
typedef CanonicalizeResult = (
AsyncImporter,
Uri canonicalUrl, {
Uri originalUrl
});

/// An in-memory cache of parsed stylesheets that have been imported by Sass.
///
/// {@category Dependencies}
final class ImportCache {
/// The importers to use when loading new Sass files.
final List<Importer> _importers;
final List<AsyncImporter> _importers;

/// The logger to use to emit warnings when parsing stylesheets.
final Logger _logger;
Expand All @@ -49,7 +53,8 @@ final class ImportCache {
/// This cache isn't used for relative imports, because they depend on the
/// specific base importer. That's stored separately in
/// [_relativeCanonicalizeCache].
final _canonicalizeCache = <(Uri, {bool forImport}), CanonicalizeResult?>{};
final _canonicalizeCache =
<(Uri, {bool forImport}), AsyncCanonicalizeResult?>{};

/// The canonicalized URLs for each non-canonical URL that's resolved using a
/// relative importer.
Expand All @@ -65,10 +70,10 @@ final class ImportCache {
final _relativeCanonicalizeCache = <(
Uri, {
bool forImport,
Importer baseImporter,
AsyncImporter baseImporter,
Uri? baseUrl
}),
CanonicalizeResult?>{};
AsyncCanonicalizeResult?>{};

/// The parsed stylesheets for each canonicalized import URL.
final _importCache = <Uri, Stylesheet?>{};
Expand All @@ -94,7 +99,7 @@ final class ImportCache {
///
/// [`PackageConfig`]: https://pub.dev/documentation/package_config/latest/package_config.package_config/PackageConfig-class.html
ImportCache(
{Iterable<Importer>? importers,
{Iterable<AsyncImporter>? importers,
Iterable<String>? loadPaths,
PackageConfig? packageConfig,
Logger? logger})
Expand All @@ -108,7 +113,7 @@ final class ImportCache {

/// Converts the user's [importers], [loadPaths], and [packageConfig]
/// options into a single list of importers.
static List<Importer> _toImporters(Iterable<Importer>? importers,
static List<AsyncImporter> _toImporters(Iterable<AsyncImporter>? importers,
Iterable<String>? loadPaths, PackageConfig? packageConfig) {
var sassPath = getEnvironmentVariable('SASS_PATH');
if (isBrowser) return [...?importers];
Expand Down Expand Up @@ -138,8 +143,8 @@ final class ImportCache {
/// If any importers understand [url], returns that importer as well as the
/// canonicalized URL and the original URL (resolved relative to [baseUrl] if
/// applicable). Otherwise, returns `null`.
CanonicalizeResult? canonicalize(Uri url,
{Importer? baseImporter, Uri? baseUrl, bool forImport = false}) {
AsyncCanonicalizeResult? canonicalize(Uri url,
{AsyncImporter? baseImporter, Uri? baseUrl, bool forImport = false}) {
if (isBrowser &&
(baseImporter == null || baseImporter is NoOpImporter) &&
_importers.isEmpty) {
Expand Down Expand Up @@ -176,8 +181,8 @@ final class ImportCache {
///
/// If [resolveUrl] is `true`, this resolves [url] relative to [baseUrl]
/// before passing it to [importer].
CanonicalizeResult? _canonicalize(
Importer importer, Uri url, Uri? baseUrl, bool forImport,
AsyncCanonicalizeResult? _canonicalize(
AsyncImporter importer, Uri url, Uri? baseUrl, bool forImport,
{bool resolveUrl = false}) {
var resolved =
resolveUrl && baseUrl != null ? baseUrl.resolveUri(url) : url;
Expand Down Expand Up @@ -214,8 +219,8 @@ final class ImportCache {
/// parsed stylesheet. Otherwise, returns `null`.
///
/// Caches the result of the import and uses cached results if possible.
(Importer, Stylesheet)? import(Uri url,
{Importer? baseImporter, Uri? baseUrl, bool forImport = false}) {
(AsyncImporter, Stylesheet)? import(Uri url,
{AsyncImporter? baseImporter, Uri? baseUrl, bool forImport = false}) {
if (canonicalize(url,
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport)
case (var importer, var canonicalUrl, :var originalUrl)) {
Expand All @@ -239,7 +244,7 @@ final class ImportCache {
/// newly imported stylesheet.
///
/// Caches the result of the import and uses cached results if possible.
Stylesheet? importCanonical(Importer importer, Uri canonicalUrl,
Stylesheet? importCanonical(AsyncImporter importer, Uri canonicalUrl,
{Uri? originalUrl, bool quiet = false}) {
return _importCache.putIfAbsent(canonicalUrl, () {
var result = importer.load(canonicalUrl);
Expand Down
Loading

0 comments on commit 5783eac

Please sign in to comment.