Skip to content

Commit 98922d3

Browse files
committed
fix: scss and less language sercvice was using css langauage service
1 parent e69c8ad commit 98922d3

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/css-ls.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as cssLangService from 'vscode-css-languageservice';
22

3-
const service = cssLangService.getCSSLanguageService();
3+
const cssService = cssLangService.getCSSLanguageService();
4+
const lessService = cssLangService.getLESSLanguageService();
5+
const scssService = cssLangService.getSCSSLanguageService();
46
export const CSS_MODES = {
57
CSS: "css",
68
LESS: "less",
@@ -14,6 +16,17 @@ export const DiagnosticSeverity = {
1416
hint: 4
1517
};
1618

19+
function _getLanguageServiceToUse(cssMode) {
20+
switch (cssMode) {
21+
case "less": return lessService;
22+
case "scss": return scssService;
23+
case "css": return cssService;
24+
default:
25+
console.error("Unknown language mode: ", cssMode, "passed to css language service");
26+
return cssService;
27+
}
28+
}
29+
1730
function getTextDocument(text, languageID, filePath = "file://placeholder.css") {
1831
return cssLangService.TextDocument.create(filePath, languageID, 1, text);
1932
}
@@ -27,6 +40,7 @@ function getTextDocument(text, languageID, filePath = "file://placeholder.css")
2740
*/
2841
export function getAllSymbols(text, cssMode, filePath) {
2942
const textDocument = getTextDocument(text, cssMode, filePath);
43+
const service = _getLanguageServiceToUse(cssMode);
3044
const stylesheet = service.parseStylesheet(textDocument);
3145
const output = [];
3246
for(let symbol of service.findDocumentSymbols(textDocument, stylesheet)) {
@@ -81,6 +95,7 @@ export function getAllSymbols(text, cssMode, filePath) {
8195
*/
8296
export function validateCSS(text, cssMode, filePath, lintSettings) {
8397
const textDocument = getTextDocument(text, cssMode, filePath);
98+
const service = _getLanguageServiceToUse(cssMode);
8499
const stylesheet = service.parseStylesheet(textDocument);
85100
return service.doValidation(textDocument, stylesheet, {
86101
validate: true,

test/test-css.worker.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe(`web worker CSS Language tests`, async function () {
7171
worker.postMessage({command: `getAllSymbols`, text, cssMode: "SCSS", filePath: "file:///c.scss"});
7272
let output = await waitForWorkerMessage(`getAllSymbols`, 1000);
7373
const symbols = output.symbols;
74-
expect(symbols).to.deep.equal(['.info', '.alert', '#success']);
74+
expect(symbols).to.deep.equal(["theme", '.info', '.alert', '#success']);
7575
});
7676

7777
/**
@@ -369,4 +369,24 @@ describe(`web worker CSS Language tests`, async function () {
369369
const symbols = output.diag;
370370
expect(symbols).to.deep.equal(cssValidationData["emptyRules"]);
371371
});
372+
373+
it("should validate less emptyRules by default", async function () {
374+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
375+
messageFromWorker = null;
376+
const text = `// less supports comments\n.box {}`;
377+
worker.postMessage({command: `validateCSS`, text, cssMode: "LESS", filePath: "file:///c.less"});
378+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
379+
const symbols = output.diag;
380+
expect(symbols).to.deep.equal(cssValidationData["emptyRulesLESS"]);
381+
});
382+
383+
it("should validate scss emptyRules by default", async function () {
384+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
385+
messageFromWorker = null;
386+
const text = `// less supports comments\n.box {}`;
387+
worker.postMessage({command: `validateCSS`, text, cssMode: "SCSS", filePath: "file:///c.scss"});
388+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
389+
const symbols = output.diag;
390+
expect(symbols).to.deep.equal(cssValidationData["emptyRulesSCSS"]);
391+
});
372392
});

test/test-files/css-tests/b.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// this is a test
12
#header {
23
color: black;
34
.navigation {

test/test-files/cssValidationData.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@
6262
}
6363
}
6464
}],
65+
"emptyRulesLESS" : [{
66+
"code": "emptyRules",
67+
"source": "less",
68+
"message": "Do not use empty rulesets",
69+
"severity": 2,
70+
"range": {
71+
"start": {
72+
"line": 1,
73+
"character": 0
74+
},
75+
"end": {
76+
"line": 1,
77+
"character": 4
78+
}
79+
}
80+
}],
81+
"emptyRulesSCSS": [{
82+
"code": "emptyRules",
83+
"source": "scss",
84+
"message": "Do not use empty rulesets",
85+
"severity": 2,
86+
"range": {
87+
"start": {
88+
"line": 1,
89+
"character": 0
90+
},
91+
"end": {
92+
"line": 1,
93+
"character": 4
94+
}
95+
}
96+
}],
6597
"zeroUnits": [{
6698
"code": "zeroUnits",
6799
"source": "css",

0 commit comments

Comments
 (0)