-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add boolean expressions to the Sass parser * Add NumberExpression to sass-parser * Add "raw" and "value" to raws. The raws.value is compared with .value. If they are the same, then raws.raw is used when stringifying * Add contributing instructions for sass-parser
- Loading branch information
Showing
18 changed files
with
627 additions
and
8 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
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
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
17 changes: 17 additions & 0 deletions
17
pkg/sass-parser/lib/src/expression/__snapshots__/boolean.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,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`a boolean expression toJSON 1`] = ` | ||
{ | ||
"inputs": [ | ||
{ | ||
"css": "@#{true}", | ||
"hasBOM": false, | ||
"id": "<input css _____>", | ||
}, | ||
], | ||
"raws": {}, | ||
"sassType": "boolean", | ||
"source": <1:4-1:8 in 0>, | ||
"value": true, | ||
} | ||
`; |
18 changes: 18 additions & 0 deletions
18
pkg/sass-parser/lib/src/expression/__snapshots__/number.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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`a number expression toJSON 1`] = ` | ||
{ | ||
"inputs": [ | ||
{ | ||
"css": "@#{123%}", | ||
"hasBOM": false, | ||
"id": "<input css _____>", | ||
}, | ||
], | ||
"raws": {}, | ||
"sassType": "number", | ||
"source": <1:4-1:8 in 0>, | ||
"unit": "%", | ||
"value": 123, | ||
} | ||
`; |
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,122 @@ | ||
// 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 {BooleanExpression} from '../..'; | ||
import * as utils from '../../../test/utils'; | ||
|
||
describe('a boolean expression', () => { | ||
let node: BooleanExpression; | ||
|
||
describe('true', () => { | ||
function describeNode( | ||
description: string, | ||
create: () => BooleanExpression | ||
): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has sassType boolean', () => expect(node.sassType).toBe('boolean')); | ||
|
||
it('is true', () => expect(node.value).toBe(true)); | ||
}); | ||
} | ||
|
||
describeNode('parsed', () => utils.parseExpression('true')); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => new BooleanExpression({value: true}) | ||
); | ||
|
||
describeNode('constructed from ExpressionProps', () => | ||
utils.fromExpressionProps({value: true}) | ||
); | ||
}); | ||
|
||
describe('false', () => { | ||
function describeNode( | ||
description: string, | ||
create: () => BooleanExpression | ||
): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has sassType boolean', () => expect(node.sassType).toBe('boolean')); | ||
|
||
it('is false', () => expect(node.value).toBe(false)); | ||
}); | ||
} | ||
|
||
describeNode('parsed', () => utils.parseExpression('false')); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => new BooleanExpression({value: false}) | ||
); | ||
|
||
describeNode('constructed from ExpressionProps', () => | ||
utils.fromExpressionProps({value: false}) | ||
); | ||
}); | ||
|
||
it('assigned new value', () => { | ||
node = utils.parseExpression('true'); | ||
node.value = false; | ||
expect(node.value).toBe(false); | ||
}); | ||
|
||
describe('stringifies', () => { | ||
it('true', () => { | ||
expect(utils.parseExpression('true').toString()).toBe('true'); | ||
}); | ||
|
||
it('false', () => { | ||
expect(utils.parseExpression('false').toString()).toBe('false'); | ||
}); | ||
}); | ||
|
||
describe('clone', () => { | ||
let original: BooleanExpression; | ||
|
||
beforeEach(() => { | ||
original = utils.parseExpression('true'); | ||
}); | ||
|
||
describe('with no overrides', () => { | ||
let clone: BooleanExpression; | ||
|
||
beforeEach(() => void (clone = original.clone())); | ||
|
||
describe('has the same properties:', () => { | ||
it('value', () => expect(clone.value).toBe(true)); | ||
|
||
it('raws', () => expect(clone.raws).toEqual({})); | ||
|
||
it('source', () => expect(clone.source).toBe(original.source)); | ||
}); | ||
|
||
it('creates a new self', () => expect(clone).not.toBe(original)); | ||
}); | ||
|
||
describe('overrides', () => { | ||
describe('value', () => { | ||
it('defined', () => | ||
expect(original.clone({value: false}).value).toBe(false)); | ||
|
||
it('undefined', () => | ||
expect(original.clone({value: undefined}).value).toBe(true)); | ||
}); | ||
|
||
describe('raws', () => { | ||
it('defined', () => | ||
expect(original.clone({raws: {}}).raws).toEqual({})); | ||
|
||
it('undefined', () => | ||
expect(original.clone({raws: undefined}).raws).toEqual({})); | ||
}); | ||
}); | ||
}); | ||
|
||
it('toJSON', () => expect(utils.parseExpression('true')).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,82 @@ | ||
// 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 {LazySource} from '../lazy-source'; | ||
import type * as sassInternal from '../sass-internal'; | ||
import * as utils from '../utils'; | ||
import {Expression} from '.'; | ||
|
||
/** | ||
* The initializer properties for {@link BooleanExpression}. | ||
* | ||
* @category Expression | ||
*/ | ||
export interface BooleanExpressionProps { | ||
value: boolean; | ||
raws?: BooleanExpressionRaws; | ||
} | ||
|
||
/** | ||
* Raws indicating how to precisely serialize a {@link BooleanExpression}. | ||
* | ||
* @category Expression | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- No raws for a boolean expression yet. | ||
export interface BooleanExpressionRaws {} | ||
|
||
/** | ||
* An expression representing a boolean literal in Sass. | ||
* | ||
* @category Expression | ||
*/ | ||
export class BooleanExpression extends Expression { | ||
readonly sassType = 'boolean' as const; | ||
declare raws: BooleanExpressionRaws; | ||
|
||
/** The boolean value of this expression. */ | ||
get value(): boolean { | ||
return this._value; | ||
} | ||
set value(value: boolean) { | ||
// TODO - postcss/postcss#1957: Mark this as dirty | ||
this._value = value; | ||
} | ||
private _value!: boolean; | ||
|
||
constructor(defaults: BooleanExpressionProps); | ||
/** @hidden */ | ||
constructor(_: undefined, inner: sassInternal.BooleanExpression); | ||
constructor(defaults?: object, inner?: sassInternal.BooleanExpression) { | ||
super(defaults); | ||
if (inner) { | ||
this.source = new LazySource(inner); | ||
this.value = inner.value; | ||
} else { | ||
this.value ??= false; | ||
} | ||
} | ||
|
||
clone(overrides?: Partial<BooleanExpressionProps>): this { | ||
return utils.cloneNode(this, overrides, ['raws', 'value']); | ||
} | ||
|
||
toJSON(): object; | ||
/** @hidden */ | ||
toJSON(_: string, inputs: Map<postcss.Input, number>): object; | ||
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object { | ||
return utils.toJSON(this, ['value'], inputs); | ||
} | ||
|
||
/** @hidden */ | ||
toString(): string { | ||
return this.value ? 'true' : 'false'; | ||
} | ||
|
||
/** @hidden */ | ||
get nonStatementChildren(): ReadonlyArray<Expression> { | ||
return []; | ||
} | ||
} |
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
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
Oops, something went wrong.