Skip to content

Commit

Permalink
Add a sass.types.Error constructor to the JS API (#627)
Browse files Browse the repository at this point in the history
Closes #590
  • Loading branch information
nex3 authored Apr 3, 2019
1 parent bc5e3c4 commit 9999835
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Consistently parse U+000C FORM FEED, U+000D CARRIAGE RETURN, and sequences of
U+000D CARRIAGE RETURN followed by U+000A LINE FEED as individual newlines.

### JavaScript API

* Add a `sass.types.Error` constructor as an alias for `Error`. This makes our
custom function API compatible with Node Sass's.

## 1.17.3

* Fix an edge case where slash-separated numbers were written to the stylesheet
Expand Down
3 changes: 2 additions & 1 deletion lib/src/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void main() {
Map: mapConstructor,
Null: nullConstructor,
Number: numberConstructor,
String: stringConstructor);
String: stringConstructor,
Error: jsErrorConstructor);
}

/// Converts Sass to CSS.
Expand Down
4 changes: 3 additions & 1 deletion lib/src/node/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Types {
external set Null(function);
external set Number(function);
external set String(function);
external set Error(function);

external factory Types({Boolean, Color, List, Map, Null, Number, String});
external factory Types(
{Boolean, Color, List, Map, Null, Number, String, Error});
}
4 changes: 2 additions & 2 deletions lib/src/node/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ bool isUndefined(value) => _isUndefined.call(value) as bool;
final _isUndefined = JSFunction("value", "return value === undefined;");

@JS("Error")
external Function get _JSError;
external Function get jsErrorConstructor;

/// Returns whether [value] is a JS Error object.
bool isJSError(value) => instanceof(value, _JSError);
bool isJSError(value) => instanceof(value, jsErrorConstructor);

/// Invokes [function] with [thisArg] as `this`.
Object call2(JSFunction function, Object thisArg, Object arg1, Object arg2) =>
Expand Down
4 changes: 4 additions & 0 deletions lib/src/node/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:js_util';

import '../value.dart';
import 'utils.dart';
import 'value/color.dart';
import 'value/list.dart';
import 'value/map.dart';
Expand All @@ -20,11 +21,14 @@ export 'value/number.dart';
export 'value/string.dart';

/// Unwraps a value wrapped with [wrapValue].
///
/// If [object] is a JS error, throws it.
Value unwrapValue(object) {
if (object != null) {
if (object is Value) return object;
var value = getProperty(object, 'dartValue');
if (value != null && value is Value) return value;
if (isJSError(object)) throw object;
}
throw "$object must be a Sass value type.";
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"install dependencies used for testing the Node API."
],
"devDependencies": {
"node-sass": "^4.11.0",
"chokidar": "^2.0.0",
"fibers": ">=1.0.0 <4.0.0",
"intercept-stdout": "^0.1.2"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.17.4-dev
version: 1.17.4
description: A Sass implementation in Dart.
author: Dart Team <[email protected]>
homepage: https://github.com/sass/dart-sass
Expand Down
1 change: 1 addition & 0 deletions test/node_api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SassTypes {
external NodeSassNullClass get Null;
external Function get Number;
external Function get String;
external Function get Error;
}

@JS()
Expand Down
23 changes: 23 additions & 0 deletions test/node_api/function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ void main() {
expect(error.toString(), contains('aw beans'));
});

test("reports a synchronous sass.types.Error", () async {
var error = await renderError(RenderOptions(
data: "a {b: foo()}",
functions: jsify({
"foo": allowInterop(
(_) => callConstructor(sass.types.Error, ["aw beans"]))
})));
expect(error.toString(), contains('aw beans'));
});

test("reports an asynchronous error", () async {
var error = await renderError(RenderOptions(
data: "a {b: foo()}",
Expand All @@ -208,6 +218,19 @@ void main() {
expect(error.toString(), contains('aw beans'));
});

test("reports an asynchronous sass.types.Error", () async {
var error = await renderError(RenderOptions(
data: "a {b: foo()}",
functions: jsify({
"foo": allowInterop((done) {
Future.delayed(Duration.zero).then((_) {
done(callConstructor(sass.types.Error, ["aw beans"]));
});
})
})));
expect(error.toString(), contains('aw beans'));
});

test("reports a null return", () async {
var error = await renderError(RenderOptions(
data: "a {b: foo()}",
Expand Down

0 comments on commit 9999835

Please sign in to comment.