Skip to content

Commit

Permalink
Add raw value to number
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodwine committed Oct 11, 2024
1 parent ac5e224 commit 6b77ccd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
34 changes: 25 additions & 9 deletions pkg/sass-parser/lib/src/expression/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {Value} from 'sass';
import {NumberExpression} from '../..';
import * as utils from '../../../test/utils';

Expand Down Expand Up @@ -109,20 +110,31 @@ describe('a number expression', () => {
});

describe('stringifies', () => {
it('unitless', () => {
expect(utils.parseExpression('123').toString()).toBe('123');
});
it('unitless', () =>
expect(utils.parseExpression('123').toString()).toBe('123'));

it('with a unit', () => {
expect(utils.parseExpression('123px').toString()).toBe('123px');
});
it('with a unit', () =>
expect(utils.parseExpression('123px').toString()).toBe('123px'));

it('floating-point number', () =>
expect(utils.parseExpression('3.14').toString()).toBe('3.14'));

it('respects raws', () =>
expect(
new NumberExpression({
value: 123,
raws: {value: '0123.0'},
}).toString()
).toBe('0123.0'));
});

describe('clone', () => {
let original: NumberExpression;

beforeEach(() => {
original = utils.parseExpression('123');
// TODO: remove this once raws are properly parsed.
original.raws.value = '0123.0';
});

describe('with no overrides', () => {
Expand All @@ -135,7 +147,7 @@ describe('a number expression', () => {

it('unit', () => expect(clone.unit).toBeNull());

it('raws', () => expect(clone.raws).toEqual({}));
it('raws', () => expect(clone.raws).toEqual({value: '0123.0'}));

it('source', () => expect(clone.source).toBe(original.source));
});
Expand Down Expand Up @@ -164,10 +176,14 @@ describe('a number expression', () => {

describe('raws', () => {
it('defined', () =>
expect(original.clone({raws: {}}).raws).toEqual({}));
expect(original.clone({raws: {value: '1e3'}}).raws).toEqual({
value: '1e3',
}));

it('undefined', () =>
expect(original.clone({raws: undefined}).raws).toEqual({}));
expect(original.clone({raws: undefined}).raws).toEqual({
value: '0123.0',
}));
});
});
});
Expand Down
14 changes: 11 additions & 3 deletions pkg/sass-parser/lib/src/expression/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ export interface NumberExpressionProps {
*
* @category Expression
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- No raws for a number expression yet.
export interface NumberExpressionRaws {}
export interface NumberExpressionRaws {
/**
* The raw string representation of the number.
*
* Numbers can be represented with or without leading and trailing zeroes, and
* use scientific notation. For example, the following number representations
* have the same value: `1e3`, `1000`, `01000.0`.
*/
value?: string;
}

/**
* An expression representing a number literal in Sass.
Expand Down Expand Up @@ -90,7 +98,7 @@ export class NumberExpression extends Expression {

/** @hidden */
toString(): string {
return this.value + (this.unit ?? '');
return (this.raws?.value ?? this.value) + (this.unit ?? '');
}

/** @hidden */
Expand Down

0 comments on commit 6b77ccd

Please sign in to comment.