2018-04-20 01:51:20 +02:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2023-05-19 22:22:44 +02:00
|
|
|
@TestOn('vm')
|
|
|
|
|
2018-04-20 01:51:20 +02:00
|
|
|
import 'package:charcode/charcode.dart';
|
|
|
|
import 'package:source_maps/source_maps.dart';
|
|
|
|
import 'package:source_span/source_span.dart';
|
|
|
|
import 'package:string_scanner/string_scanner.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
|
|
|
import 'package:sass/sass.dart';
|
2018-09-11 00:38:30 +02:00
|
|
|
import 'package:sass/src/utils.dart';
|
2018-04-20 01:51:20 +02:00
|
|
|
|
2019-06-27 02:34:24 +02:00
|
|
|
import 'dart_api/test_importer.dart';
|
|
|
|
|
2019-11-06 01:28:26 +01:00
|
|
|
void main() {
|
2018-04-20 01:51:20 +02:00
|
|
|
group("maps source to target for", () {
|
|
|
|
group("a style rule", () {
|
|
|
|
test("that's basic", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo
|
|
|
|
{{2}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
2021-04-15 23:01:07 +02:00
|
|
|
test("with a multiline selector", () {
|
2018-04-20 01:51:20 +02:00
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo,
|
|
|
|
bar
|
|
|
|
{{2}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}foo,
|
|
|
|
bar {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo,
|
|
|
|
{{1}}bar {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("with a property on a different line", () {
|
|
|
|
_expectScssSourceMap("""
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar:
|
|
|
|
{{3}}baz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar: {{3}}baz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("with a multiline property", () {
|
|
|
|
_expectScssSourceMap("""
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar: baz
|
|
|
|
bang;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}bar: baz bang;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("that's nested", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
foo
|
|
|
|
{{1}}bar
|
|
|
|
{{2}}baz: bang
|
|
|
|
""", """
|
|
|
|
foo {
|
|
|
|
{{1}}bar {
|
|
|
|
{{2}}baz: bang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo bar {
|
|
|
|
{{2}}baz: bang;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("with a nested rule and declaration", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo
|
|
|
|
{{2}}a: b
|
|
|
|
|
|
|
|
{{3}}bar
|
|
|
|
{{4}}x: y
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}a: b;
|
|
|
|
|
|
|
|
{{3}}bar {
|
|
|
|
{{4}}x: y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}a: b;
|
|
|
|
}
|
|
|
|
{{3}}foo bar {
|
|
|
|
{{4}}x: y;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("with a nested declaration", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo
|
|
|
|
{{2}}a: b
|
|
|
|
{{3}}c: d
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}a: b {
|
|
|
|
{{3}}c: d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}a: b;
|
|
|
|
{{3}}a-c: d;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("an unknown at-rule", () {
|
|
|
|
test("without children", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@foo (fblthp)
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp);
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp);
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
group("that contains", () {
|
|
|
|
test("declarations", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@foo (fblthp)
|
|
|
|
{{2}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}bar: baz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("style rules", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@foo (fblthp)
|
|
|
|
{{2}}bar
|
|
|
|
{{3}}baz: bang
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}bar {
|
|
|
|
{{3}}baz: bang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}bar {
|
|
|
|
{{3}}baz: bang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("at-rules", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@foo (fblthp)
|
|
|
|
{{2}}@bar baz
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}@bar baz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@foo (fblthp) {
|
|
|
|
{{2}}@bar baz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("a comment", () {
|
|
|
|
test("that covers a single line", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}/* foo bar
|
|
|
|
{{2}}/* baz bang
|
|
|
|
""", """
|
|
|
|
{{1}}/* foo bar */
|
|
|
|
{{2}}/* baz bang */
|
|
|
|
""", """
|
|
|
|
{{1}}/* foo bar */
|
|
|
|
{{2}}/* baz bang */
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("that covers multiple lines", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}/* foo bar
|
|
|
|
baz bang
|
|
|
|
""", """
|
|
|
|
{{1}}/* foo bar
|
|
|
|
* baz bang */
|
|
|
|
""", """
|
|
|
|
{{1}}/* foo bar
|
|
|
|
{{1}} * baz bang */
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("@import", () {
|
|
|
|
test("with a single URL", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
@import {{1}}url(foo)
|
|
|
|
""", """
|
|
|
|
@import {{1}}url(foo);
|
|
|
|
""", """
|
|
|
|
{{1}}@import url(foo);
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("with multiple URLs", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
@import {{1}}url(foo), {{2}}"bar.css"
|
|
|
|
""", """
|
|
|
|
@import {{1}}url(foo),
|
|
|
|
{{2}}"bar.css";
|
|
|
|
""", """
|
|
|
|
{{1}}@import url(foo);
|
|
|
|
{{2}}@import "bar.css";
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("@keyframes", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@keyframes name
|
|
|
|
{{2}}from
|
|
|
|
{{3}}top: 0px
|
|
|
|
|
|
|
|
{{4}}10%
|
|
|
|
{{5}}top: 10px
|
|
|
|
""", """
|
|
|
|
{{1}}@keyframes name {
|
|
|
|
{{2}}from {
|
|
|
|
{{3}}top: 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{4}}10% {
|
|
|
|
{{5}}top: 10px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@keyframes name {
|
|
|
|
{{2}}from {
|
|
|
|
{{3}}top: 0px;
|
|
|
|
}
|
|
|
|
{{4}}10% {
|
|
|
|
{{5}}top: 10px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
group("@media", () {
|
|
|
|
test("at the root", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@media screen
|
|
|
|
{{2}}foo
|
|
|
|
{{3}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}@media screen {
|
|
|
|
{{2}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@media screen {
|
|
|
|
{{2}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("within a style rule", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo
|
|
|
|
{{2}}@media screen
|
|
|
|
{{3}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}@media screen {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}@media screen {
|
|
|
|
{{1}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("@supports", () {
|
|
|
|
test("at the root", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}@supports (display: grid)
|
|
|
|
{{2}}foo
|
|
|
|
{{3}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}@supports (display: grid) {
|
|
|
|
{{2}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{1}}@supports (display: grid) {
|
|
|
|
{{2}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("within a style rule", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}foo
|
|
|
|
{{2}}@supports (display: grid)
|
|
|
|
{{3}}bar: baz
|
|
|
|
""", """
|
|
|
|
{{1}}foo {
|
|
|
|
{{2}}@supports (display: grid) {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}@supports (display: grid) {
|
|
|
|
{{1}}foo {
|
|
|
|
{{3}}bar: baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
2018-04-26 02:03:13 +02:00
|
|
|
|
|
|
|
group("a value from a variable defined", () {
|
|
|
|
group("in", () {
|
|
|
|
test("a variable declaration", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$var: {{1}}value;
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}value;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("an @each rule", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@each $var in {{1}}1 2 {
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}2;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("a @for rule", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@for $var from {{1}}1 through 2 {
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}2;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
group("a mixin argument that is", () {
|
|
|
|
test("the default value", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@mixin foo($var: {{1}}1) {
|
|
|
|
{{2}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{3}}a {
|
|
|
|
@include foo();
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{3}}a {
|
|
|
|
{{2}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by position", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{1}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
@include foo({{3}}1);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{1}}b: {{3}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by name", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{1}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
@include foo($var: {{3}}1);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{1}}b: {{3}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by arglist", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{1}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
@include foo({{3}}(1,)...);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{1}}b: {{3}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("in a variable which is referenced by", () {
|
|
|
|
test("a variable rename", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$var1: {{1}}value;
|
|
|
|
$var2: $var1;
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var2;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}value;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("an @each rule from a variable", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$list: {{1}}1 2;
|
|
|
|
|
|
|
|
@each $var in $list {
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}2;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("a @for rule from a variable", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$start: {{1}}1;
|
|
|
|
$end: 2;
|
|
|
|
|
|
|
|
@for $var from $start through $end {
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}2;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
2019-07-30 01:51:54 +02:00
|
|
|
test("a @use rule with a with clause", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$var1: {{1}}new value;
|
|
|
|
@use 'other' with ($var2: $var1);
|
2019-06-27 02:34:24 +02:00
|
|
|
|
2019-07-30 01:51:54 +02:00
|
|
|
{{2}}a {
|
2019-08-30 00:31:03 +02:00
|
|
|
{{3}}b: other.$var2;
|
2019-07-30 01:51:54 +02:00
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{2}}a {
|
|
|
|
{{3}}b: {{1}}new value;
|
|
|
|
}
|
|
|
|
""",
|
|
|
|
importer: TestImporter(
|
|
|
|
(url) => Uri.parse("u:$url"),
|
|
|
|
(_) => ImporterResult(r"$var2: default value !default;",
|
|
|
|
syntax: Syntax.scss)));
|
2019-10-02 00:05:11 +02:00
|
|
|
});
|
2019-06-27 02:34:24 +02:00
|
|
|
|
2018-04-26 02:03:13 +02:00
|
|
|
group("a mixin argument that is", () {
|
|
|
|
test("the default value", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$original: {{1}}1;
|
|
|
|
|
|
|
|
@mixin foo($var: $original) {
|
|
|
|
{{2}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{3}}a {
|
|
|
|
@include foo();
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{3}}a {
|
|
|
|
{{2}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by position", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$original: {{1}}1;
|
|
|
|
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{2}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{3}}a {
|
|
|
|
@include foo($original);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{3}}a {
|
|
|
|
{{2}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by name", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$original: {{1}}1;
|
|
|
|
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{2}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{3}}a {
|
|
|
|
@include foo($var: $original);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{3}}a {
|
|
|
|
{{2}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("passed by arglist", () {
|
|
|
|
_expectScssSourceMap(r"""
|
|
|
|
$original: {{1}}1;
|
|
|
|
|
|
|
|
@mixin foo($var) {
|
|
|
|
{{2}}b: $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
{{3}}a {
|
|
|
|
@include foo($original...);
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
{{3}}a {
|
|
|
|
{{2}}b: {{1}}1;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-11 00:38:30 +02:00
|
|
|
|
|
|
|
group("a stylesheet with Unicode characters", () {
|
|
|
|
test("in expanded mode", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}föö
|
|
|
|
{{2}}bär: bäz
|
|
|
|
""", """
|
|
|
|
{{1}}föö {
|
|
|
|
{{2}}bär: bäz;
|
|
|
|
}
|
|
|
|
""", """
|
|
|
|
@charset "UTF-8";
|
|
|
|
{{1}}föö {
|
|
|
|
{{2}}bär: bäz;
|
|
|
|
}
|
|
|
|
""");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("in compressed mode", () {
|
|
|
|
_expectSourceMap("""
|
|
|
|
{{1}}föö
|
|
|
|
{{2}}bär: bäz
|
|
|
|
""", """
|
|
|
|
{{1}}föö {
|
|
|
|
{{2}}bär: bäz;
|
|
|
|
}
|
|
|
|
""", "\uFEFF{{1}}föö{{{2}}bär:bäz}", style: OutputStyle.compressed);
|
|
|
|
});
|
|
|
|
});
|
2018-04-26 02:03:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't use the source map location for variable errors", () {
|
|
|
|
// When source maps are enabled (by passing a callback to sourceMap), Sass
|
|
|
|
// tracks the original location where each variable was declared so that
|
|
|
|
// browsers can link to variable declarations rather than just usages.
|
|
|
|
// However, we want to refer to the usages when reporting errors because
|
|
|
|
// they have more context.
|
|
|
|
expect(() {
|
|
|
|
compileString(r"""
|
|
|
|
$map: (a: b);
|
|
|
|
x {y: $map}
|
|
|
|
""", sourceMap: (_) {});
|
2021-03-17 04:51:32 +01:00
|
|
|
}, throwsA(predicate((untypedError) {
|
2018-04-26 02:03:13 +02:00
|
|
|
var error = untypedError as SourceSpanException;
|
2021-03-17 03:25:39 +01:00
|
|
|
expect(error.span!.text, equals(r"$map"));
|
2018-04-26 02:03:13 +02:00
|
|
|
return true;
|
|
|
|
})));
|
2018-04-20 01:51:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asserts that [sass] and [scss] both compile to [css], and that the
|
|
|
|
/// associated source maps are generated properly.
|
|
|
|
///
|
|
|
|
/// All three strings are expected to be annotated to indicate which locations
|
|
|
|
/// in the source Sass and SCSS should map to which locations in the CSS. This
|
|
|
|
/// is done using numbers enclosed in double curly braces called "location
|
|
|
|
/// identifiers'. For example, the source text
|
|
|
|
///
|
|
|
|
/// {{1}}foo{{2}}: {{3}}1 + 1{{4}};
|
|
|
|
///
|
|
|
|
/// indicates four locations that should be mapped to the locations with the
|
|
|
|
/// same numbers in the target text:
|
|
|
|
///
|
|
|
|
/// {{1}}foo{{2}}: {{3}}2{{4}};
|
|
|
|
///
|
|
|
|
/// The [css] text may have multiple instances of the same location identifier,
|
|
|
|
/// which indicates that the same source text is mapped to multiple different
|
|
|
|
/// target locations.
|
|
|
|
///
|
|
|
|
/// This also re-indents the input strings with [_reindent].
|
2018-09-11 00:38:30 +02:00
|
|
|
void _expectSourceMap(String sass, String scss, String css,
|
2021-03-17 03:25:39 +01:00
|
|
|
{Importer? importer, OutputStyle? style}) {
|
2019-06-27 02:34:24 +02:00
|
|
|
_expectSassSourceMap(sass, css, importer: importer, style: style);
|
|
|
|
_expectScssSourceMap(scss, css, importer: importer, style: style);
|
2018-04-20 01:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Like [_expectSourceMap], but with only SCSS source.
|
2019-06-27 02:34:24 +02:00
|
|
|
void _expectScssSourceMap(String scss, String css,
|
2021-03-17 03:25:39 +01:00
|
|
|
{Importer? importer, OutputStyle? style}) {
|
2018-04-20 01:51:20 +02:00
|
|
|
var scssTuple = _extractLocations(_reindent(scss));
|
|
|
|
var scssText = scssTuple.item1;
|
|
|
|
var scssLocations = _tuplesToMap(scssTuple.item2);
|
|
|
|
|
|
|
|
var cssTuple = _extractLocations(_reindent(css));
|
|
|
|
var cssText = cssTuple.item1;
|
|
|
|
var cssLocations = cssTuple.item2;
|
|
|
|
|
2021-03-17 03:25:39 +01:00
|
|
|
late SingleMapping scssMap;
|
2019-06-27 02:34:24 +02:00
|
|
|
var scssOutput = compileString(scssText,
|
|
|
|
sourceMap: (map) => scssMap = map, importer: importer, style: style);
|
2018-04-20 01:51:20 +02:00
|
|
|
expect(scssOutput, equals(cssText));
|
|
|
|
_expectMapMatches(scssMap, scssText, cssText, scssLocations, cssLocations);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like [_expectSourceMap], but with only indented source.
|
2019-06-27 02:34:24 +02:00
|
|
|
void _expectSassSourceMap(String sass, String css,
|
2021-03-17 03:25:39 +01:00
|
|
|
{Importer? importer, OutputStyle? style}) {
|
2018-04-20 01:51:20 +02:00
|
|
|
var sassTuple = _extractLocations(_reindent(sass));
|
|
|
|
var sassText = sassTuple.item1;
|
|
|
|
var sassLocations = _tuplesToMap(sassTuple.item2);
|
|
|
|
|
|
|
|
var cssTuple = _extractLocations(_reindent(css));
|
|
|
|
var cssText = cssTuple.item1;
|
|
|
|
var cssLocations = cssTuple.item2;
|
|
|
|
|
2021-03-17 03:25:39 +01:00
|
|
|
late SingleMapping sassMap;
|
2018-04-20 01:51:20 +02:00
|
|
|
var sassOutput = compileString(sassText,
|
2019-06-27 02:34:24 +02:00
|
|
|
indented: true,
|
|
|
|
sourceMap: (map) => sassMap = map,
|
|
|
|
importer: importer,
|
|
|
|
style: style);
|
2018-04-20 01:51:20 +02:00
|
|
|
expect(sassOutput, equals(cssText));
|
|
|
|
_expectMapMatches(sassMap, sassText, cssText, sassLocations, cssLocations);
|
|
|
|
}
|
|
|
|
|
2021-04-15 23:01:07 +02:00
|
|
|
/// Returns [string] with leading whitespace stripped from each line so that the
|
2018-04-20 01:51:20 +02:00
|
|
|
/// least-indented line has zero indentation.
|
|
|
|
String _reindent(String string) {
|
2018-09-11 00:38:30 +02:00
|
|
|
var lines = trimAsciiRight(string).split("\n");
|
2018-04-20 01:51:20 +02:00
|
|
|
var minIndent = lines
|
2018-09-11 00:38:30 +02:00
|
|
|
.where((line) => trimAscii(line).isNotEmpty)
|
|
|
|
.map((line) => line.length - trimAsciiLeft(line).length)
|
2018-04-20 01:51:20 +02:00
|
|
|
.reduce((length1, length2) => length1 < length2 ? length1 : length2);
|
|
|
|
return lines
|
2018-09-11 00:38:30 +02:00
|
|
|
.map((line) => trimAscii(line).isEmpty ? "" : line.substring(minIndent))
|
2018-04-20 01:51:20 +02:00
|
|
|
.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses and removes the location annotations from [text].
|
|
|
|
Tuple2<String, List<Tuple2<String, SourceLocation>>> _extractLocations(
|
|
|
|
String text) {
|
2018-11-16 00:16:24 +01:00
|
|
|
var scanner = StringScanner(text);
|
|
|
|
var buffer = StringBuffer();
|
2018-04-20 01:51:20 +02:00
|
|
|
var locations = <Tuple2<String, SourceLocation>>[];
|
|
|
|
|
|
|
|
var offset = 0;
|
|
|
|
var line = 0;
|
|
|
|
var column = 0;
|
|
|
|
while (!scanner.isDone) {
|
2018-11-16 00:16:24 +01:00
|
|
|
if (scanner.matches(RegExp(r"{{[^{]"))) {
|
2018-09-11 00:38:30 +02:00
|
|
|
scanner.expect("{{");
|
2018-04-20 01:51:20 +02:00
|
|
|
var start = scanner.position;
|
|
|
|
while (!scanner.scan("}}")) {
|
|
|
|
scanner.readChar();
|
|
|
|
}
|
2018-11-16 00:16:24 +01:00
|
|
|
locations.add(Tuple2(scanner.substring(start, scanner.position - 2),
|
|
|
|
SourceLocation(offset, line: line, column: column)));
|
2018-04-20 01:51:20 +02:00
|
|
|
} else if (scanner.scanChar($lf)) {
|
|
|
|
offset++;
|
|
|
|
line++;
|
|
|
|
column = 0;
|
|
|
|
buffer.writeln();
|
|
|
|
} else {
|
|
|
|
buffer.writeCharCode(scanner.readChar());
|
|
|
|
offset++;
|
|
|
|
column++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 00:16:24 +01:00
|
|
|
return Tuple2(buffer.toString(), locations);
|
2018-04-20 01:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a list of tuples to a map, asserting that each key appears only
|
|
|
|
/// once.
|
|
|
|
Map<K, V> _tuplesToMap<K, V>(Iterable<Tuple2<K, V>> tuples) {
|
|
|
|
var map = <K, V>{};
|
|
|
|
for (var tuple in tuples) {
|
|
|
|
expect(map, isNot(contains(tuple.item1)));
|
|
|
|
map[tuple.item1] = tuple.item2;
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asserts that the entries in [map] match the map given by [sourceLocations]
|
|
|
|
/// and [targetLocations].
|
|
|
|
void _expectMapMatches(
|
|
|
|
SingleMapping map,
|
|
|
|
String sourceText,
|
|
|
|
String targetText,
|
|
|
|
Map<String, SourceLocation> sourceLocations,
|
|
|
|
List<Tuple2<String, SourceLocation>> targetLocations) {
|
|
|
|
expect(sourceLocations.keys,
|
2019-05-18 03:02:12 +02:00
|
|
|
equals({for (var tuple in targetLocations) tuple.item1}));
|
2018-04-20 01:51:20 +02:00
|
|
|
|
|
|
|
String actualMap() =>
|
|
|
|
"\nActual map:\n\n" + _mapToString(map, sourceText, targetText) + "\n";
|
|
|
|
|
|
|
|
var entryIter = _entriesForMap(map).iterator;
|
|
|
|
for (var tuple in targetLocations) {
|
|
|
|
var name = tuple.item1;
|
|
|
|
var expectedTarget = tuple.item2;
|
2021-03-17 03:25:39 +01:00
|
|
|
var expectedSource = sourceLocations[name]!;
|
2018-04-20 01:51:20 +02:00
|
|
|
|
|
|
|
if (!entryIter.moveNext()) {
|
|
|
|
fail('Missing mapping "$name", expected '
|
2019-05-09 02:40:32 +02:00
|
|
|
'${_mapping(expectedSource, expectedTarget)}.\n' +
|
2018-04-20 01:51:20 +02:00
|
|
|
actualMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
var entry = entryIter.current;
|
|
|
|
if (expectedSource.line != entry.source.line ||
|
|
|
|
expectedSource.column != entry.source.column ||
|
|
|
|
expectedTarget.line != entry.target.line ||
|
|
|
|
expectedTarget.column != entry.target.column) {
|
|
|
|
fail('Mapping "$name" was ${_mapping(entry.source, entry.target)}, '
|
2019-05-09 02:40:32 +02:00
|
|
|
'expected ${_mapping(expectedSource, expectedTarget)}.\n' +
|
2018-04-20 01:51:20 +02:00
|
|
|
actualMap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(entryIter.moveNext(), isFalse,
|
|
|
|
reason: 'Expected no more mappings.\n' + actualMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a [map] back into [Entry]s.
|
|
|
|
Iterable<Entry> _entriesForMap(SingleMapping map) sync* {
|
|
|
|
for (var lineEntry in map.lines) {
|
|
|
|
for (var entry in lineEntry.entries) {
|
2018-11-16 00:16:24 +01:00
|
|
|
yield Entry(
|
|
|
|
SourceLocation(0, line: entry.sourceLine, column: entry.sourceColumn),
|
|
|
|
SourceLocation(0, line: lineEntry.line, column: entry.column),
|
2018-04-20 01:51:20 +02:00
|
|
|
null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a terse human-readable string for a mapping from a source
|
|
|
|
/// [SourceLocation] to a target [SourceLocation].
|
|
|
|
String _mapping(SourceLocation source, SourceLocation target) =>
|
|
|
|
"${_location(source)} to ${_location(target)}";
|
|
|
|
|
|
|
|
/// Returns a terse human-readable string for a [SourceLocation].
|
|
|
|
String _location(SourceLocation location) =>
|
|
|
|
"${location.line}:${location.column}";
|
|
|
|
|
|
|
|
/// Returns a human-readable string representation of [map] between [sourceText]
|
|
|
|
/// and [targetText].
|
|
|
|
String _mapToString(SingleMapping map, String sourceText, String targetText) {
|
|
|
|
var entries = _entriesForMap(map);
|
|
|
|
var entriesInSourceOrder = entries.toList()
|
|
|
|
..sort((entry1, entry2) => entry1.source.compareTo(entry2.source));
|
|
|
|
|
|
|
|
// A map from lines and columns in [sourceText] to the names of the entries
|
|
|
|
// with those source locations.
|
|
|
|
var entryNames = <Tuple2<int, int>, String>{};
|
|
|
|
var i = 0;
|
|
|
|
for (var entry in entriesInSourceOrder) {
|
2018-11-16 00:16:24 +01:00
|
|
|
entryNames.putIfAbsent(
|
|
|
|
Tuple2(entry.source.line, entry.source.column), () => (++i).toString());
|
2018-04-20 01:51:20 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 00:16:24 +01:00
|
|
|
var sourceScanner = LineScanner(sourceText);
|
|
|
|
var sourceBuffer = StringBuffer();
|
2018-04-20 01:51:20 +02:00
|
|
|
while (!sourceScanner.isDone) {
|
2018-11-16 00:16:24 +01:00
|
|
|
var name = entryNames[Tuple2(sourceScanner.line, sourceScanner.column)];
|
2018-04-20 01:51:20 +02:00
|
|
|
if (name != null) sourceBuffer.write("{{$name}}");
|
|
|
|
sourceBuffer.writeCharCode(sourceScanner.readChar());
|
|
|
|
}
|
|
|
|
|
2018-11-16 00:16:24 +01:00
|
|
|
var targetScanner = LineScanner(targetText);
|
|
|
|
var targetBuffer = StringBuffer();
|
2018-04-20 01:51:20 +02:00
|
|
|
var entryIter = entries.iterator..moveNext();
|
|
|
|
while (!targetScanner.isDone) {
|
|
|
|
var entry = entryIter.current;
|
2021-03-17 04:51:32 +01:00
|
|
|
if (targetScanner.line == entry.target.line &&
|
2018-04-20 01:51:20 +02:00
|
|
|
targetScanner.column == entry.target.column) {
|
2018-11-16 00:16:24 +01:00
|
|
|
var name = entryNames[Tuple2(entry.source.line, entry.source.column)];
|
2018-04-20 01:51:20 +02:00
|
|
|
targetBuffer.write("{{$name}}");
|
2021-04-09 22:19:35 +02:00
|
|
|
if (!entryIter.moveNext()) {
|
|
|
|
targetBuffer.write(targetScanner.rest);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-20 01:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
targetBuffer.writeCharCode(targetScanner.readChar());
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceBuffer.toString() +
|
|
|
|
"\n\n" +
|
|
|
|
"v" * 50 +
|
|
|
|
"\n\n" +
|
|
|
|
targetBuffer.toString();
|
|
|
|
}
|