Skip to content

Commit

Permalink
Add "raw" and "value" to raws. The raws.value is compared with .value…
Browse files Browse the repository at this point in the history
…. If they are the same, then raws.raw is used when stringifying
  • Loading branch information
Goodwine committed Oct 11, 2024
1 parent 6b77ccd commit c1d524a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
35 changes: 22 additions & 13 deletions pkg/sass-parser/lib/src/expression/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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 @@ -119,13 +118,23 @@ describe('a number expression', () => {
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('raws', () => {
it('with the same raw value as the expression', () =>
expect(
new NumberExpression({
value: 123,
raws: {raw: 'hello', value: 123},
}).toString()
).toBe('hello'));

it('with a different raw value than the expression', () =>
expect(
new NumberExpression({
value: 123,
raws: {raw: 'hello', value: 234},
}).toString()
).toBe('123'));
});
});

describe('clone', () => {
Expand All @@ -134,7 +143,7 @@ describe('a number expression', () => {
beforeEach(() => {
original = utils.parseExpression('123');
// TODO: remove this once raws are properly parsed.
original.raws.value = '0123.0';
original.raws.raw = '0123.0';
});

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

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

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

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

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

it('undefined', () =>
expect(original.clone({raws: undefined}).raws).toEqual({
value: '0123.0',
raw: '0123.0',
}));
});
});
Expand Down
17 changes: 15 additions & 2 deletions pkg/sass-parser/lib/src/expression/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ export interface NumberExpressionRaws {
* use scientific notation. For example, the following number representations
* have the same value: `1e3`, `1000`, `01000.0`.
*/
value?: string;
raw?: string;

/**
* The numeric value for the raw string that represents the number.
*
* This `value` is compared with {@link NumberExpression.value}. If they
* match, then the {@link raw} string is output when stringifying the
* expression. Otherwise, the {@link raw} string is ignored, and the
* {@link NumberExpression.value} is output.
*/
value?: number;
}

/**
Expand Down Expand Up @@ -98,7 +108,10 @@ export class NumberExpression extends Expression {

/** @hidden */
toString(): string {
return (this.raws?.value ?? this.value) + (this.unit ?? '');
if (this.raws?.raw != null && this.raws?.value === this.value) {
return this.raws.raw + (this.unit ?? '');
}
return this.value + (this.unit ?? '');
}

/** @hidden */
Expand Down

0 comments on commit c1d524a

Please sign in to comment.