Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/parser/scssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,23 @@ export class SCSSParser extends cssParser.Parser {
return super._parseIfTest();
}

public _parseIfBranch() {
// Sass if() branches may be an expression, `!important`, or an expression followed by `!important`.
const node = this.create(nodes.Node);
if (!node.addChild(this._parseIfCondition())) {
return this.finish(node, ParseError.IfConditionExpected, [], [TokenType.SemiColon]);
}
if (!this.accept(TokenType.Colon)) {
return this.finish(node, ParseError.ColonExpected, [], [TokenType.SemiColon]);
}
if (node.addChild(this._parseExpr())) {
node.addChild(this._parsePrio());
} else {
node.addChild(this._tryParsePrio());
}
return this.finish(node);
}

public _parseFunction(): nodes.Function | null {

const pos = this.mark();
Expand Down
10 changes: 10 additions & 0 deletions src/test/css/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Parser } from '../../parser/cssParser.js';
import { TokenType } from '../../parser/cssScanner.js';
import * as nodes from '../../parser/cssNodes.js';
import { ParseError } from '../../parser/cssErrors.js';
import { getCSSLanguageService, TextDocument } from '../../cssLanguageService.js';
import { assertError, assertFunction, assertNode, assertNoNode, assertType } from '../testUtil/parser.js';

suite('CSS - Parser', () => {
Expand Down Expand Up @@ -611,6 +612,15 @@ suite('CSS - Parser', () => {
assertFunction('if(else: white)', parser, parser._parseFunction.bind(parser));
assertError('if()', parser, parser._parseFunction.bind(parser), ParseError.IfConditionExpected);
assertError('if(invalid: black;)', parser, parser._parseFunction.bind(parser), ParseError.IfConditionExpected);
// Plain CSS if() branches do not accept `!important`.
assertError('if(style(--x: yes): red !important; else: blue)', parser, parser._parseFunction.bind(parser), ParseError.RightParenthesisExpected);
assertError('if(style(--x: yes): !important; else: blue)', parser, parser._parseFunction.bind(parser), ParseError.RightParenthesisExpected);
});

test('if function !important branch validation', function () {
const ls = getCSSLanguageService();
const document = TextDocument.create('test://test/test.css', 'css', 0, 'a { color: if(style(--x: yes): red !important; else: blue); }');
assert.ok(ls.doValidation(document, ls.parseStylesheet(document)).length > 0);
});

test('attr', function () {
Expand Down
23 changes: 22 additions & 1 deletion src/test/scss/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
'use strict';

import { suite, test } from 'node:test';
import * as assert from 'node:assert';
import { SCSSParser } from '../../parser/scssParser.js';
import { ParseError } from '../../parser/cssErrors.js';
import { SCSSParseError } from '../../parser/scssErrors.js';
import { getSCSSLanguageService, TextDocument } from '../../cssLanguageService.js';

import { assertNode, assertError, assertNoNode } from '../testUtil/parser.js';

Expand Down Expand Up @@ -763,5 +765,24 @@ suite('SCSS - Parser', () => {
assertNode('if(true, black, white)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass(true): black; else: white;)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($value == \'default\'): flex-gutter(); else: $value;)', parser, parser._parseFunction.bind(parser));
})
});

test('if function !important branch', function() {
const parser = new SCSSParser();
assertNode('if(sass($e): !important; else: null)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($e): red !important; else: null)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($e): null; else: !important)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($e): red; else: null)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($e): red)', parser, parser._parseFunction.bind(parser));
assertNode('if(sass($e): ; else: null)', parser, parser._parseFunction.bind(parser));
assertError('if(!important: red; else: null)', parser, parser._parseFunction.bind(parser), ParseError.RightParenthesisExpected);
assertError('if(sass(!important): red; else: null)', parser, parser._parseFunction.bind(parser), ParseError.RightParenthesisExpected);
assertError('if(sass($e) !important: red; else: null)', parser, parser._parseFunction.bind(parser), ParseError.RightParenthesisExpected);
});

test('if function !important validation', function() {
const ls = getSCSSLanguageService();
const document = TextDocument.create('test://test/test.scss', 'scss', 0, '.a { color: if(sass($e): !important; else: null); }');
assert.deepEqual(ls.doValidation(document, ls.parseStylesheet(document)), []);
});
});