Skip to content

Commit

Permalink
Add an option to the CLI and Dart Sass to silence warnings from deps
Browse files Browse the repository at this point in the history
Closes #672
  • Loading branch information
nex3 committed May 22, 2021
1 parent 7f982a1 commit d660bf1
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 23 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,18 @@
## 1.33.1
## 1.34.0

* Don't emit the same warning in the same location multiple times.

### Command Line Interface

* Add a `--quiet-deps` flag which silences compiler warnings from stylesheets
loaded through `--load-path`s.

### Dart API

* Add a `quietDeps` argument to `compile()`, `compileString()`,
`compileAsync()`, and `compileStringAsync()` which silences compiler warnings
from stylesheets loaded through importers, load paths, and `package:` URLs.

## 1.33.0

* Deprecate the use of `/` for division. The new `math.div()` function should be
Expand Down
14 changes: 14 additions & 0 deletions lib/sass.dart
Expand Up @@ -61,6 +61,9 @@ export 'src/warn.dart' show warn;
///
/// The [style] parameter controls the style of the resulting CSS.
///
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
///
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
/// sections of the source file(s) correspond to which in the resulting CSS.
/// It's called immediately before this method returns, and only if compilation
Expand Down Expand Up @@ -94,6 +97,7 @@ String compile(String path,
PackageConfig? packageConfig,
Iterable<Callable>? functions,
OutputStyle? style,
bool quietDeps = false,
void sourceMap(SingleMapping map)?,
bool charset = true}) {
logger ??= Logger.stderr(color: color);
Expand All @@ -106,6 +110,7 @@ String compile(String path,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand Down Expand Up @@ -150,6 +155,9 @@ String compile(String path,
/// [String] or a [Uri]. If [importer] is passed, [url] must be passed as well
/// and `importer.load(url)` should return `source`.
///
/// If [quietDeps] is `true`, this will silence compiler warnings emitted for
/// stylesheets loaded through [importers], [loadPaths], or [packageConfig].
///
/// If [sourceMap] is passed, it's passed a [SingleMapping] that indicates which
/// sections of the source file(s) correspond to which in the resulting CSS.
/// It's called immediately before this method returns, and only if compilation
Expand Down Expand Up @@ -186,6 +194,7 @@ String compileString(String source,
OutputStyle? style,
Importer? importer,
Object? url,
bool quietDeps = false,
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) {
Expand All @@ -202,6 +211,7 @@ String compileString(String source,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand All @@ -221,6 +231,7 @@ Future<String> compileAsync(String path,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool quietDeps = false,
void sourceMap(SingleMapping map)?}) async {
logger ??= Logger.stderr(color: color);
var result = await c.compileAsync(path,
Expand All @@ -232,6 +243,7 @@ Future<String> compileAsync(String path,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
sourceMap: sourceMap != null);
result.sourceMap.andThen(sourceMap);
return result.css;
Expand All @@ -253,6 +265,7 @@ Future<String> compileStringAsync(String source,
OutputStyle? style,
AsyncImporter? importer,
Object? url,
bool quietDeps = false,
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) async {
Expand All @@ -269,6 +282,7 @@ Future<String> compileStringAsync(String source,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand Down
9 changes: 8 additions & 1 deletion lib/src/async_compile.dart
Expand Up @@ -34,6 +34,7 @@ Future<CompileResult> compileAsync(String path,
bool useSpaces = true,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps = false,
bool sourceMap = false,
bool charset = true}) async {
// If the syntax is different than the importer would default to, we have to
Expand All @@ -43,7 +44,8 @@ Future<CompileResult> compileAsync(String path,
(syntax == null || syntax == Syntax.forPath(path))) {
importCache ??= AsyncImportCache.none(logger: logger);
stylesheet = (await importCache.importCanonical(
FilesystemImporter('.'), p.toUri(canonicalize(path)), p.toUri(path)))!;
FilesystemImporter('.'), p.toUri(canonicalize(path)),
originalUrl: p.toUri(path)))!;
} else {
stylesheet = Stylesheet.parse(
readFile(path), syntax ?? Syntax.forPath(path),
Expand All @@ -61,6 +63,7 @@ Future<CompileResult> compileAsync(String path,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);
}
Expand All @@ -83,6 +86,7 @@ Future<CompileResult> compileStringAsync(String source,
int? indentWidth,
LineFeed? lineFeed,
Object? url,
bool quietDeps = false,
bool sourceMap = false,
bool charset = true}) async {
var stylesheet =
Expand All @@ -99,6 +103,7 @@ Future<CompileResult> compileStringAsync(String source,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);
}
Expand All @@ -117,6 +122,7 @@ Future<CompileResult> _compileStylesheet(
bool useSpaces,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps,
bool sourceMap,
bool charset) async {
var evaluateResult = await evaluateAsync(stylesheet,
Expand All @@ -125,6 +131,7 @@ Future<CompileResult> _compileStylesheet(
importer: importer,
functions: functions,
logger: logger,
quietDeps: quietDeps,
sourceMap: sourceMap);

var serializeResult = serialize(evaluateResult.stylesheet,
Expand Down
11 changes: 7 additions & 4 deletions lib/src/async_import_cache.dart
Expand Up @@ -162,8 +162,8 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
var tuple = await canonicalize(url,
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport);
if (tuple == null) return null;
var stylesheet =
await importCanonical(tuple.item1, tuple.item2, tuple.item3);
var stylesheet = await importCanonical(tuple.item1, tuple.item2,
originalUrl: tuple.item3);
if (stylesheet == null) return null;
return Tuple2(tuple.item1, stylesheet);
}
Expand All @@ -177,9 +177,12 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
/// into [canonicalUrl]. It's used to resolve a relative canonical URL, which
/// importers may return for legacy reasons.
///
/// If [quiet] is `true`, this will disable logging warnings when parsing the
/// newly imported stylesheet.
///
/// Caches the result of the import and uses cached results if possible.
Future<Stylesheet?> importCanonical(AsyncImporter importer, Uri canonicalUrl,
[Uri? originalUrl]) async {
{Uri? originalUrl, bool quiet = false}) async {
return await putIfAbsentAsync(_importCache, canonicalUrl, () async {
var result = await importer.load(canonicalUrl);
if (result == null) return null;
Expand All @@ -191,7 +194,7 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
url: originalUrl == null
? canonicalUrl
: originalUrl.resolveUri(canonicalUrl),
logger: _logger);
logger: quiet ? Logger.quiet : _logger);
});
}

Expand Down
11 changes: 9 additions & 2 deletions lib/src/compile.dart
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_compile.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: dcb7cfbedf1e1189808c0056debf6a68bd387dab
// Checksum: bdf01f7ff8eea0efafa6c7c93920caf26e324f4e
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -44,6 +44,7 @@ CompileResult compile(String path,
bool useSpaces = true,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps = false,
bool sourceMap = false,
bool charset = true}) {
// If the syntax is different than the importer would default to, we have to
Expand All @@ -53,7 +54,8 @@ CompileResult compile(String path,
(syntax == null || syntax == Syntax.forPath(path))) {
importCache ??= ImportCache.none(logger: logger);
stylesheet = importCache.importCanonical(
FilesystemImporter('.'), p.toUri(canonicalize(path)), p.toUri(path))!;
FilesystemImporter('.'), p.toUri(canonicalize(path)),
originalUrl: p.toUri(path))!;
} else {
stylesheet = Stylesheet.parse(
readFile(path), syntax ?? Syntax.forPath(path),
Expand All @@ -71,6 +73,7 @@ CompileResult compile(String path,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);
}
Expand All @@ -93,6 +96,7 @@ CompileResult compileString(String source,
int? indentWidth,
LineFeed? lineFeed,
Object? url,
bool quietDeps = false,
bool sourceMap = false,
bool charset = true}) {
var stylesheet =
Expand All @@ -109,6 +113,7 @@ CompileResult compileString(String source,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);
}
Expand All @@ -127,6 +132,7 @@ CompileResult _compileStylesheet(
bool useSpaces,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps,
bool sourceMap,
bool charset) {
var evaluateResult = evaluate(stylesheet,
Expand All @@ -135,6 +141,7 @@ CompileResult _compileStylesheet(
importer: importer,
functions: functions,
logger: logger,
quietDeps: quietDeps,
sourceMap: sourceMap);

var serializeResult = serialize(evaluateResult.stylesheet,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/executable/compile_stylesheet.dart
Expand Up @@ -68,13 +68,15 @@ Future<void> compileStylesheet(ExecutableOptions options, StylesheetGraph graph,
importCache: importCache,
importer: FilesystemImporter('.'),
style: options.style,
quietDeps: options.quietDeps,
sourceMap: options.emitSourceMap,
charset: options.charset)
: await compileAsync(source,
syntax: syntax,
logger: options.logger,
importCache: importCache,
style: options.style,
quietDeps: options.quietDeps,
sourceMap: options.emitSourceMap,
charset: options.charset);
} else {
Expand All @@ -85,13 +87,15 @@ Future<void> compileStylesheet(ExecutableOptions options, StylesheetGraph graph,
importCache: graph.importCache,
importer: FilesystemImporter('.'),
style: options.style,
quietDeps: options.quietDeps,
sourceMap: options.emitSourceMap,
charset: options.charset)
: compile(source,
syntax: syntax,
logger: options.logger,
importCache: graph.importCache,
style: options.style,
quietDeps: options.quietDeps,
sourceMap: options.emitSourceMap,
charset: options.charset);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/src/executable/options.dart
Expand Up @@ -99,6 +99,9 @@ class ExecutableOptions {
..addFlag('unicode',
help: 'Whether to use Unicode characters for messages.')
..addFlag('quiet', abbr: 'q', help: "Don't print warnings.")
..addFlag('quiet-deps',
help: "Don't print compiler warnings from dependencies.\n"
"Stylesheets imported through load paths count as dependencies.")
..addFlag('trace', help: 'Print full Dart stack traces for exceptions.')
..addFlag('help',
abbr: 'h', help: 'Print this usage information.', negatable: false)
Expand Down Expand Up @@ -163,9 +166,12 @@ class ExecutableOptions {
? _options['unicode'] as bool
: !term_glyph.ascii;

/// Whether to silence normal output.
/// Whether to silence all warnings.
bool get quiet => _options['quiet'] as bool;

/// Whether to silence warnings in dependencies.
bool get quietDeps => _options['quiet-deps'] as bool;

/// The logger to use to emit messages from Sass.
Logger get logger => quiet ? Logger.quiet : Logger.stderr(color: color);

Expand Down
12 changes: 8 additions & 4 deletions lib/src/import_cache.dart
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_import_cache.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: 950db49eb9e3a85f35bc4a3d7cfe029fb60ae498
// Checksum: 6821c9a63333c3c99b0c9515aa04e73a14e0f141
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -161,7 +161,8 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
var tuple = canonicalize(url,
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport);
if (tuple == null) return null;
var stylesheet = importCanonical(tuple.item1, tuple.item2, tuple.item3);
var stylesheet =
importCanonical(tuple.item1, tuple.item2, originalUrl: tuple.item3);
if (stylesheet == null) return null;
return Tuple2(tuple.item1, stylesheet);
}
Expand All @@ -175,9 +176,12 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
/// into [canonicalUrl]. It's used to resolve a relative canonical URL, which
/// importers may return for legacy reasons.
///
/// If [quiet] is `true`, this will disable logging warnings when parsing the
/// newly imported stylesheet.
///
/// Caches the result of the import and uses cached results if possible.
Stylesheet? importCanonical(Importer importer, Uri canonicalUrl,
[Uri? originalUrl]) {
{Uri? originalUrl, bool quiet = false}) {
return _importCache.putIfAbsent(canonicalUrl, () {
var result = importer.load(canonicalUrl);
if (result == null) return null;
Expand All @@ -189,7 +193,7 @@ Relative canonical URLs are deprecated and will eventually be disallowed.
url: originalUrl == null
? canonicalUrl
: originalUrl.resolveUri(canonicalUrl),
logger: _logger);
logger: quiet ? Logger.quiet : _logger);
});
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/stylesheet_graph.dart
Expand Up @@ -95,8 +95,8 @@ class StylesheetGraph {
var node = _nodes[canonicalUrl];
if (node != null) return const {};

var stylesheet = _ignoreErrors(
() => importCache.importCanonical(importer, canonicalUrl, originalUrl));
var stylesheet = _ignoreErrors(() => importCache
.importCanonical(importer, canonicalUrl, originalUrl: originalUrl));
if (stylesheet == null) return const {};

node = StylesheetNode._(stylesheet, importer, canonicalUrl,
Expand Down Expand Up @@ -278,8 +278,8 @@ class StylesheetGraph {
/// error will be produced during compilation.
if (active.contains(canonicalUrl)) return null;

var stylesheet = _ignoreErrors(
() => importCache.importCanonical(importer, canonicalUrl, resolvedUrl));
var stylesheet = _ignoreErrors(() => importCache
.importCanonical(importer, canonicalUrl, originalUrl: resolvedUrl));
if (stylesheet == null) return null;

active.add(canonicalUrl);
Expand Down

0 comments on commit d660bf1

Please sign in to comment.