Skip to content

Commit

Permalink
Fix at-each-key-value-single-line end positions (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Oct 24, 2022
1 parent 10cd52b commit 014ea5b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 61 deletions.
129 changes: 75 additions & 54 deletions src/rules/at-each-key-value-single-line/__tests__/index.js
Expand Up @@ -7,162 +7,162 @@ testRule({

accept: [
{
code: `
code: dedent`
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in $font-weights {}
`,
description: "Proper map loop that gets both keys + values"
description: "Proper map loop that gets both keys + values",
},
{
code: `
code: dedent`
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when using global function"
"Loop that just gets keys + has no need for values when using global function",
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map.keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with default namespace"
"Loop that just gets keys + has no need for values when loading sass module with default namespace",
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with default namespace but using global function"
"Loop that just gets keys + has no need for values when loading sass module with default namespace but using global function",
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with no namespace"
"Loop that just gets keys + has no need for values when loading sass module with no namespace",
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with no namespace but using global function"
"Loop that just gets keys + has no need for values when loading sass module with no namespace but using global function",
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in ns.keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with custom namespace"
"Loop that just gets keys + has no need for values when loading sass module with custom namespace",
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {}
`,
description:
"Loop that just gets keys + has no need for values when loading sass module with custom namespace but using global function"
"Loop that just gets keys + has no need for values when loading sass module with custom namespace but using global function",
},
{
code: `
code: dedent`
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in map-keys($font-weights) {
$value: map-get($other-weights, $key);
$value: map-get($other-weights, $key);
}
`,
description:
"map-get pattern used with different hash than loop when using global function"
"map-get pattern used with different hash than loop when using global function",
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in map.keys($font-weights) {
$value: map.get($other-weights, $key);
$value: map.get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with default namespace"
"map.get pattern used with different hash than loop when loading sass module with default namespace",
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in map-keys($font-weights) {
$value: map-get($other-weights, $key);
$value: map-get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with default namespace but using global function"
"map.get pattern used with different hash than loop when loading sass module with default namespace but using global function",
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in keys($font-weights) {
$value: get($other-weights, $key);
$value: get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with no namespace"
"map.get pattern used with different hash than loop when loading sass module with no namespace",
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in map-keys($font-weights) {
$value: map-get($other-weights, $key);
$value: map-get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with no namespace but using global function"
"map.get pattern used with different hash than loop when loading sass module with no namespace but using global function",
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in ns.keys($font-weights) {
$value: ns.get($other-weights, $key);
$value: ns.get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with custom namespace"
"map.get pattern used with different hash than loop when loading sass module with custom namespace",
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$other-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key, $value in map-keys($font-weights) {
$value: map-get($other-weights, $key);
$value: map-get($other-weights, $key);
}
`,
description:
"map.get pattern used with different hash than loop when loading sass module with custom namespace but using global function"
}
"map.get pattern used with different hash than loop when loading sass module with custom namespace but using global function",
},
],

reject: [
{
code: `
code: dedent`
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {
$value: map-get($font-weights, $key);
Expand All @@ -171,10 +171,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when using global function",
message: messages.expected,
line: 3
line: 2,
column: 7,
endLine: 2,
endColumn: 38,
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map.keys($font-weights) {
Expand All @@ -184,10 +187,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with default namespace",
message: messages.expected,
line: 4
line: 3,
column: 7,
endLine: 3,
endColumn: 38,
},
{
code: `
code: dedent`
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {
Expand All @@ -197,10 +203,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with default namespace but using global function",
message: messages.expected,
line: 4
line: 3,
column: 7,
endLine: 3,
endColumn: 38,
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in keys($font-weights) {
Expand All @@ -210,10 +219,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with no namespace",
message: messages.expected,
line: 4
line: 3,
column: 7,
endLine: 3,
endColumn: 34,
},
{
code: `
code: dedent`
@use "sass:map" as *;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {
Expand All @@ -223,10 +235,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with no namespace but using global function",
message: messages.expected,
line: 4
line: 3,
column: 7,
endLine: 3,
endColumn: 38,
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in ns.keys($font-weights) {
Expand All @@ -236,10 +251,13 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with custom namespace",
message: messages.expected,
line: 4
line: 3,
column: 7,
endLine: 3,
endColumn: 37,
},
{
code: `
code: dedent`
@use "sass:map" as ns;
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $key in map-keys($font-weights) {
Expand All @@ -249,7 +267,10 @@ testRule({
description:
"Loop that gets keys + then grabs values inside the map when loading sass module with custom namespace but using global function",
message: messages.expected,
line: 4
}
]
line: 3,
column: 7,
endLine: 3,
endColumn: 38,
},
],
});
15 changes: 8 additions & 7 deletions src/rules/at-each-key-value-single-line/index.js
Expand Up @@ -5,17 +5,17 @@ export const ruleName = namespace("at-each-key-value-single-line");

export const messages = utils.ruleMessages(ruleName, {
expected:
"Use @each $key, $value in $map syntax instead of $value: map-get($map, $key)"
"Use @each $key, $value in $map syntax instead of $value: map-get($map, $key)",
});

export const meta = {
url: ruleUrl(ruleName)
url: ruleUrl(ruleName),
};

export default function rule(primary) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, {
actual: primary
actual: primary,
});

if (!validOptions) {
Expand All @@ -24,7 +24,7 @@ export default function rule(primary) {

const mapNamespace = moduleNamespace(root, "sass:map");

root.walkAtRules("each", rule => {
root.walkAtRules("each", (rule) => {
const parts = separateEachParams(rule.params);

// If loop is fetching both key + value, return
Expand All @@ -38,7 +38,7 @@ export default function rule(primary) {
}

// Loop over decls inside of each statement and loop for variable assignments.
rule.walkDecls(innerDecl => {
rule.walkDecls((innerDecl) => {
// Check that this decl is a map-get call
if (innerDecl.prop[0] !== "$") {
return;
Expand All @@ -65,7 +65,8 @@ export default function rule(primary) {
message: messages.expected,
node: rule,
result,
ruleName
ruleName,
word: rule.params,
});
});
});
Expand All @@ -81,7 +82,7 @@ rule.meta = meta;
function separateEachParams(paramString) {
const parts = paramString.split("in");

return [parts[0].split(",").map(s => s.trim()), parts[1].trim()];
return [parts[0].split(",").map((s) => s.trim()), parts[1].trim()];
}

function didCallMapKeys(mapDecl, mapNamespace) {
Expand Down

0 comments on commit 014ea5b

Please sign in to comment.