Skip to content

Commit

Permalink
Test for sass built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 23, 2024
1 parent 72caf3d commit 02f5428
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FindReferencesFeature extends GoToDefinitionFeature {
document,
name,
includeDeclaration: context.includeDeclaration,
isBuiltin: builtin != null,
);
stylesheet.accept(visitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class FindReferencesVisitor
final TextDocument _document;
final String _name;
final bool _includeDeclaration;
final bool _isBuiltin;

FindReferencesVisitor(this._document, this._name,
{bool includeDeclaration = false})
: _includeDeclaration = includeDeclaration;
{bool includeDeclaration = false, bool isBuiltin = false})
: _includeDeclaration = includeDeclaration,
_isBuiltin = isBuiltin;

@override
void visitDeclaration(sass.Declaration node) {
Expand Down Expand Up @@ -189,7 +191,12 @@ class FindReferencesVisitor
}
} else {
var name = node.name;
if (!name.contains(_name)) {

// We don't have any good way to avoid name
// collisions with CSS functions, so only include
// builtins when used from a namespace.
var unsafeBuiltin = _isBuiltin && node.namespace == null;
if (!name.contains(_name) || unsafeBuiltin) {
super.visitFunctionExpression(node);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class GoToDefinitionFeature extends LanguageFeature {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,42 @@ $map: (
});
});

group('sass mixins', () {
group('placeholder selectors', () {
setUp(() {
ls.cache.clear();
});

test('finds placeholder selectors', () async {
// TODO: test with declaration and @extend usage.
});
});
fs.createDocument(r'''
%theme {
color: var(--color-text);
}
''', uri: '_place.scss');
var document = fs.createDocument(r'''
@use "place";
group('placeholder selectors', () {
setUp(() {
ls.cache.clear();
});
.a {
@extend %theme;
}
''', uri: 'styles.scss');

test('finds placeholder selectors', () async {
// TODO: test with declaration and @extend usage.
var result =
await ls.findReferences(document, at(line: 3, char: 12), context);

var [first, second] = result;

expect(first.uri.toString(), endsWith('styles.scss'));
expect(first.range, StartsAtLine(3));
expect(first.range, EndsAtLine(3));
expect(first.range, StartsAtCharacter(10));
expect(first.range, EndsAtCharacter(16));

expect(second.uri.toString(), endsWith('_place.scss'));

expect(second.range, StartsAtLine(0));
expect(second.range, EndsAtLine(0));
expect(second.range, StartsAtCharacter(0));
expect(second.range, EndsAtCharacter(6));
});
});

Expand All @@ -532,7 +551,32 @@ $map: (
});

test('finds sass built-in modules', () async {
// TODO
var particle = fs.createDocument(r'''
@use "sass:color";
$_color: color.scale($color: "#1b1917", $alpha: -75%);
.a {
color: $_color;
transform: scale(1.1); // Does not confuse color.scale for the transform function
}
''', uri: 'particle.scss');
var wave = fs.createDocument(r'''
@use "sass:color";
$_other: color.scale($color: "#1b1917", $alpha: -75%);
''', uri: 'wave.scss');

// Emulate the language server's initial scan.
// Needed since the stylesheets don't all have eachother in their
// module tree, but they all reference the same variable.
ls.parseStylesheet(particle);
ls.parseStylesheet(wave);

var result =
await ls.findReferences(wave, at(line: 2, char: 16), context);

expect(result, hasLength(2));
});
});
}

0 comments on commit 02f5428

Please sign in to comment.