Skip to content

Commit

Permalink
Compile asynchronous code to synchronous
Browse files Browse the repository at this point in the history
See #9
  • Loading branch information
nex3 committed Nov 18, 2017
1 parent f01e86f commit f99a57e
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 32 deletions.
5 changes: 5 additions & 0 deletions lib/src/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

// DO NOT EDIT. This file was generated from async_environment.dart.
// See tool/synchronize.dart for details.
//
// Checksum: 97410abbd78c3bbc9899f3ac460cc0736218bfe3

import 'ast/sass.dart';
import 'callable.dart';
import 'functions.dart';
Expand Down
69 changes: 38 additions & 31 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/synchronize.dart for details.
//
// Checksum: ef8fa3966d7580d8511d8d8430a8f65cd9cb9018

import 'dart:math' as math;

import 'package:charcode/charcode.dart';
Expand All @@ -13,9 +18,9 @@ import 'package:tuple/tuple.dart';
import '../ast/css.dart';
import '../ast/sass.dart';
import '../ast/selector.dart';
import '../environment.dart';
import '../callable.dart';
import '../color_names.dart';
import '../environment.dart';
import '../exception.dart';
import '../extend/extender.dart';
import '../importer.dart';
Expand All @@ -29,7 +34,7 @@ import 'interface/statement.dart';
import 'interface/expression.dart';

/// A function that takes a callback with no arguments.
typedef _ScopeCallback(callback());
typedef void _ScopeCallback(void callback());

/// The URL used in stack traces when no source URL is available.
final _noSourceUrl = Uri.parse("-");
Expand Down Expand Up @@ -301,12 +306,12 @@ class _EvaluateVisitor
}

Value visitAtRootRule(AtRootRule node) {
var query = node.query == null
? AtRootQuery.defaultQuery
: _adjustParseError(
node.query.span,
() => new AtRootQuery.parse(
_performInterpolation(node.query, warnForColor: true)));
var query = AtRootQuery.defaultQuery;
if (node.query != null) {
var resolved = _performInterpolation(node.query, warnForColor: true);
query = _adjustParseError(
node.query.span, () => new AtRootQuery.parse(resolved));
}

var parent = _parent;
var included = <CssParentNode>[];
Expand Down Expand Up @@ -384,7 +389,7 @@ class _EvaluateVisitor
/// [_parent] to [newParent].
_ScopeCallback _scopeForAtRoot(CssParentNode newParent, AtRootQuery query,
List<CssParentNode> included) {
var scope = (callback()) {
var scope = (void callback()) {
// We can't use [_withParent] here because it'll add the node to the tree
// in the wrong place.
var oldParent = _parent;
Expand Down Expand Up @@ -623,11 +628,13 @@ class _EvaluateVisitor
}

Value visitIfRule(IfRule node) {
var clause = node.clauses
.firstWhere((pair) => pair.item1.accept(this).isTruthy,
orElse: () => null)
?.item2 ??
node.lastClause;
var clause = node.lastClause;
for (var pair in node.clauses) {
if (pair.item1.accept(this).isTruthy) {
clause = pair.item2;
break;
}
}
if (clause == null) return null;

return _environment.scope(
Expand Down Expand Up @@ -802,18 +809,16 @@ class _EvaluateVisitor
throw _exception("Mixin doesn't accept a content block.", node.span);
}

Value callback() {
_environment.asMixin(() {
for (var statement in mixin.declaration.children) {
statement.accept(this);
}
});
return null;
}

var environment = node.children == null ? null : _environment.closure();
_runUserDefinedCallable(node.arguments, mixin, node.span, () {
_environment.withContent(node.children, environment, callback);
_environment.withContent(node.children, environment, () {
_environment.asMixin(() {
for (var statement in mixin.declaration.children) {
statement.accept(this);
}
});
return null;
});
});

return null;
Expand Down Expand Up @@ -876,11 +881,13 @@ class _EvaluateVisitor

/// Evaluates [interpolation] and parses the result as a list of media
/// queries.
List<CssMediaQuery> _visitMediaQueries(Interpolation interpolation) =>
_adjustParseError(
interpolation.span,
() => CssMediaQuery.parseList(
_performInterpolation(interpolation, warnForColor: true)));
List<CssMediaQuery> _visitMediaQueries(Interpolation interpolation) {
var resolved = _performInterpolation(interpolation, warnForColor: true);

// TODO(nweiz): Remove this type argument when sdk#31398 is fixed.
return _adjustParseError<List<CssMediaQuery>>(
interpolation.span, () => CssMediaQuery.parseList(resolved));
}

/// Returns a list of queries that selects for platforms that match both
/// [queries1] and [queries2].
Expand Down Expand Up @@ -1176,7 +1183,7 @@ class _EvaluateVisitor
SassColor visitColorExpression(ColorExpression node) => node.value;

SassList visitListExpression(ListExpression node) => new SassList(
node.contents.map((expression) => expression.accept(this)),
node.contents.map((Expression expression) => expression.accept(this)),
node.separator,
brackets: node.hasBrackets);

Expand Down Expand Up @@ -1375,7 +1382,7 @@ class _EvaluateVisitor
Tuple3<List<Value>, Map<String, Value>, ListSeparator> _evaluateArguments(
ArgumentInvocation arguments, FileSpan span) {
var positional = arguments.positional
.map((expression) => expression.accept(this))
.map((Expression expression) => expression.accept(this))
.toList();
var named = normalizedMapMap<String, Expression, Value>(arguments.named,
value: (_, expression) => expression.accept(this));
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies:

dev_dependencies:
archive: "^1.0.0"
analyzer: "^0.30.0"
crypto: ">=0.9.2 <3.0.0"
dart_style: "^1.0.0"
grinder: "^0.8.0"
http: "^0.11.0"
Expand Down
27 changes: 27 additions & 0 deletions test/synchronize_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:convert';
import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:test/test.dart';

void main() {
test("synchronized files are up-to-date", () {
({
'lib/src/visitor/async_evaluate.dart': 'lib/src/visitor/evaluate.dart',
'lib/src/async_environment.dart': 'lib/src/environment.dart'
}).forEach((sourcePath, targetPath) {
var source = new File(sourcePath).readAsStringSync();
var target = new File(targetPath).readAsStringSync();

var hash = sha1.convert(UTF8.encode(source));
if (!target.contains("Checksum: $hash")) {
fail("$targetPath is out-of-date.\n"
"Run pub run grinder to update it.");
}
});
});
}
12 changes: 11 additions & 1 deletion tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import 'package:yaml/yaml.dart';

import 'package:sass/src/util/path.dart';

import 'synchronize.dart';

export 'synchronize.dart';

/// The version of Dart Sass.
final String _version =
loadYaml(new File('pubspec.yaml').readAsStringSync())['version'] as String;
Expand All @@ -33,7 +37,13 @@ final _sdkDir = p.dirname(p.dirname(Platform.resolvedExecutable));

main(List<String> args) => grind(args);

@DefaultTask('Run the Dart formatter.')
@DefaultTask('Compile async code and reformat.')
all() {
format();
synchronize();
}

@Task('Run the Dart formatter.')
format() {
Pub.run('dart_style',
script: 'format',
Expand Down
Loading

0 comments on commit f99a57e

Please sign in to comment.