Skip to content

Commit

Permalink
Merge pull request #1326 from sass/limit-warnings
Browse files Browse the repository at this point in the history
Add an option to the CLI and Dart Sass to silence warnings from deps
  • Loading branch information
nex3 committed May 22, 2021
2 parents 7f982a1 + 16f1816 commit d15a41a
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 52 deletions.
22 changes: 21 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,27 @@
## 1.33.1
## 1.34.0

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

* Cap deprecation warnings at 5 per feature by default.

### Command Line Interface

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

* Add a `--verbose` flag which causes the compiler to emit all deprecation
warnings, not just 5 per feature.

### 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.

* Add a `verbose` argument to `compile()`, `compileString()`, `compileAsync()`,
and `compileStringAsync()` which causes the compiler to emit all deprecation
warnings, not just 5 per feature.

## 1.33.0

* Deprecate the use of `/` for division. The new `math.div()` function should be
Expand Down
30 changes: 30 additions & 0 deletions lib/sass.dart
Expand Up @@ -61,6 +61,13 @@ 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].
///
/// By default, once a deprecation warning for a given feature is printed five
/// times, further warnings for that feature are silenced. If [verbose] is true,
/// all deprecation warnings are printed instead.
///
/// 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 +101,8 @@ String compile(String path,
PackageConfig? packageConfig,
Iterable<Callable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
void sourceMap(SingleMapping map)?,
bool charset = true}) {
logger ??= Logger.stderr(color: color);
Expand All @@ -106,6 +115,8 @@ String compile(String path,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand Down Expand Up @@ -150,6 +161,13 @@ 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].
///
/// By default, once a deprecation warning for a given feature is printed five
/// times, further warnings for that feature are silenced. If [verbose] is true,
/// all deprecation warnings are printed instead.
///
/// 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 +204,8 @@ String compileString(String source,
OutputStyle? style,
Importer? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) {
Expand All @@ -202,6 +222,8 @@ String compileString(String source,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand All @@ -221,6 +243,8 @@ Future<String> compileAsync(String path,
Iterable<String>? loadPaths,
Iterable<AsyncCallable>? functions,
OutputStyle? style,
bool quietDeps = false,
bool verbose = false,
void sourceMap(SingleMapping map)?}) async {
logger ??= Logger.stderr(color: color);
var result = await c.compileAsync(path,
Expand All @@ -232,6 +256,8 @@ Future<String> compileAsync(String path,
packageConfig: packageConfig),
functions: functions,
style: style,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null);
result.sourceMap.andThen(sourceMap);
return result.css;
Expand All @@ -253,6 +279,8 @@ Future<String> compileStringAsync(String source,
OutputStyle? style,
AsyncImporter? importer,
Object? url,
bool quietDeps = false,
bool verbose = false,
void sourceMap(SingleMapping map)?,
bool charset = true,
@Deprecated("Use syntax instead.") bool indented = false}) async {
Expand All @@ -269,6 +297,8 @@ Future<String> compileStringAsync(String source,
style: style,
importer: importer,
url: url,
quietDeps: quietDeps,
verbose: verbose,
sourceMap: sourceMap != null,
charset: charset);
result.sourceMap.andThen(sourceMap);
Expand Down
28 changes: 25 additions & 3 deletions lib/src/async_compile.dart
Expand Up @@ -15,6 +15,7 @@ import 'importer.dart';
import 'importer/node.dart';
import 'io.dart';
import 'logger.dart';
import 'logger/terse.dart';
import 'syntax.dart';
import 'utils.dart';
import 'visitor/async_evaluate.dart';
Expand All @@ -34,23 +35,29 @@ Future<CompileResult> compileAsync(String path,
bool useSpaces = true,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true}) async {
TerseLogger? terseLogger;
if (!verbose) logger = terseLogger = TerseLogger(logger ?? Logger.stderr());

// If the syntax is different than the importer would default to, we have to
// parse the file manually and we can't store it in the cache.
Stylesheet? stylesheet;
if (nodeImporter == null &&
(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),
url: p.toUri(path), logger: logger);
}

return await _compileStylesheet(
var result = await _compileStylesheet(
stylesheet,
logger,
importCache,
Expand All @@ -61,8 +68,12 @@ Future<CompileResult> compileAsync(String path,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);

terseLogger?.summarize(node: nodeImporter != null);
return result;
}

/// Like [compileStringAsync] in `lib/sass.dart`, but provides more options to
Expand All @@ -83,12 +94,17 @@ Future<CompileResult> compileStringAsync(String source,
int? indentWidth,
LineFeed? lineFeed,
Object? url,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true}) async {
TerseLogger? terseLogger;
if (!verbose) logger = terseLogger = TerseLogger(logger ?? Logger.stderr());

var stylesheet =
Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger);

return _compileStylesheet(
var result = await _compileStylesheet(
stylesheet,
logger,
importCache,
Expand All @@ -99,8 +115,12 @@ Future<CompileResult> compileStringAsync(String source,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);

terseLogger?.summarize(node: nodeImporter != null);
return result;
}

/// Compiles [stylesheet] and returns its result.
Expand All @@ -117,6 +137,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 +146,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
30 changes: 26 additions & 4 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: 8e813f2ead6e78899ce820e279983278809a7ea5
//
// ignore_for_file: unused_import

Expand All @@ -25,6 +25,7 @@ import 'importer.dart';
import 'importer/node.dart';
import 'io.dart';
import 'logger.dart';
import 'logger/terse.dart';
import 'syntax.dart';
import 'utils.dart';
import 'visitor/evaluate.dart';
Expand All @@ -44,23 +45,29 @@ CompileResult compile(String path,
bool useSpaces = true,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true}) {
TerseLogger? terseLogger;
if (!verbose) logger = terseLogger = TerseLogger(logger ?? Logger.stderr());

// If the syntax is different than the importer would default to, we have to
// parse the file manually and we can't store it in the cache.
Stylesheet? stylesheet;
if (nodeImporter == null &&
(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),
url: p.toUri(path), logger: logger);
}

return _compileStylesheet(
var result = _compileStylesheet(
stylesheet,
logger,
importCache,
Expand All @@ -71,8 +78,12 @@ CompileResult compile(String path,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);

terseLogger?.summarize(node: nodeImporter != null);
return result;
}

/// Like [compileString] in `lib/sass.dart`, but provides more options to
Expand All @@ -93,12 +104,17 @@ CompileResult compileString(String source,
int? indentWidth,
LineFeed? lineFeed,
Object? url,
bool quietDeps = false,
bool verbose = false,
bool sourceMap = false,
bool charset = true}) {
TerseLogger? terseLogger;
if (!verbose) logger = terseLogger = TerseLogger(logger ?? Logger.stderr());

var stylesheet =
Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger);

return _compileStylesheet(
var result = _compileStylesheet(
stylesheet,
logger,
importCache,
Expand All @@ -109,8 +125,12 @@ CompileResult compileString(String source,
useSpaces,
indentWidth,
lineFeed,
quietDeps,
sourceMap,
charset);

terseLogger?.summarize(node: nodeImporter != null);
return result;
}

/// Compiles [stylesheet] and returns its result.
Expand All @@ -127,6 +147,7 @@ CompileResult _compileStylesheet(
bool useSpaces,
int? indentWidth,
LineFeed? lineFeed,
bool quietDeps,
bool sourceMap,
bool charset) {
var evaluateResult = evaluate(stylesheet,
Expand All @@ -135,6 +156,7 @@ CompileResult _compileStylesheet(
importer: importer,
functions: functions,
logger: logger,
quietDeps: quietDeps,
sourceMap: sourceMap);

var serializeResult = serialize(evaluateResult.stylesheet,
Expand Down

0 comments on commit d15a41a

Please sign in to comment.