Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(391): don't mutate existing config in getConfig #392

Merged
merged 3 commits into from
Jul 12, 2021
Merged

fix(391): don't mutate existing config in getConfig #392

merged 3 commits into from
Jul 12, 2021

Conversation

snyamathi
Copy link
Contributor

Fixes: #391

This PR prevents the mutation of the config which is passed into getConfig.

The documentation shows the declaration of finalConfig as a separate variable from defaultConfig so I don't believe this mutation is an intentional part of the API, just an accidental oversight. However, this could potentially cause issues for someone who was (incorrectly / unknowingly) depending on this side effect.

// Generate Atomizer configuration from an array of Atomic classnames
var finalConfig = atomizer.getConfig(foundClasses, defaultConfig);

https://github.com/acss-io/atomizer#api

Atomizer.prototype.getConfig = function (classNames/*:string[]*/, config/*:AtomizerConfig*/)/*:AtomizerConfig*/ {
config = config || { classNames: [] };
Atomizer.prototype.getConfig = function (classNames/*:string[]*/, existingConfig/*:AtomizerConfig*/)/*:AtomizerConfig*/ {
const config = existingConfig && _.cloneDeep(existingConfig) || { classNames: [] };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was in two minds about this approach, I thought we could avoid a deep clone but actually this is nice because no one consuming this can't mistakenly mutate a reference in the original config via the new one either.

Seems like the right way to go to me 👍

@src-code src-code merged commit b91fa8c into acss-io:master Jul 12, 2021
@roderickhsiao
Copy link
Contributor

Haven't dig into deep details but it seems like https://github.com/acss-io/webpack-atomizer-loader/blob/master/lib/atomicLoader.ts is kind of doing side effect? tried to upgrade the latest version and the css is not generated correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Atomizer getConfig mutates provided object, should be pure
6 participants