-
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.
- Loading branch information
Showing
9 changed files
with
327 additions
and
7 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// 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 {NumberExpression} from '../..'; | ||
import * as utils from '../../../test/utils'; | ||
|
||
describe('a number expression', () => { | ||
let node: NumberExpression; | ||
|
||
describe('unitless', () => { | ||
function describeNode( | ||
description: string, | ||
create: () => NumberExpression | ||
): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has sassType number', () => expect(node.sassType).toBe('number')); | ||
|
||
it('is a number', () => expect(node.value).toBe(123)); | ||
|
||
it('has no unit', () => expect(node.unit).toBeNull()); | ||
}); | ||
} | ||
|
||
describeNode('parsed', () => utils.parseExpression('123')); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => | ||
new NumberExpression({ | ||
value: 123, | ||
}) | ||
); | ||
|
||
describeNode('constructed from ExpressionProps', () => | ||
utils.fromExpressionProps({ | ||
value: 123, | ||
}) | ||
); | ||
}); | ||
|
||
describe('with a unit', () => { | ||
function describeNode( | ||
description: string, | ||
create: () => NumberExpression | ||
): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has sassType number', () => expect(node.sassType).toBe('number')); | ||
|
||
it('is a number', () => expect(node.value).toBe(123)); | ||
|
||
it('has a unit', () => expect(node.unit).toBe('px')); | ||
}); | ||
} | ||
|
||
describeNode('parsed', () => utils.parseExpression('123px')); | ||
|
||
describeNode( | ||
'constructed manually', | ||
() => | ||
new NumberExpression({ | ||
value: 123, | ||
unit: 'px', | ||
}) | ||
); | ||
|
||
describeNode('constructed from ExpressionProps', () => | ||
utils.fromExpressionProps({ | ||
value: 123, | ||
unit: 'px', | ||
}) | ||
); | ||
}); | ||
|
||
describe('floating-point number', () => { | ||
describe('unitless', () => { | ||
beforeEach(() => void (node = utils.parseExpression('3.14'))); | ||
|
||
it('value', () => expect(node.value).toBe(3.14)); | ||
|
||
it('unit', () => expect(node.unit).toBeNull()); | ||
}); | ||
|
||
describe('with a unit', () => { | ||
beforeEach(() => void (node = utils.parseExpression('1.618px'))); | ||
|
||
it('value', () => expect(node.value).toBe(1.618)); | ||
|
||
it('unit', () => expect(node.unit).toBe('px')); | ||
}); | ||
}); | ||
|
||
describe('assigned new', () => { | ||
beforeEach(() => void (node = utils.parseExpression('123'))); | ||
|
||
it('value', () => { | ||
node.value = 456; | ||
expect(node.value).toBe(456); | ||
}); | ||
|
||
it('unit', () => { | ||
node.unit = 'px'; | ||
expect(node.unit).toBe('px'); | ||
}); | ||
}); | ||
|
||
describe('stringifies', () => { | ||
it('unitless', () => { | ||
expect(utils.parseExpression('123').toString()).toBe('123'); | ||
}); | ||
|
||
it('with a unit', () => { | ||
expect(utils.parseExpression('123px').toString()).toBe('123px'); | ||
}); | ||
}); | ||
|
||
describe('clone', () => { | ||
let original: NumberExpression; | ||
|
||
beforeEach(() => { | ||
original = utils.parseExpression('123'); | ||
}); | ||
|
||
describe('with no overrides', () => { | ||
let clone: NumberExpression; | ||
|
||
beforeEach(() => void (clone = original.clone())); | ||
|
||
describe('has the same properties:', () => { | ||
it('value', () => expect(clone.value).toBe(123)); | ||
|
||
it('unit', () => expect(clone.unit).toBeNull()); | ||
|
||
it('raws', () => expect(clone.raws).toEqual({})); | ||
|
||
it('source', () => expect(clone.source).toBe(original.source)); | ||
}); | ||
|
||
describe('creates a new', () => { | ||
it('self', () => expect(clone).not.toBe(original)); | ||
}); | ||
}); | ||
|
||
describe('overrides', () => { | ||
describe('value', () => { | ||
it('defined', () => | ||
expect(original.clone({value: 123}).value).toBe(123)); | ||
|
||
it('undefined', () => | ||
expect(original.clone({value: undefined}).value).toBe(123)); | ||
}); | ||
|
||
describe('unit', () => { | ||
it('defined', () => | ||
expect(original.clone({unit: 'px'}).unit).toBe('px')); | ||
|
||
it('undefined', () => | ||
expect(original.clone({unit: undefined}).unit).toBeNull()); | ||
}); | ||
|
||
describe('raws', () => { | ||
it('defined', () => | ||
expect(original.clone({raws: {}}).raws).toEqual({})); | ||
|
||
it('undefined', () => | ||
expect(original.clone({raws: undefined}).raws).toEqual({})); | ||
}); | ||
}); | ||
}); | ||
|
||
it('toJSON', () => expect(utils.parseExpression('123%')).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,100 @@ | ||
// 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 NumberExpression}. | ||
* | ||
* @category Expression | ||
*/ | ||
export interface NumberExpressionProps { | ||
value: number; | ||
unit?: string; | ||
raws?: NumberExpressionRaws; | ||
} | ||
|
||
/** | ||
* Raws indicating how to precisely serialize a {@link NumberExpression}. | ||
* | ||
* @category Expression | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- No raws for a number expression yet. | ||
export interface NumberExpressionRaws {} | ||
|
||
/** | ||
* An expression representing a number literal in Sass. | ||
* | ||
* @category Expression | ||
*/ | ||
export class NumberExpression extends Expression { | ||
readonly sassType = 'number' as const; | ||
declare raws: NumberExpressionRaws; | ||
|
||
/** The numeric value of this expression. */ | ||
get value(): number { | ||
return this._value; | ||
} | ||
set value(value: number) { | ||
// TODO - postcss/postcss#1957: Mark this as dirty | ||
this._value = value; | ||
} | ||
private _value!: number; | ||
|
||
/** The denominator units of this number. */ | ||
get unit(): string | null { | ||
return this._unit; | ||
} | ||
set unit(unit: string | null) { | ||
// TODO - postcss/postcss#1957: Mark this as dirty | ||
this._unit = unit; | ||
} | ||
private _unit!: string | null; | ||
|
||
/** Whether the number is unitless. */ | ||
isUnitless(): boolean { | ||
return this.unit === null; | ||
} | ||
|
||
constructor(defaults: NumberExpressionProps); | ||
/** @hidden */ | ||
constructor(_: undefined, inner: sassInternal.NumberExpression); | ||
constructor(defaults?: object, inner?: sassInternal.NumberExpression) { | ||
super(defaults); | ||
if (inner) { | ||
this.source = new LazySource(inner); | ||
this.value = inner.value; | ||
this.unit = inner.unit; | ||
} else { | ||
this.value ??= 0; | ||
this.unit ??= null; | ||
} | ||
} | ||
|
||
clone(overrides?: Partial<NumberExpressionProps>): this { | ||
return utils.cloneNode(this, overrides, ['raws', 'value', 'unit']); | ||
} | ||
|
||
toJSON(): object; | ||
/** @hidden */ | ||
toJSON(_: string, inputs: Map<postcss.Input, number>): object; | ||
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object { | ||
return utils.toJSON(this, ['value', 'unit'], inputs); | ||
} | ||
|
||
/** @hidden */ | ||
toString(): string { | ||
return this.value + (this.unit ?? ''); | ||
} | ||
|
||
/** @hidden */ | ||
get nonStatementChildren(): ReadonlyArray<Expression> { | ||
return []; | ||
} | ||
} |
Oops, something went wrong.