MCPcopy Create free account
hub / github.com/adobe/react-spectrum / getVariableMappings

Function getVariableMappings

lib/vars.js:56–116  ·  view source on GitHub ↗
(themes)

Source from the content-addressed store, hash-verified

54
55// Get unique variables, static variables, and a mapping of original variable names.
56function getVariableMappings(themes) {
57 let themeVars = {};
58 for (let theme of themes) {
59 let values = getVars(
60 `${__dirname}/../packages/@adobe/spectrum-css-temp/vars/spectrum-${theme}.css`
61 );
62 let mappings = getUniqueVars(values);
63 themeVars[theme] = {values, mappings};
64 }
65
66 let {values, mappings} = themeVars[themes[0]];
67 let mapping = {};
68 let staticVars = {};
69 let vars = {};
70 for (let v in mappings) {
71 // If the variable does not change values across themes, save it in the static variables list
72 let isStatic = themes.every(t => themeVars[t].values[v] === values[v]);
73 if (isStatic) {
74 staticVars[v] = values[v];
75 continue;
76 }
77
78 // Find a variable that exists in the mappings across all themes
79 let mapped = mappings[v].find(mapped => {
80 return themes.every(t => {
81 return themeVars[t].mappings[v].includes(mapped);
82 });
83 });
84
85 if (!mapped) {
86 throw new Error('Could not find mapping');
87 }
88
89 // If the variable maps to itself, add it to the mapping of unique variables
90 if (mapped === v) {
91 for (let t of themes) {
92 if (!vars[t]) {
93 vars[t] = {};
94 }
95
96 vars[t][v] = themeVars[t].values[v];
97 }
98 } else {
99 // Otherwise, map the variable to one of the unique variables.
100 mapping[v] = mapped;
101 }
102 }
103
104 for (let mapped in mapping) {
105 for (let t of themes) {
106 if (
107 themeVars[t].values[mapped] !== themeVars[t].values[mapping[mapped]] ||
108 !vars[t][mapping[mapped]]
109 ) {
110 throw new Error('Invalid mapping ' + mapped + ' ' + mapping[mapped]);
111 }
112 }
113 }

Callers 1

vars.jsFile · 0.85

Calls 2

getUniqueVarsFunction · 0.85
getVarsFunction · 0.70

Tested by

no test coverage detected