Skip to content

Commit a4e89b0

Browse files
committed
Fix range and round properties of calculation function
Both was supposed to be set based on node.definition but were actually set based on the substitution definition, and node.definition.range does not represent a numeric range. Following dd2da38, the min and max boudaries default to the min and max safe JS integers.
1 parent dfda2ab commit a4e89b0

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

lib/parse/replace.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,41 @@ function getPercentageResolutionType({ definition: { name }, parent }) {
7676
*/
7777
function replaceNumeric(node) {
7878

79+
const {
80+
context,
81+
definition: {
82+
max = Number.MAX_SAFE_INTEGER,
83+
min = Number.MIN_SAFE_INTEGER,
84+
name,
85+
},
86+
input,
87+
parent,
88+
} = node
7989
const topLevel = !isProducedBy(node, '<calc-value>')
80-
const { context, definition, input, parent } = node
8190
const token = input.next()
8291

8392
if (token.types[0] !== '<function-token>') {
84-
if (definition.name === '<number>' && topLevel) {
93+
if (name === '<number>' && topLevel) {
8594
return replaceWithColorComponentKeyword(node)
8695
}
8796
return null
8897
}
8998

9099
// <dimension> produced by a specific dimension type
91-
if (definition.name === '<dimension>' && parent?.definition.type === 'non-terminal') {
100+
if (name === '<dimension>' && parent?.definition.type === 'non-terminal') {
92101
return null
93102
}
94103

95-
for (const { definition, element, name } of substitutions.numeric) {
104+
for (const fn of substitutions.numeric) {
96105

97106
// Validate the grammar
98-
if (isFailure(parseGrammar([token], { name: '<function-token>', range: name, type: 'token' }, context))) {
107+
if (isFailure(parseGrammar([token], { name: '<function-token>', range: fn.name, type: 'token' }, context))) {
99108
continue
100109
}
101-
if (element && !getRule(node)?.definition.elemental) {
110+
if (fn.element && !getRule(node)?.definition.elemental) {
102111
return error(node)
103112
}
104-
const match = parseGrammar(input, definition, node, 'lazy')
113+
const match = parseGrammar(input, fn.definition, node, 'lazy')
105114
if (isFailure(match)) {
106115
return error(node)
107116
}
@@ -114,10 +123,9 @@ function replaceNumeric(node) {
114123
const type = getCalculationType(match, resolutionType)
115124

116125
if (matchNumericType(type, replacedType, resolutionType)) {
117-
const { name, range } = definition
118126
return {
119127
...match,
120-
range,
128+
range: { max, min },
121129
round: name === '<integer>',
122130
types: [...match.types, '<calc-function>'],
123131
value: simplifyCalculation(match, resolutionType, context.globalObject.devicePixelRatio),

test/value.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,16 +1627,16 @@ describe('<calc()>', () => {
16271627
value: list([one, { types: ['<calc-invert>'], value: two }], '*', ['<calc-product>']),
16281628
})
16291629
// Resolved calculation
1630-
assert.representation('<number>', 'calc(1)', {
1630+
assert.representation('<integer [-1,0]>', 'calc(1)', {
16311631
name: 'calc',
1632-
range: undefined,
1633-
round: false,
1632+
range: { max: 0, min: -1 },
1633+
round: true,
16341634
types: ['<function>', '<calc()>', '<calc-function>'],
16351635
value: number(1, ['<calc-value>']),
16361636
})
16371637
assert.representation('<number>', 'calc(1 + 2)', {
16381638
name: 'calc',
1639-
range: undefined,
1639+
range: { max: Number.MAX_SAFE_INTEGER, min: Number.MIN_SAFE_INTEGER },
16401640
round: false,
16411641
types: ['<function>', '<calc()>', '<calc-function>'],
16421642
value: {

0 commit comments

Comments
 (0)