Skip to content

Commit

Permalink
fix: local references as references
Browse files Browse the repository at this point in the history
Fixes an issue where importing an alias could get you the alias raw value instead of the expected source value.
  • Loading branch information
tivac committed Jul 18, 2021
1 parent e961578 commit aeba154
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
18 changes: 12 additions & 6 deletions packages/processor/plugins/before/values-local.js
Expand Up @@ -28,12 +28,18 @@ module.exports = () => ({
if(parsed.type !== "assignment") {
return;
}

values[parsed.name] = {
value : parsed.value,
source : rule.source,
};


// References to existing values are handled as object references,
// so they're always kept up-to-date
if(values[parsed.value]) {
values[parsed.name] = values[parsed.value];
} else {
values[parsed.name] = {
value : parsed.value,
source : rule.source,
};
}

rule.remove();
},
},
Expand Down
24 changes: 14 additions & 10 deletions packages/processor/test/__snapshots__/values.test.js.snap
Expand Up @@ -19,8 +19,7 @@ exports[`/processor.js values should support exporting imported variables 1`] =
.mc_green {
color: green;
}
"
}"
`;

exports[`/processor.js values should support importing variables from a file 1`] = `
Expand All @@ -32,8 +31,7 @@ exports[`/processor.js values should support importing variables from a file 1`]
.mc_green {
color: green;
}
"
}"
`;

exports[`/processor.js values should support local values in value composition 1`] = `
Expand All @@ -46,6 +44,15 @@ exports[`/processor.js values should support local values in value composition 1
"
`;

exports[`/processor.js values should support several layers of value references 1`] = `
"/* packages/processor/test/specimens/values.css */
/* packages/processor/test/specimens/value-references.css */
.mc_aliased {
color: blue;
background: blue;
}"
`;

exports[`/processor.js values should support simple values 1`] = `
"/* values.css */
.mc_a {
Expand All @@ -61,8 +68,7 @@ exports[`/processor.js values should support value aliasing 1`] = `
.mc_aliased {
color: red;
background: blue;
}
"
}"
`;

exports[`/processor.js values should support value composition 1`] = `
Expand All @@ -71,8 +77,7 @@ exports[`/processor.js values should support value composition 1`] = `
.mc_red {
color: red;
background: blue;
}
"
}"
`;

exports[`/processor.js values should support value namespaces 1`] = `
Expand All @@ -88,8 +93,7 @@ exports[`/processor.js values should support value namespaces 1`] = `
.mc_other {
color: #000;
}
"
}"
`;

exports[`/processor.js values should support value replacement in :external(...) 1`] = `
Expand Down
6 changes: 6 additions & 0 deletions packages/processor/test/specimens/value-references.css
@@ -0,0 +1,6 @@
@value * as vals from "./values.css";

.aliased {
color: vals.alias;
background: vals.alias2;
}
3 changes: 3 additions & 0 deletions packages/processor/test/specimens/values.css
Expand Up @@ -2,3 +2,6 @@
@value b: blue;
@value base: #FFF;
@value base-other: #000;

@value alias: b;
@value alias2: alias;
8 changes: 8 additions & 0 deletions packages/processor/test/values.test.js
Expand Up @@ -172,5 +172,13 @@ describe("/processor.js", () => {

expect(css).toMatchSnapshot();
});

it("should support several layers of value references", async () => {
await processor.file(require.resolve("./specimens/value-references.css"));

const { css } = await processor.output();

expect(css).toMatchSnapshot();
});
});
});

0 comments on commit aeba154

Please sign in to comment.