Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_pLGAn-QZITPX71heBZ0EO.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"changes": {
"Cargo.toml": "Patch"
},
"note": "Export collision-free Figma colour token names, verify source-map variable bindings against each node, keep fixed icons from shrinking, and report code tokens that differ from the Figma binding",
"date": "2026-11-28T15:16:06.146464Z"
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ impl CodegenOptions {
) -> Self {
self.text_style_tokens = named_tokens(styles, "styles");
self.variable_tokens = named_tokens(variables, "variables");
// The theme's own names, collision-free across the whole catalog, so
// a `$token` in the code is the key `devup.json` defines for exactly
// that variable.
if let Some(snapshot) =
variables.and_then(|result| crate::theme::variable_snapshot_from_result(result).ok())
{
self.variable_tokens
.extend(crate::theme::variable_token_names(&snapshot));
}
self.length_tokens = length_tokens(variables);
let mut snapshot = variables
.and_then(|result| crate::theme::variable_snapshot_from_result(result).ok())
Expand Down Expand Up @@ -139,15 +148,13 @@ fn length_tokens(variables: Option<&UpstreamResult>) -> BTreeMap<String, LengthT
else {
return BTreeMap::new();
};
let names = crate::theme::variable_token_names(&snapshot);
snapshot
.variables
.iter()
.chain(&snapshot.used_remote_variables)
.filter_map(|variable| {
let token = variable_token(
&variable.name,
variable.code_syntax.get("WEB").map(String::as_str),
);
let token = names.get(&variable.id)?.clone();
let value = lengths.get(&token)?.clone();
Some((variable.id.clone(), LengthToken { token, value }))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,14 @@ pub(super) fn push_layout_props(
"paddingBottom",
)
};
if view.string(sizing) == Some("FIXED") && line_overflows(snapshot, parent, size, near, far)
{
let fixed = view.string(sizing) == Some("FIXED");
// An icon or picture drawn at a fixed size is never meant to give way
// to its neighbours, at any width: Figma does not shrink it, and a
// label that runs longer at runtime than in the drawing would squeeze
// a 24px icon in the same row to a sliver. (Figma has no shrink
// factor of its own: FIXED is how it says a child keeps its size.)
let never_shrinks = fixed && crate::codegen::asset_kind(snapshot, node).is_some();
if never_shrinks || (fixed && line_overflows(snapshot, parent, size, near, far)) {
string_prop(props, "flexShrink", "0");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,15 @@ const PROP_SOURCES: &[(&str, &str)] = &[
fn add_text_entries(
tsx: &str,
range: &GeneratedRange,
node_id: &str,
node: &devup_mcp_figma::RawNode,
variable_tokens: &BTreeMap<String, String>,
style_tokens: &BTreeMap<String, String>,
entries: &mut Vec<ProvenanceEntry>,
unverified: &mut Vec<UnverifiedVariableEntry>,
) {
let node_id = node.id.as_str();
let source = &tsx[range.start..range.end];
let bound = bound_variable_ids(node, None);
let view = node.typed_view();
let segments = view
.value("styledTextSegments")
Expand Down Expand Up @@ -389,6 +391,19 @@ fn add_text_entries(
let prop_end = source[start..]
.find('"')
.map_or(start + needle.len(), |offset| start + offset + 1);
if !bound.contains(variable_id) {
unverified.push(UnverifiedVariableEntry {
node_id: node_id.to_owned(),
property: "styledTextSegments".to_owned(),
token: token.clone(),
candidate_variable_ids: vec![variable_id.clone()],
generated_range: Some(GeneratedRange {
start: range.start + prop_start,
end: range.start + prop_end,
}),
});
continue;
}
entries.push(generated_entry(
range.start + prop_start,
range.start + prop_end,
Expand Down Expand Up @@ -650,3 +665,173 @@ mod line_height_calculation_tests {
);
}
}

/// A `$token` in a generated attribute whose variable is not bound where the
/// attribute says it came from. Never recorded as a variable mapping; counted
/// so the summary says how many were refused.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnverifiedVariableEntry {
pub node_id: String,
pub property: String,
pub token: String,
pub candidate_variable_ids: Vec<String>,
#[serde(skip)]
pub generated_range: Option<GeneratedRange>,
}

/// Every variable id bound on `node`, in document order of its fields: the
/// `boundVariables` map and any `VARIABLE_ALIAS` inside a paint, effect or
/// segment. With `property`, only the bindings of that field —
/// `boundVariables[property]` and `paint.boundVariables.color` of the paints
/// in it — and, for a paint list, the paint drawn on top first.
fn bound_variable_ids(node: &devup_mcp_figma::RawNode, property: Option<&str>) -> Vec<String> {
fn scan(value: &serde_json::Value, output: &mut Vec<String>) {
match value {
serde_json::Value::Object(object) => {
if object.get("type").and_then(serde_json::Value::as_str) == Some("VARIABLE_ALIAS")
&& let Some(id) = object
.get("id")
.and_then(serde_json::Value::as_str)
.filter(|id| !id.is_empty() && *id != "figma.mixed" && *id != "MIXED")
&& !output.iter().any(|known| known == id)
{
output.push(id.to_owned());
}
object.values().for_each(|value| scan(value, output));
}
serde_json::Value::Array(values) => values.iter().for_each(|value| scan(value, output)),
_ => {}
}
}
let view = node.typed_view();
let mut output = Vec::new();
match property {
Some(property) => {
if let Some(paints) = view.value(property).and_then(serde_json::Value::as_array) {
// Figma paints the last paint of a list on top: that is the
// one a single CSS colour stands for.
for paint in paints.iter().rev() {
if paint.get("visible").and_then(serde_json::Value::as_bool) != Some(false) {
scan(paint, &mut output);
}
}
} else if let Some(value) = view.value(property) {
scan(value, &mut output);
}
if let Some(bound) = view.value("boundVariables").and_then(|b| b.get(property)) {
scan(bound, &mut output);
}
// Mixed text keeps its paints per character range: in the
// segments, and as `textRangeFills` among the bindings.
if node.node_type == "TEXT" && property == "fills" {
if let Some(bound) = view
.value("boundVariables")
.and_then(|b| b.get("textRangeFills"))
{
scan(bound, &mut output);
}
for segment in view
.value("styledTextSegments")
.and_then(serde_json::Value::as_array)
.into_iter()
.flatten()
{
if let Some(fills) = segment.get("fills") {
scan(fills, &mut output);
}
}
}
}
None => {
for (_, value) in node.fields.iter().chain(&node.extra) {
scan(value, &mut output);
}
}
}
output
}

/// The fields whose bindings a `$token` written for `property` can come
/// from. A paint list is its own evidence; any other field (a radius, a
/// padding, a size) may be bound under a more specific key, so the whole node
/// is.
fn binding_is_paint(property: &str) -> bool {
matches!(property, "fills" | "strokes" | "effects")
}

/// The variable a generated attribute value stands for on `node`, proven by
/// the node's own bindings: of the variables whose `$token` appears in
/// `value`, the one bound to `property` (the paint drawn on top first), else
/// one bound anywhere on the node for a non-paint property. A token no
/// binding of the node accounts for is returned as unverified instead.
///
/// Returns the node whose binding proves it, too: `node`
/// itself, or — with `snapshot` — a descendant that is not an element of its
/// own (`emitted` has no range for it) and so is drawn by this one, as an
/// icon's vector is drawn by the box that masks it. A descendant that is its
/// own element is never evidence for its parent: an inlined button's label
/// binds the label, not the button.
fn verified_resource_source(
value: &str,
tokens: &BTreeMap<String, String>,
snapshot: Option<&Snapshot>,
node: &devup_mcp_figma::RawNode,
property: &str,
emitted: &BTreeMap<String, GeneratedRange>,
) -> Result<Option<(String, String)>, UnverifiedVariableEntry> {
let candidates = tokens
.iter()
.filter(|(id, token)| {
find_resource_id(value, &BTreeMap::from([((*id).clone(), (*token).clone())]), '$')
.is_some()
})
.map(|(id, _)| id.clone())
.collect::<Vec<_>>();
if candidates.is_empty() {
return Ok(None);
}
let own = bound_variable_ids(node, Some(property));
if let Some(id) = own.iter().find(|id| candidates.contains(id)) {
return Ok(Some((node.id.clone(), id.clone())));
}
if !binding_is_paint(property)
&& let Some(id) = bound_variable_ids(node, None)
.into_iter()
.find(|id| candidates.contains(id))
{
return Ok(Some((node.id.clone(), id)));
}
if let Some(snapshot) = snapshot {
let mut pending = node.typed_view().child_ids().map(str::to_owned).collect::<Vec<_>>();
while let Some(child_id) = pending.pop() {
if emitted.contains_key(&child_id) {
continue;
}
let Some(child) = snapshot.nodes.get(&child_id) else {
continue;
};
if child.typed_view().bool("visible") == Some(false) {
continue;
}
if let Some(id) = bound_variable_ids(child, Some(property))
.into_iter()
.find(|id| candidates.contains(id))
{
return Ok(Some((child_id, id)));
}
pending.extend(child.typed_view().child_ids().map(str::to_owned));
}
}
Err(UnverifiedVariableEntry {
node_id: node.id.clone(),
property: property.to_owned(),
token: candidates
.first()
.and_then(|id| tokens.get(id))
.cloned()
.unwrap_or_default(),
candidate_variable_ids: candidates,
generated_range: None,
})
}
55 changes: 53 additions & 2 deletions crates/devup-mcp-devup-ui/src/provenance/baked_into_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ pub(crate) fn finalize_tsx(
.push(asset);
}
let mut entries = Vec::new();
let mut unverified_variables = Vec::<UnverifiedVariableEntry>::new();
for (node_id, range) in node_ranges {
let Some(node) = snapshot.nodes.get(&node_id) else {
continue;
Expand Down Expand Up @@ -703,7 +704,32 @@ pub(crate) fn finalize_tsx(
};
let end = value_start + value_end + 1;
let value = &opening[value_start..value_start + value_end];
let variable_id = find_resource_id(value, variable_tokens, '$');
let mut evidence_node = None;
let variable_id = match verified_resource_source(
value,
variable_tokens,
Some(snapshot),
node,
property,
&emitted_ranges,
) {
// A descendant drawn by this element proves the variable; the
// attribute stays this element's, and the variable is entered
// under the descendant that binds it.
Ok(Some((source_node, variable_id))) if source_node != node_id => {
evidence_node = Some((source_node, variable_id));
None
}
Ok(found) => found.map(|(_, variable_id)| variable_id),
Err(mut unverified) => {
unverified.generated_range = Some(GeneratedRange {
start: range.start + open_relative + start,
end: range.start + open_relative + end,
});
unverified_variables.push(unverified);
None
}
};
let style_id = (*prop == "typography")
.then(|| {
style_tokens
Expand Down Expand Up @@ -793,6 +819,17 @@ pub(crate) fn finalize_tsx(
style_id,
resolution,
));
if let Some((source_node, variable_id)) = evidence_node {
entries.push(generated_entry(
range.start + open_relative + start,
range.start + open_relative + end,
&source_node,
property,
Some(variable_id),
None,
"variable-token",
));
}
}

if crate::codegen::folded_mask_dimensions(snapshot, node).is_some() {
Expand Down Expand Up @@ -865,11 +902,11 @@ pub(crate) fn finalize_tsx(
add_text_entries(
&tsx,
&range,
&node_id,
node,
variable_tokens,
style_tokens,
&mut entries,
&mut unverified_variables,
);
}
}
Expand All @@ -881,6 +918,19 @@ pub(crate) fn finalize_tsx(
&assets_by_node,
&mut entries,
);
// A token the attribute's own node does not bind may still be the
// flattened paint of a descendant it represents; that pass recorded it
// under the descendant's id. Only a token nothing accounts for is left.
unverified_variables.retain(|unverified| {
!entries.iter().any(|entry| {
entry.resolution == "variable-token"
&& entry.generated_range == unverified.generated_range
&& entry
.variable_id
.as_ref()
.is_some_and(|id| unverified.candidate_variable_ids.contains(id))
})
});
entries.sort_by(|left, right| {
let left_range = left.generated_range.as_ref();
let right_range = right.generated_range.as_ref();
Expand All @@ -898,6 +948,7 @@ pub(crate) fn finalize_tsx(
asset_css_mappings: Vec::new(),
variable_tokens: variable_tokens.clone(),
style_tokens: style_tokens.clone(),
unverified_variables,
},
)
}
Loading
Loading