10
10
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
11
* and limitations under the License.
12
12
*/
13
-
14
13
import getPathFromName from './getPathFromName.js' ;
15
14
import createReferenceRegex from './createReferenceRegex.js' ;
16
15
import getValueByPath from './getValueByPath.js' ;
17
16
import GroupMessages from '../groupMessages.js' ;
18
17
import defaults from './defaults.js' ;
19
18
19
+ const FILTER_WARNINGS = GroupMessages . GROUP . FilteredOutputReferences ;
20
+
21
+ /**
22
+ * @typedef {import('../../../types/Config.d.ts').RegexOptions } RegexOptions
23
+ * @typedef {import('../../StyleDictionary.js').default } Dictionary
24
+ * @typedef {import('../../../types/DesignToken.d.ts').TransformedTokens } Tokens
25
+ * @typedef {import('../../../types/DesignToken.d.ts').TransformedToken } Token
26
+ *
27
+ * Public API wrapper around the function below this one
28
+ *
29
+ * @memberof Dictionary
30
+ * @param {string|Object<string, string|any> } value the value that contains a reference
31
+ * @param {Tokens } tokens the dictionary to search in
32
+ * @param {RegexOptions & { unfilteredTokens?: Tokens } } [opts]
33
+ * @param {Token[] } [references] array of token's references because a token's value can contain multiple references due to string interpolation
34
+ * @returns {Token[] }
35
+ */
36
+ export function getReferences ( value , tokens , opts = { } , references = [ ] ) {
37
+ return _getReferences ( value , tokens , opts , references , true ) ;
38
+ }
39
+
20
40
/**
21
41
* This is a helper function that is added to the dictionary object that
22
42
* is passed to formats and actions. It will resolve a reference giving
@@ -26,19 +46,21 @@ import defaults from './defaults.js';
26
46
* ```css
27
47
* --color-background-base: var(--color-core-white);
28
48
* ```
29
- * @typedef {import('../../../types/Config.d.ts').RegexOptions } RegexOptions
30
- * @typedef {import('../../StyleDictionary.js').default } Dictionary
31
- * @typedef {import('../../../types/DesignToken.d.ts').TransformedTokens } Tokens
32
- * @typedef {import('../../../types/DesignToken.d.ts').TransformedToken } Token
33
49
*
34
- * @memberof Dictionary
35
50
* @param {string|Object<string, string|any> } value the value that contains a reference
36
51
* @param {Tokens } tokens the dictionary to search in
37
52
* @param {RegexOptions & { unfilteredTokens?: Tokens } } [opts]
38
53
* @param {Token[] } [references] array of token's references because a token's value can contain multiple references due to string interpolation
54
+ * @param {boolean } [throwImmediately]
39
55
* @returns {Token[] }
40
56
*/
41
- export default function getReferences ( value , tokens , opts = { } , references = [ ] ) {
57
+ export default function _getReferences (
58
+ value ,
59
+ tokens ,
60
+ opts = { } ,
61
+ references = [ ] ,
62
+ throwImmediately = false ,
63
+ ) {
42
64
const regex = createReferenceRegex ( opts ) ;
43
65
44
66
/**
@@ -55,14 +77,21 @@ export default function getReferences(value, tokens, opts = {}, references = [])
55
77
56
78
let ref = getValueByPath ( pathName , tokens ) ;
57
79
58
- if ( ! ref && opts . unfilteredTokens ) {
80
+ if ( ref === undefined && opts . unfilteredTokens ) {
81
+ if ( ! throwImmediately ) {
82
+ // warn the user about this
83
+ GroupMessages . add ( FILTER_WARNINGS , variable ) ;
84
+ }
59
85
// fall back on unfilteredTokens as it is unfiltered
60
86
ref = getValueByPath ( pathName , opts . unfilteredTokens ) ;
61
- // and warn the user about this
62
- GroupMessages . add ( GroupMessages . GROUP . FilteredOutputReferences , variable ) ;
63
87
}
88
+
64
89
if ( ref !== undefined ) {
65
90
references . push ( ref ) ;
91
+ } else if ( throwImmediately ) {
92
+ throw new Error (
93
+ `Reference doesn't exist: tries to reference ${ variable } , which is not defined.` ,
94
+ ) ;
66
95
}
67
96
return '' ;
68
97
}
@@ -83,7 +112,7 @@ export default function getReferences(value, tokens, opts = {}, references = [])
83
112
}
84
113
// if it is an object, we go further down the rabbit hole
85
114
if ( value . hasOwnProperty ( key ) && typeof value [ key ] === 'object' ) {
86
- getReferences ( value [ key ] , tokens , opts , references ) ;
115
+ _getReferences ( value [ key ] , tokens , opts , references ) ;
87
116
}
88
117
}
89
118
}
0 commit comments