Skip to content

Commit 02f5428

Browse files
committed
Test for sass built-in
1 parent 72caf3d commit 02f5428

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

pkgs/sass_language_services/lib/src/features/find_references/find_references_feature.dart

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class FindReferencesFeature extends GoToDefinitionFeature {
6363
document,
6464
name,
6565
includeDeclaration: context.includeDeclaration,
66+
isBuiltin: builtin != null,
6667
);
6768
stylesheet.accept(visitor);
6869

pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class FindReferencesVisitor
1212
final TextDocument _document;
1313
final String _name;
1414
final bool _includeDeclaration;
15+
final bool _isBuiltin;
1516

1617
FindReferencesVisitor(this._document, this._name,
17-
{bool includeDeclaration = false})
18-
: _includeDeclaration = includeDeclaration;
18+
{bool includeDeclaration = false, bool isBuiltin = false})
19+
: _includeDeclaration = includeDeclaration,
20+
_isBuiltin = isBuiltin;
1921

2022
@override
2123
void visitDeclaration(sass.Declaration node) {
@@ -189,7 +191,12 @@ class FindReferencesVisitor
189191
}
190192
} else {
191193
var name = node.name;
192-
if (!name.contains(_name)) {
194+
195+
// We don't have any good way to avoid name
196+
// collisions with CSS functions, so only include
197+
// builtins when used from a namespace.
198+
var unsafeBuiltin = _isBuiltin && node.namespace == null;
199+
if (!name.contains(_name) || unsafeBuiltin) {
193200
super.visitFunctionExpression(node);
194201
return;
195202
}

pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart

-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ class GoToDefinitionFeature extends LanguageFeature {
179179
}
180180
}
181181

182-
// Could be a Sass built-in module.
183182
return Definition(name, kinds.first, null);
184183
}
185184

pkgs/sass_language_services/test/features/find_references/find_references_test.dart

+55-11
Original file line numberDiff line numberDiff line change
@@ -506,23 +506,42 @@ $map: (
506506
});
507507
});
508508

509-
group('sass mixins', () {
509+
group('placeholder selectors', () {
510510
setUp(() {
511511
ls.cache.clear();
512512
});
513513

514514
test('finds placeholder selectors', () async {
515-
// TODO: test with declaration and @extend usage.
516-
});
517-
});
515+
fs.createDocument(r'''
516+
%theme {
517+
color: var(--color-text);
518+
}
519+
''', uri: '_place.scss');
520+
var document = fs.createDocument(r'''
521+
@use "place";
518522
519-
group('placeholder selectors', () {
520-
setUp(() {
521-
ls.cache.clear();
522-
});
523+
.a {
524+
@extend %theme;
525+
}
526+
''', uri: 'styles.scss');
523527

524-
test('finds placeholder selectors', () async {
525-
// TODO: test with declaration and @extend usage.
528+
var result =
529+
await ls.findReferences(document, at(line: 3, char: 12), context);
530+
531+
var [first, second] = result;
532+
533+
expect(first.uri.toString(), endsWith('styles.scss'));
534+
expect(first.range, StartsAtLine(3));
535+
expect(first.range, EndsAtLine(3));
536+
expect(first.range, StartsAtCharacter(10));
537+
expect(first.range, EndsAtCharacter(16));
538+
539+
expect(second.uri.toString(), endsWith('_place.scss'));
540+
541+
expect(second.range, StartsAtLine(0));
542+
expect(second.range, EndsAtLine(0));
543+
expect(second.range, StartsAtCharacter(0));
544+
expect(second.range, EndsAtCharacter(6));
526545
});
527546
});
528547

@@ -532,7 +551,32 @@ $map: (
532551
});
533552

534553
test('finds sass built-in modules', () async {
535-
// TODO
554+
var particle = fs.createDocument(r'''
555+
@use "sass:color";
556+
557+
$_color: color.scale($color: "#1b1917", $alpha: -75%);
558+
559+
.a {
560+
color: $_color;
561+
transform: scale(1.1); // Does not confuse color.scale for the transform function
562+
}
563+
''', uri: 'particle.scss');
564+
var wave = fs.createDocument(r'''
565+
@use "sass:color";
566+
567+
$_other: color.scale($color: "#1b1917", $alpha: -75%);
568+
''', uri: 'wave.scss');
569+
570+
// Emulate the language server's initial scan.
571+
// Needed since the stylesheets don't all have eachother in their
572+
// module tree, but they all reference the same variable.
573+
ls.parseStylesheet(particle);
574+
ls.parseStylesheet(wave);
575+
576+
var result =
577+
await ls.findReferences(wave, at(line: 2, char: 16), context);
578+
579+
expect(result, hasLength(2));
536580
});
537581
});
538582
}

0 commit comments

Comments
 (0)