-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BooleanExpression
and NumberExpression
to sass-parser
#2376
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
22393a4
Add boolean expressions to the Sass parser
Goodwine 91e992c
Add NumberExpression to sass-parser
Goodwine 06ba95d
Add raw value to number
Goodwine 10c5498
Add "raw" and "value" to raws. The raws.value is compared with .value…
Goodwine 14ab832
ipdate changelog and version
Goodwine 7c17cfe
check for undefined and falsy raw
Goodwine bb563be
add changelog again
Goodwine 00e8f2b
add note about running dart run grinder before test
Goodwine 152a6c4
update changelogs
Goodwine 5fa276a
pr feedback
Goodwine 093b522
rename NumberExpression raws.value to raws.number to be less confusin…
Goodwine c926c14
feedback
Goodwine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can update this to 0.2.7 if this version has already come out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just cut a release for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updating all three in tandem can be confusing 😅 - I wonder if we could find a way to more easily update these