Skip to content
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

Expose inImportRule() as the test-only fakeFromImport() function #1750

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@

* Deprecate passing non-`deg` units to `color.hwb()`'s `$hue` argument.

### Dart API

* Add a top-level `fakeFromImport()` function for testing custom importers
that use `AsyncImporter.fromImport`.

### JS API

* Add a `charset` option that controls whether or not Sass emits a
`@charset`/BOM for non-ASCII stylesheets.

* Fix Sass npm package types for TS 4.7+ Node16 and NodeNext module resolution.

## 1.53.0
Expand Down
9 changes: 9 additions & 0 deletions lib/sass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// We strongly recommend importing this library with the prefix `sass`.
library sass;

import 'package:meta/meta.dart';
import 'package:package_config/package_config_types.dart';
import 'package:source_maps/source_maps.dart';

Expand All @@ -15,6 +16,7 @@ import 'src/compile_result.dart';
import 'src/exception.dart';
import 'src/import_cache.dart';
import 'src/importer.dart';
import 'src/importer/utils.dart';
import 'src/logger.dart';
import 'src/syntax.dart';
import 'src/util/nullable.dart';
Expand Down Expand Up @@ -474,3 +476,10 @@ Future<String> compileStringAsync(
result.sourceMap.andThen(sourceMap);
return result.css;
}

/// Runs [callback] in a context where [AsyncImporter.fromImport] returns
/// `true`.
///
/// This is only intended for use when testing custom importers.
@visibleForTesting
T fakeFromImport<T>(T callback()) => inImportRule(callback);
2 changes: 1 addition & 1 deletion lib/src/importer/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import '../io.dart';
/// removed, at which point we can delete this and have one consistent behavior.
bool get fromImport => Zone.current[#_inImportRule] as bool? ?? false;

/// Runs [callback] in a context where [inImportRule] returns `true` and
/// Runs [callback] in a context where [fromImport] returns `true` and
/// [resolveImportPath] uses `@import` semantics rather than `@use` semantics.
T inImportRule<T>(T callback()) =>
runZoned(callback, zoneValues: {#_inImportRule: true});
Expand Down