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: improve support for each #103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions .tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,13 @@ module.exports = {
syntax: require('postcss-scss')
}
},
'each': {
message: 'supports each',
source: 'each.scss',
expect: 'each.expect.scss',
result: 'each.result.scss',
processOptions: {
syntax: require('postcss-scss')
}
},
};
2 changes: 1 addition & 1 deletion src/lib/get-value-as-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function getValueAsObject(value) {
const matchWrappingParens = /^\(([\W\w]*)\)$/g;

// match a property name (any-possible_name)
const matchDeclaration = /^([\w-]+)\s*:\s*([\W\w]+)\s*$/;
const matchDeclaration = /^["']?([\w-]+)["']?\s*:\s*([\W\w]+)\s*$/;
Copy link
Author

Choose a reason for hiding this comment

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

quoted keys where not supported


// match a trailing comma
const matchTrailingComma = /\s*,\s*$/;
7 changes: 6 additions & 1 deletion src/lib/transform-each-atrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function transformEachAtrule(rule, opts) {
// return the @each statement options (@each NAME in LIST, @each NAME ITERATOR in LIST)
const getEachOpts = (node, opts) => {
const params = node.params.split(matchInOperator);
const args = (params[0] || '').trim().split(' ');
const args = (params[0] || '').trim().split(/[\s,]+/);
Copy link
Author

Choose a reason for hiding this comment

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

@each $value, $key in $arrayTest {

support for sass syntax -> comma

const varname = args[0].trim().slice(1);
const incname = args.length > 1 && args[1].trim().slice(1);
const rawlist = getValueAsObject(
Expand All @@ -67,6 +67,11 @@ const getEachOpts = (node, opts) => {
);
const list = 'string' === typeof rawlist ? [rawlist] : rawlist;

const reverseParams = incname && !Array.isArray(list)
if (reverseParams) {
return { incname: varname, varname: incname, list };
}
Comment on lines +70 to +73
Copy link
Author

@armano2 armano2 Sep 20, 2023

Choose a reason for hiding this comment

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

in case of object if 2 arguments are passed, its key, value


return { varname, incname, list };
};

Expand Down
47 changes: 47 additions & 0 deletions test/each.expect.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* arrayTest: $value */
.one-arrayTest {
content: one;
}
.two-arrayTest {
content: two;
}

/* arrayTest: $value, $key */
.0-arrayTest {
content: one;
}
.1-arrayTest {
content: two;
}

/* objectTest: $value */
."1"-arrayTest {
content: "1";
}
."2"-arrayTest {
Copy link
Author

@armano2 armano2 Sep 20, 2023

Choose a reason for hiding this comment

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

this idffers from sass, as sass removes "', not sure if you want me to remove it to

removal of quotes is only present if string is \w_ from what i observed

content: "2";
}

/* objectTest: $key, $value */
.one-arrayTest {
content: "1";
}
.two-arrayTest {
content: "2";
}

/* objectStringTest: $key, $value */
.one-arrayTest {
content: "1";
}
.two-arrayTest {
content: "2";
}

/* objectStringTestMultiLine: $key, $value */
.one-arrayTest {
content: "1";
}
.two-arrayTest {
content: "2";
}
51 changes: 51 additions & 0 deletions test/each.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
$arrayTest: (one, two);

$objectTest: (one: "1", two: "2");
$objectStringTest: ("one": "1", "two": "2");

$objectStringTestMultiLine: (
"one": "1",
"two": "2"
);

/* arrayTest: $value */
@each $value in $arrayTest {
.#{$value}-arrayTest {
content: #{$value};
}
}

/* arrayTest: $value, $key */
@each $value, $key in $arrayTest {
.#{$key}-arrayTest {
content: #{$value};
}
}

/* objectTest: $value */
@each $value in $objectTest {
.#{$value}-arrayTest {
content: #{$value};
}
}

/* objectTest: $key, $value */
@each $key, $value in $objectTest {
.#{$key}-arrayTest {
content: #{$value};
}
}

/* objectStringTest: $key, $value */
@each $key, $value in $objectStringTest {
.#{$key}-arrayTest {
content: #{$value};
}
}

/* objectStringTestMultiLine: $key, $value */
@each $key, $value in $objectStringTestMultiLine {
.#{$key}-arrayTest {
content: #{$value};
}
}