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

no-duplicate-dollar-variables: allow dup vars in unrelated scopes #453

Merged
merged 2 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/rules/no-duplicate-dollar-variables/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ testRule(rule, {
}
`,
description: "A single variable and a normal CSS property."
},
{
code: `
.a {
$ab: 1;
}
.b {
$ab: 2;
}
`,
description: "Two variables in unrelated scopes."
},
{
code: `
@mixin a {
$ab: 1;
}
@mixin c {
$ab: 2;
}
`,
description: "Two variables in unrelated at-rule scopes."
},
{
code: `
@if 1 == 2 {
$ab: 1;
}
@else {
$ab: 2;
}
`,
description: "Two variables in unrelated at-rule scope cases."
}
],

Expand Down
51 changes: 49 additions & 2 deletions src/rules/no-duplicate-dollar-variables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ export default function(value, secondaryOptions) {

const vars = {};

/**
* Traverse the [vars] tree through the path defined by [ancestors], creating nodes as needed.
*
* Return the tree of the node defined by the last of [ancestors].
*/
function getScope(ancestors) {
let scope = vars;

for (const node of ancestors) {
if (!(node in scope)) {
scope[node] = {};
}

scope = scope[node];
}

return scope;
}

/**
* Returns whether [variable] is declared anywhere in the scopes along the path defined by
* [ancestors].
*/
function isDeclared(ancestors, variable) {
let scope = vars;

for (const node of ancestors) {
scope = scope[node];

if (scope[variable]) return true;
}

return false;
}

root.walkDecls(decl => {
const isVar = decl.prop[0] === "$";
const isInsideIgnoredAtRule =
Expand Down Expand Up @@ -60,7 +95,19 @@ export default function(value, secondaryOptions) {
return;
}

if (vars[decl.prop]) {
const ancestors = [];
let parent = decl.parent;

while (parent !== null && parent !== undefined) {
const parentKey = parent.toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to be inefficient - I think this serializes the entire subtree below [parent]. I'm not sure what to use as unique keys to traverse down the scopes though. the non-leaf nodes are all Containers I think? I couldn't find a good common way to uniquify them...


ancestors.unshift(parentKey);
parent = parent.parent;
}

const scope = getScope(ancestors);

if (isDeclared(ancestors, decl.prop)) {
utils.report({
message: messages.rejected(decl.prop),
node: decl,
Expand All @@ -69,7 +116,7 @@ export default function(value, secondaryOptions) {
});
}

vars[decl.prop] = true;
scope[decl.prop] = true;
});
};
}