-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add sass-parser support for the
@while
rule (#2410)
- sass-parser/0.4.14
- sass-parser/0.4.13
- sass-parser/0.4.12
- sass-parser/0.4.9
- sass-parser/0.4.8
- sass-parser/0.4.7
- sass-parser/0.4.6
- sass-parser/0.4.5
- sass-parser/0.4.4
- sass-parser/0.4.3
- sass_api/15.2.0
- sass_api/15.1.0
- sass_api/15.0.4
- sass_api/15.0.3
- sass_api/15.0.2
- sass_api/15.0.1
- sass_api/14.4.0
- sass_api/14.3.0
- sass_api/14.2.0
- sass_api/14.1.3
- sass_api/14.1.2
- 1.85.0
- 1.84.0
- 1.83.4
- 1.83.3
- 1.83.2
- 1.83.1
- 1.83.0
- 1.82.0
- 1.81.1
- 1.81.0
- 1.80.7
- 1.80.6
Showing
8 changed files
with
428 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
## 0.4.3-dev | ||
|
||
* No user-visible changes. | ||
* Add support for parsing the `@while` rule. | ||
|
||
## 0.4.2 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
pkg/sass-parser/lib/src/statement/__snapshots__/while-rule.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`a @while rule toJSON 1`] = ` | ||
{ | ||
"inputs": [ | ||
{ | ||
"css": "@while foo {}", | ||
"hasBOM": false, | ||
"id": "<input css _____>", | ||
}, | ||
], | ||
"name": "while", | ||
"nodes": [], | ||
"params": "foo", | ||
"raws": {}, | ||
"sassType": "while-rule", | ||
"source": <1:1-1:14 in 0>, | ||
"type": "atrule", | ||
"whileCondition": <foo>, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Copyright 2024 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import {GenericAtRule, StringExpression, WhileRule, sass, scss} from '../..'; | ||
import * as utils from '../../../test/utils'; | ||
|
||
describe('a @while rule', () => { | ||
let node: WhileRule; | ||
describe('with empty children', () => { | ||
function describeNode(description: string, create: () => WhileRule): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has a name', () => expect(node.name.toString()).toBe('while')); | ||
|
||
it('has an expression', () => | ||
expect(node).toHaveStringExpression('whileCondition', 'foo')); | ||
|
||
it('has matching params', () => expect(node.params).toBe('foo')); | ||
|
||
it('has empty nodes', () => expect(node.nodes).toEqual([])); | ||
}); | ||
} | ||
|
||
describeNode( | ||
'parsed as SCSS', | ||
() => scss.parse('@while foo {}').nodes[0] as WhileRule, | ||
); | ||
|
||
describeNode( | ||
'parsed as Sass', | ||
() => sass.parse('@while foo').nodes[0] as WhileRule, | ||
); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => | ||
new WhileRule({ | ||
whileCondition: {text: 'foo'}, | ||
}), | ||
); | ||
|
||
describeNode('constructed from ChildProps', () => | ||
utils.fromChildProps({ | ||
whileCondition: {text: 'foo'}, | ||
}), | ||
); | ||
}); | ||
|
||
describe('with a child', () => { | ||
function describeNode(description: string, create: () => WhileRule): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has a name', () => expect(node.name.toString()).toBe('while')); | ||
|
||
it('has an expression', () => | ||
expect(node).toHaveStringExpression('whileCondition', 'foo')); | ||
|
||
it('has matching params', () => expect(node.params).toBe('foo')); | ||
|
||
it('has a child node', () => { | ||
expect(node.nodes).toHaveLength(1); | ||
expect(node.nodes[0]).toBeInstanceOf(GenericAtRule); | ||
expect(node.nodes[0]).toHaveProperty('name', 'child'); | ||
}); | ||
}); | ||
} | ||
|
||
describeNode( | ||
'parsed as SCSS', | ||
() => scss.parse('@while foo {@child}').nodes[0] as WhileRule, | ||
); | ||
|
||
describeNode( | ||
'parsed as Sass', | ||
() => sass.parse('@while foo\n @child').nodes[0] as WhileRule, | ||
); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => | ||
new WhileRule({ | ||
whileCondition: {text: 'foo'}, | ||
nodes: [{name: 'child'}], | ||
}), | ||
); | ||
|
||
describeNode('constructed from ChildProps', () => | ||
utils.fromChildProps({ | ||
whileCondition: {text: 'foo'}, | ||
nodes: [{name: 'child'}], | ||
}), | ||
); | ||
}); | ||
|
||
describe('throws an error when assigned a new', () => { | ||
beforeEach( | ||
() => void (node = new WhileRule({whileCondition: {text: 'foo'}})), | ||
); | ||
|
||
it('name', () => expect(() => (node.name = 'bar')).toThrow()); | ||
|
||
it('params', () => expect(() => (node.params = 'true')).toThrow()); | ||
}); | ||
|
||
describe('assigned a new expression', () => { | ||
beforeEach(() => { | ||
node = scss.parse('@while foo {}').nodes[0] as WhileRule; | ||
}); | ||
|
||
it("removes the old expression's parent", () => { | ||
const oldExpression = node.whileCondition; | ||
node.whileCondition = {text: 'bar'}; | ||
expect(oldExpression.parent).toBeUndefined(); | ||
}); | ||
|
||
it("assigns the new expression's parent", () => { | ||
const expression = new StringExpression({text: 'bar'}); | ||
node.whileCondition = expression; | ||
expect(expression.parent).toBe(node); | ||
}); | ||
|
||
it('assigns the expression explicitly', () => { | ||
const expression = new StringExpression({text: 'bar'}); | ||
node.whileCondition = expression; | ||
expect(node.whileCondition).toBe(expression); | ||
}); | ||
|
||
it('assigns the expression as ExpressionProps', () => { | ||
node.whileCondition = {text: 'bar'}; | ||
expect(node).toHaveStringExpression('whileCondition', 'bar'); | ||
}); | ||
}); | ||
|
||
describe('stringifies', () => { | ||
describe('to SCSS', () => { | ||
it('with default raws', () => | ||
expect( | ||
new WhileRule({ | ||
whileCondition: {text: 'foo'}, | ||
}).toString(), | ||
).toBe('@while foo {}')); | ||
|
||
it('with afterName', () => | ||
expect( | ||
new WhileRule({ | ||
whileCondition: {text: 'foo'}, | ||
raws: {afterName: '/**/'}, | ||
}).toString(), | ||
).toBe('@while/**/foo {}')); | ||
|
||
it('with between', () => | ||
expect( | ||
new WhileRule({ | ||
whileCondition: {text: 'foo'}, | ||
raws: {between: '/**/'}, | ||
}).toString(), | ||
).toBe('@while foo/**/{}')); | ||
}); | ||
}); | ||
|
||
describe('clone', () => { | ||
let original: WhileRule; | ||
beforeEach(() => { | ||
original = scss.parse('@while foo {}').nodes[0] as WhileRule; | ||
// TODO: remove this once raws are properly parsed | ||
original.raws.between = ' '; | ||
}); | ||
|
||
describe('with no overrides', () => { | ||
let clone: WhileRule; | ||
beforeEach(() => void (clone = original.clone())); | ||
|
||
describe('has the same properties:', () => { | ||
it('params', () => expect(clone.params).toBe('foo')); | ||
|
||
it('whileCondition', () => | ||
expect(clone).toHaveStringExpression('whileCondition', 'foo')); | ||
|
||
it('raws', () => expect(clone.raws).toEqual({between: ' '})); | ||
|
||
it('source', () => expect(clone.source).toBe(original.source)); | ||
}); | ||
|
||
describe('creates a new', () => { | ||
it('self', () => expect(clone).not.toBe(original)); | ||
|
||
for (const attr of ['whileCondition', 'raws'] as const) { | ||
it(attr, () => expect(clone[attr]).not.toBe(original[attr])); | ||
} | ||
}); | ||
}); | ||
|
||
describe('overrides', () => { | ||
describe('raws', () => { | ||
it('defined', () => | ||
expect(original.clone({raws: {afterName: ' '}}).raws).toEqual({ | ||
afterName: ' ', | ||
})); | ||
|
||
it('undefined', () => | ||
expect(original.clone({raws: undefined}).raws).toEqual({ | ||
between: ' ', | ||
})); | ||
}); | ||
|
||
describe('whileCondition', () => { | ||
describe('defined', () => { | ||
let clone: WhileRule; | ||
beforeEach(() => { | ||
clone = original.clone({whileCondition: {text: 'bar'}}); | ||
}); | ||
|
||
it('changes params', () => expect(clone.params).toBe('bar')); | ||
|
||
it('changes whileCondition', () => | ||
expect(clone).toHaveStringExpression('whileCondition', 'bar')); | ||
}); | ||
|
||
describe('undefined', () => { | ||
let clone: WhileRule; | ||
beforeEach(() => { | ||
clone = original.clone({whileCondition: undefined}); | ||
}); | ||
|
||
it('preserves params', () => expect(clone.params).toBe('foo')); | ||
|
||
it('preserves whileCondition', () => | ||
expect(clone).toHaveStringExpression('whileCondition', 'foo')); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('toJSON', () => | ||
expect(scss.parse('@while foo {}').nodes[0]).toMatchSnapshot()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2024 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import * as postcss from 'postcss'; | ||
import type {AtRuleRaws} from 'postcss/lib/at-rule'; | ||
|
||
import {convertExpression} from '../expression/convert'; | ||
import {Expression, ExpressionProps} from '../expression'; | ||
import {fromProps} from '../expression/from-props'; | ||
import {LazySource} from '../lazy-source'; | ||
import type * as sassInternal from '../sass-internal'; | ||
import * as utils from '../utils'; | ||
import { | ||
ChildNode, | ||
ContainerProps, | ||
NewNode, | ||
Statement, | ||
StatementWithChildren, | ||
appendInternalChildren, | ||
normalize, | ||
} from '.'; | ||
import {_AtRule} from './at-rule-internal'; | ||
import {interceptIsClean} from './intercept-is-clean'; | ||
import * as sassParser from '../..'; | ||
|
||
/** | ||
* The set of raws supported by {@link WhileRule}. | ||
* | ||
* @category Statement | ||
*/ | ||
export type WhileRuleRaws = Omit<AtRuleRaws, 'params'>; | ||
|
||
/** | ||
* The initializer properties for {@link WhileRule}. | ||
* | ||
* @category Statement | ||
*/ | ||
export type WhileRuleProps = ContainerProps & { | ||
raws?: WhileRuleRaws; | ||
whileCondition: Expression | ExpressionProps; | ||
}; | ||
|
||
/** | ||
* A `@while` rule. Extends [`postcss.AtRule`]. | ||
* | ||
* [`postcss.AtRule`]: https://postcss.org/api/#atrule | ||
* | ||
* @category Statement | ||
*/ | ||
export class WhileRule | ||
extends _AtRule<Partial<WhileRuleProps>> | ||
implements Statement | ||
{ | ||
readonly sassType = 'while-rule' as const; | ||
declare parent: StatementWithChildren | undefined; | ||
declare raws: WhileRuleRaws; | ||
declare nodes: ChildNode[]; | ||
|
||
get name(): string { | ||
return 'while'; | ||
} | ||
set name(value: string) { | ||
throw new Error("WhileRule.name can't be overwritten."); | ||
} | ||
|
||
get params(): string { | ||
return this.whileCondition.toString(); | ||
} | ||
set params(value: string | number | undefined) { | ||
throw new Error("WhileRule.params can't be overwritten."); | ||
} | ||
|
||
/** The expresison whose value is emitted when the while rule is executed. */ | ||
get whileCondition(): Expression { | ||
return this._whileCondition!; | ||
} | ||
set whileCondition(whileCondition: Expression | ExpressionProps) { | ||
if (this._whileCondition) this._whileCondition.parent = undefined; | ||
if (!('sassType' in whileCondition)) { | ||
whileCondition = fromProps(whileCondition); | ||
} | ||
if (whileCondition) whileCondition.parent = this; | ||
this._whileCondition = whileCondition; | ||
} | ||
private _whileCondition?: Expression; | ||
|
||
constructor(defaults: WhileRuleProps); | ||
/** @hidden */ | ||
constructor(_: undefined, inner: sassInternal.WhileRule); | ||
constructor(defaults?: WhileRuleProps, inner?: sassInternal.WhileRule) { | ||
super(defaults as unknown as postcss.AtRuleProps); | ||
this.nodes ??= []; | ||
|
||
if (inner) { | ||
this.source = new LazySource(inner); | ||
this.whileCondition = convertExpression(inner.condition); | ||
appendInternalChildren(this, inner.children); | ||
} | ||
} | ||
|
||
clone(overrides?: Partial<WhileRuleProps>): this { | ||
return utils.cloneNode(this, overrides, ['raws', 'whileCondition']); | ||
} | ||
|
||
toJSON(): object; | ||
/** @hidden */ | ||
toJSON(_: string, inputs: Map<postcss.Input, number>): object; | ||
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object { | ||
return utils.toJSON( | ||
this, | ||
['name', 'whileCondition', 'params', 'nodes'], | ||
inputs, | ||
); | ||
} | ||
|
||
/** @hidden */ | ||
toString( | ||
stringifier: postcss.Stringifier | postcss.Syntax = sassParser.scss | ||
.stringify, | ||
): string { | ||
return super.toString(stringifier); | ||
} | ||
|
||
/** @hidden */ | ||
get nonStatementChildren(): ReadonlyArray<Expression> { | ||
return [this.whileCondition]; | ||
} | ||
|
||
/** @hidden */ | ||
normalize(node: NewNode, sample?: postcss.Node): ChildNode[] { | ||
return normalize(this, node, sample); | ||
} | ||
} | ||
|
||
interceptIsClean(WhileRule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters