From a09a62538e696488480cc320da09ac56a516d5ed Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Tue, 11 Dec 2018 17:53:46 -0800 Subject: [PATCH] Remove extra importer parameters from internal compile functions These can be passed as an ImportCache now, so they make the API more confusing for relatively little benefit. This also changes the const AsyncImportCache.none field to a non-const AsyncImportCache.none() constructor. The const field didn't make sense for a couple reasons: first, it wouldn't use a user's custom logger; and second, it couldn't cache any relative imports, which were still possible when the initial file was loaded from disk. --- lib/sass.dart | 42 ++++++++++++++++++----------- lib/src/async_compile.dart | 25 +++-------------- lib/src/async_import_cache.dart | 10 ++++--- lib/src/compile.dart | 27 +++---------------- lib/src/import_cache.dart | 12 ++++++--- lib/src/visitor/async_evaluate.dart | 13 +++++---- lib/src/visitor/evaluate.dart | 15 ++++++----- 7 files changed, 65 insertions(+), 79 deletions(-) diff --git a/lib/sass.dart b/lib/sass.dart index 3429db90..7967196a 100644 --- a/lib/sass.dart +++ b/lib/sass.dart @@ -9,9 +9,11 @@ import 'dart:async'; import 'package:source_maps/source_maps.dart'; +import 'src/async_import_cache.dart'; import 'src/callable.dart'; import 'src/compile.dart' as c; import 'src/exception.dart'; +import 'src/import_cache.dart'; import 'src/importer.dart'; import 'src/logger.dart'; import 'src/sync_package_resolver.dart'; @@ -86,11 +88,13 @@ String compile(String path, Iterable functions, OutputStyle style, void sourceMap(SingleMapping map)}) { + logger ??= Logger.stderr(color: color); var result = c.compile(path, - logger: logger ?? Logger.stderr(color: color), - importers: importers, - loadPaths: loadPaths, - packageResolver: packageResolver, + logger: logger, + importCache: ImportCache(importers, + logger: logger, + loadPaths: loadPaths, + packageResolver: packageResolver), functions: functions, style: style, sourceMap: sourceMap != null); @@ -167,12 +171,14 @@ String compileString(String source, url, void sourceMap(SingleMapping map), @Deprecated("Use syntax instead.") bool indented = false}) { + logger ??= Logger.stderr(color: color); var result = c.compileString(source, syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss), - logger: logger ?? Logger.stderr(color: color), - importers: importers, - packageResolver: packageResolver, - loadPaths: loadPaths, + logger: logger, + importCache: ImportCache(importers, + logger: logger, + packageResolver: packageResolver, + loadPaths: loadPaths), functions: functions, style: style, importer: importer, @@ -196,11 +202,13 @@ Future compileAsync(String path, Iterable functions, OutputStyle style, void sourceMap(SingleMapping map)}) async { + logger ??= Logger.stderr(color: color); var result = await c.compileAsync(path, - logger: logger ?? Logger.stderr(color: color), - importers: importers, - loadPaths: loadPaths, - packageResolver: packageResolver, + logger: logger, + importCache: AsyncImportCache(importers, + logger: logger, + loadPaths: loadPaths, + packageResolver: packageResolver), functions: functions, style: style, sourceMap: sourceMap != null); @@ -226,12 +234,14 @@ Future compileStringAsync(String source, url, void sourceMap(SingleMapping map), @Deprecated("Use syntax instead.") bool indented = false}) async { + logger ??= Logger.stderr(color: color); var result = await c.compileStringAsync(source, syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss), - logger: logger ?? Logger.stderr(color: color), - importers: importers, - packageResolver: packageResolver, - loadPaths: loadPaths, + logger: logger, + importCache: AsyncImportCache(importers, + logger: logger, + packageResolver: packageResolver, + loadPaths: loadPaths), functions: functions, style: style, importer: importer, diff --git a/lib/src/async_compile.dart b/lib/src/async_compile.dart index 41c8c4bf..b300590e 100644 --- a/lib/src/async_compile.dart +++ b/lib/src/async_compile.dart @@ -23,20 +23,12 @@ import 'visitor/serialize.dart'; /// Like [compileAsync] in `lib/sass.dart`, but provides more options to support /// the node-sass compatible API and the executable. /// -/// No more than one of the following arguments or sets of arguments may be -/// provided: -/// -/// * `importCache`; -/// * `nodeImporter`; -/// * `importers`, `packageResolver`, and/or `loadPaths`. +/// At most one of `importCache` and `nodeImporter` may be provided at once. Future compileAsync(String path, {Syntax syntax, Logger logger, AsyncImportCache importCache, NodeImporter nodeImporter, - Iterable importers, - Iterable loadPaths, - SyncPackageResolver packageResolver, Iterable functions, OutputStyle style, bool useSpaces = true, @@ -48,8 +40,7 @@ Future compileAsync(String path, Stylesheet stylesheet; if (nodeImporter == null && (syntax == null || syntax == Syntax.forPath(path))) { - importCache ??= AsyncImportCache(importers, - loadPaths: loadPaths, packageResolver: packageResolver, logger: logger); + importCache ??= AsyncImportCache.none(logger: logger); stylesheet = await importCache.importCanonical( FilesystemImporter('.'), p.toUri(p.canonicalize(path)), p.toUri(path)); } else { @@ -75,12 +66,7 @@ Future compileAsync(String path, /// Like [compileStringAsync] in `lib/sass.dart`, but provides more options to /// support the node-sass compatible API. /// -/// No more than one of the following arguments or sets of arguments may be -/// provided: -/// -/// * `importCache`; -/// * `nodeImporter`; -/// * `importers`, `packageResolver`, and/or `loadPaths`. +/// At most one of `importCache` and `nodeImporter` may be provided at once. Future compileStringAsync(String source, {Syntax syntax, Logger logger, @@ -100,11 +86,6 @@ Future compileStringAsync(String source, var stylesheet = Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger); - if (nodeImporter == null) { - importCache ??= AsyncImportCache(importers, - loadPaths: loadPaths, packageResolver: packageResolver, logger: logger); - } - return _compileStylesheet( stylesheet, logger, diff --git a/lib/src/async_import_cache.dart b/lib/src/async_import_cache.dart index 2f55f868..d00966bf 100644 --- a/lib/src/async_import_cache.dart +++ b/lib/src/async_import_cache.dart @@ -17,9 +17,6 @@ import 'utils.dart'; // ignore: unused_import /// An in-memory cache of parsed stylesheets that have been imported by Sass. class AsyncImportCache { - /// A cache that contains no importers. - static const none = AsyncImportCache._none(); - /// The importers to use when loading new Sass files. final List _importers; @@ -63,6 +60,13 @@ class AsyncImportCache { _canonicalizeCache = {}, _importCache = {}; + /// Creates an import cache without any globally-avaiable importers. + AsyncImportCache.none({Logger logger}) + : _importers = const [], + _logger = logger ?? const Logger.stderr(), + _canonicalizeCache = {}, + _importCache = {}; + /// Converts the user's [importers], [loadPaths], and [packageResolver] /// options into a single list of importers. static List _toImporters(Iterable importers, diff --git a/lib/src/compile.dart b/lib/src/compile.dart index 03291af5..872976b4 100644 --- a/lib/src/compile.dart +++ b/lib/src/compile.dart @@ -5,7 +5,7 @@ // DO NOT EDIT. This file was generated from async_compile.dart. // See tool/synchronize.dart for details. // -// Checksum: ece42e2134684c290c8d921521c863857664d323 +// Checksum: e6de63e581c0f4da96756b9e2904bd81a090dc5b // // ignore_for_file: unused_import @@ -31,20 +31,12 @@ import 'visitor/serialize.dart'; /// Like [compile] in `lib/sass.dart`, but provides more options to support /// the node-sass compatible API and the executable. /// -/// No more than one of the following arguments or sets of arguments may be -/// provided: -/// -/// * `importCache`; -/// * `nodeImporter`; -/// * `importers`, `packageResolver`, and/or `loadPaths`. +/// At most one of `importCache` and `nodeImporter` may be provided at once. CompileResult compile(String path, {Syntax syntax, Logger logger, ImportCache importCache, NodeImporter nodeImporter, - Iterable importers, - Iterable loadPaths, - SyncPackageResolver packageResolver, Iterable functions, OutputStyle style, bool useSpaces = true, @@ -56,8 +48,7 @@ CompileResult compile(String path, Stylesheet stylesheet; if (nodeImporter == null && (syntax == null || syntax == Syntax.forPath(path))) { - importCache ??= ImportCache(importers, - loadPaths: loadPaths, packageResolver: packageResolver, logger: logger); + importCache ??= ImportCache.none(logger: logger); stylesheet = importCache.importCanonical( FilesystemImporter('.'), p.toUri(p.canonicalize(path)), p.toUri(path)); } else { @@ -83,12 +74,7 @@ CompileResult compile(String path, /// Like [compileString] in `lib/sass.dart`, but provides more options to /// support the node-sass compatible API. /// -/// No more than one of the following arguments or sets of arguments may be -/// provided: -/// -/// * `importCache`; -/// * `nodeImporter`; -/// * `importers`, `packageResolver`, and/or `loadPaths`. +/// At most one of `importCache` and `nodeImporter` may be provided at once. CompileResult compileString(String source, {Syntax syntax, Logger logger, @@ -108,11 +94,6 @@ CompileResult compileString(String source, var stylesheet = Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger); - if (nodeImporter == null) { - importCache ??= ImportCache(importers, - loadPaths: loadPaths, packageResolver: packageResolver, logger: logger); - } - return _compileStylesheet( stylesheet, logger, diff --git a/lib/src/import_cache.dart b/lib/src/import_cache.dart index 041ac16b..34bc54ef 100644 --- a/lib/src/import_cache.dart +++ b/lib/src/import_cache.dart @@ -5,7 +5,7 @@ // DO NOT EDIT. This file was generated from async_import_cache.dart. // See tool/synchronize.dart for details. // -// Checksum: 54045771e1267d445a733a83d9fb7e40439d886e +// Checksum: 4989811a7b432e181ccc42004e91f4fe54a786ca // // ignore_for_file: unused_import @@ -22,9 +22,6 @@ import 'utils.dart'; // ignore: unused_import /// An in-memory cache of parsed stylesheets that have been imported by Sass. class ImportCache { - /// A cache that contains no importers. - static const none = ImportCache._none(); - /// The importers to use when loading new Sass files. final List _importers; @@ -68,6 +65,13 @@ class ImportCache { _canonicalizeCache = {}, _importCache = {}; + /// Creates an import cache without any globally-avaiable importers. + ImportCache.none({Logger logger}) + : _importers = const [], + _logger = logger ?? const Logger.stderr(), + _canonicalizeCache = {}, + _importCache = {}; + /// Converts the user's [importers], [loadPaths], and [packageResolver] /// options into a single list of importers. static List _toImporters(Iterable importers, diff --git a/lib/src/visitor/async_evaluate.dart b/lib/src/visitor/async_evaluate.dart index 073f50f8..3cf88e21 100644 --- a/lib/src/visitor/async_evaluate.dart +++ b/lib/src/visitor/async_evaluate.dart @@ -204,7 +204,9 @@ class _EvaluateVisitor Map variables, Logger logger, bool sourceMap}) - : _importCache = importCache ?? AsyncImportCache.none, + : _importCache = nodeImporter == null + ? importCache ?? AsyncImportCache.none(logger: logger) + : null, _importer = importer ?? Importer.noOp, _nodeImporter = nodeImporter, _logger = logger ?? const Logger.stderr(), @@ -1815,10 +1817,11 @@ class _EvaluateVisitor /// Creates a new stack frame with location information from [member] and /// [span]. - Frame _stackFrame(String member, FileSpan span) => frameForSpan(span, member, - url: span.sourceUrl == null - ? null - : _importCache.humanize(span.sourceUrl)); + Frame _stackFrame(String member, FileSpan span) { + var url = span.sourceUrl; + if (url != null && _importCache != null) url = _importCache.humanize(url); + return frameForSpan(span, member, url: url); + } /// Returns a stack trace at the current point. /// diff --git a/lib/src/visitor/evaluate.dart b/lib/src/visitor/evaluate.dart index c4546218..f77f3a52 100644 --- a/lib/src/visitor/evaluate.dart +++ b/lib/src/visitor/evaluate.dart @@ -5,7 +5,7 @@ // DO NOT EDIT. This file was generated from async_evaluate.dart. // See tool/synchronize.dart for details. // -// Checksum: 07e2cda457e3bca4efd3b3f4742744c91c2970d8 +// Checksum: 2065f7d701646283e536c5300b0f20f38c4acb46 // // ignore_for_file: unused_import @@ -211,7 +211,9 @@ class _EvaluateVisitor Map variables, Logger logger, bool sourceMap}) - : _importCache = importCache ?? ImportCache.none, + : _importCache = nodeImporter == null + ? importCache ?? ImportCache.none(logger: logger) + : null, _importer = importer ?? Importer.noOp, _nodeImporter = nodeImporter, _logger = logger ?? const Logger.stderr(), @@ -1790,10 +1792,11 @@ class _EvaluateVisitor /// Creates a new stack frame with location information from [member] and /// [span]. - Frame _stackFrame(String member, FileSpan span) => frameForSpan(span, member, - url: span.sourceUrl == null - ? null - : _importCache.humanize(span.sourceUrl)); + Frame _stackFrame(String member, FileSpan span) { + var url = span.sourceUrl; + if (url != null && _importCache != null) url = _importCache.humanize(url); + return frameForSpan(span, member, url: url); + } /// Returns a stack trace at the current point. ///