Skip to content

Commit 3e143a0

Browse files
committed
Minor refactoring
The refactorized implementation was brittle because it depended on the relative color syntax always being the top-level value definition of color functions, which might not always be true. The new implementation may slightly increase time complexity but significantly decrease code complexity because it is the most idiomatic way to check that a source color has been specified.
1 parent 406f899 commit 3e143a0

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

lib/parse/replace.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
const { colorFunctionKeywords, colorSpaces, colorSpaceKeywords } = require('../values/colors.js')
33
const { getCalculationType, matchNumericType } = require('./types.js')
4-
const { isAmpersand, isOmitted } = require('../utils/value.js')
54
const createError = require('../error.js')
65
const { findSibling } = require('../utils/context.js')
6+
const { isAmpersand } = require('../utils/value.js')
77
const { simplifyCalculation } = require('./simplify.js')
88
const substitutions = require('../values/substitutions.js')
99

@@ -109,8 +109,7 @@ function resolveColorChannelDefinition(node) {
109109
const { context: { trees } } = node
110110
// Find the deepest tree whose context is a color function
111111
for (let index = trees.length; 0 < index; --index) {
112-
const tree = trees[index - 1]
113-
const { children, context: { replaced, function: fn } } = tree
112+
const { [index - 1]: { context: { replaced, function: fn } }} = trees
114113
if (fn) {
115114
const { definition: { name } } = fn
116115
// Ignore trees of numeric substitution functions and their arguments
@@ -120,15 +119,13 @@ function resolveColorChannelDefinition(node) {
120119
continue
121120
}
122121
// There must be a source color for color keywords to be valid
123-
const source = children[0]
124-
if (source?.definition.type !== 'optional' || isOmitted(source.value)) {
125-
return null
126-
}
127-
if (name === 'color') {
128-
const colorSpace = findSibling(node, node => colorSpaces.includes(node.definition.name))
129-
return colorSpaceKeywords[colorSpace.definition.name]
122+
if (findSibling(node, node => node.definition.name === '<color>')) {
123+
if (name === 'color') {
124+
const colorSpace = findSibling(node, node => colorSpaces.includes(node.definition.name))
125+
return colorSpaceKeywords[colorSpace.definition.name]
126+
}
127+
return colorFunctionKeywords[name]
130128
}
131-
return colorFunctionKeywords[name]
132129
}
133130
return null
134131
}

0 commit comments

Comments
 (0)