From 02f5428e040b6f5389ee9a83970e6f4ae8b24ee0 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Sat, 23 Nov 2024 19:50:53 +0100 Subject: [PATCH] Test for sass built-in --- .../find_references_feature.dart | 1 + .../find_references_visitor.dart | 13 +++- .../go_to_definition_feature.dart | 1 - .../find_references/find_references_test.dart | 66 +++++++++++++++---- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/pkgs/sass_language_services/lib/src/features/find_references/find_references_feature.dart b/pkgs/sass_language_services/lib/src/features/find_references/find_references_feature.dart index 8bba3c2..91834bc 100644 --- a/pkgs/sass_language_services/lib/src/features/find_references/find_references_feature.dart +++ b/pkgs/sass_language_services/lib/src/features/find_references/find_references_feature.dart @@ -63,6 +63,7 @@ class FindReferencesFeature extends GoToDefinitionFeature { document, name, includeDeclaration: context.includeDeclaration, + isBuiltin: builtin != null, ); stylesheet.accept(visitor); diff --git a/pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart b/pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart index 0da019c..2f4d8bd 100644 --- a/pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart +++ b/pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart @@ -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) { @@ -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; } diff --git a/pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart b/pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart index a51e21d..6869ed4 100644 --- a/pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart +++ b/pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart @@ -179,7 +179,6 @@ class GoToDefinitionFeature extends LanguageFeature { } } - // Could be a Sass built-in module. return Definition(name, kinds.first, null); } diff --git a/pkgs/sass_language_services/test/features/find_references/find_references_test.dart b/pkgs/sass_language_services/test/features/find_references/find_references_test.dart index 0811477..78fe6f1 100644 --- a/pkgs/sass_language_services/test/features/find_references/find_references_test.dart +++ b/pkgs/sass_language_services/test/features/find_references/find_references_test.dart @@ -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)); }); }); @@ -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)); }); }); }