Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/prefer-logical-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Examples of **incorrect** code for this rule:
/* incorrect use of physical properties */
a {
margin-left: 10px;
transition: margin-left 0.3s;
}
```

Expand All @@ -28,6 +29,7 @@ Examples of **correct** code for this rule:

a {
margin-inline-start: 10px;
transition: margin-inline-start 0.3s;
}
```

Expand Down
50 changes: 50 additions & 0 deletions src/rules/prefer-logical-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ const unitReplacements = new Map([
["vw", "vi"],
]);

const propertiesWithPropertyNames = new Set([
"transition",
"transition-property",
"will-change",
]);

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -278,6 +284,50 @@ export default /** @satisfies {PreferLogicalPropertiesRuleDefinition} */ ({
});
}
}

if (
propertiesWithPropertyNames.has(property) &&
node.value.type === "Value"
) {
for (const child of node.value.children) {
if (child.type !== "Identifier") {
continue;
}

const name = child.name.toLowerCase();
const nameReplacement =
propertiesReplacements.get(name);

if (
nameReplacement &&
!allowProperties.includes(name)
) {
context.report({
loc: child.loc,
messageId: "notLogicalProperty",
data: {
property: child.name,
replacement: nameReplacement,
},
suggest: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: child.name,
replacement: nameReplacement,
},
fix(fixer) {
return fixer.replaceText(
child,
nameReplacement,
);
},
},
],
});
}
}
}
},
Dimension(node) {
const unit = node.unit.toLowerCase();
Expand Down
133 changes: 133 additions & 0 deletions tests/rules/prefer-logical-properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ ruleTester.run("prefer-logical-properties", rule, {
},
],
},
{
code: "a { transition: margin-left 1s; }",
options: [
{
allowProperties: ["margin-left"],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -245,5 +253,130 @@ ruleTester.run("prefer-logical-properties", rule, {
},
],
},
{
code: "a { transition: margin-left 1s; }",
errors: [
{
messageId: "notLogicalProperty",
line: 1,
column: 17,
endLine: 1,
endColumn: 28,
data: {
property: "margin-left",
replacement: "margin-inline-start",
},
suggestions: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: "margin-left",
replacement: "margin-inline-start",
},
output: "a { transition: margin-inline-start 1s; }",
},
],
},
],
},
{
code: "a { TRANSITION: MARGIN-LEFT 1s; }",
errors: [
{
messageId: "notLogicalProperty",
line: 1,
column: 17,
endLine: 1,
endColumn: 28,
data: {
property: "MARGIN-LEFT",
replacement: "margin-inline-start",
},
suggestions: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: "MARGIN-LEFT",
replacement: "margin-inline-start",
},
output: "a { TRANSITION: margin-inline-start 1s; }",
},
],
},
],
},
{
code: "a { transition-property: width, top; }",
errors: [
{
messageId: "notLogicalProperty",
line: 1,
column: 26,
endLine: 1,
endColumn: 31,
data: {
property: "width",
replacement: "inline-size",
},
suggestions: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: "width",
replacement: "inline-size",
},
output: "a { transition-property: inline-size, top; }",
},
],
},
{
messageId: "notLogicalProperty",
line: 1,
column: 33,
endLine: 1,
endColumn: 36,
data: {
property: "top",
replacement: "inset-block-start",
},
suggestions: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: "top",
replacement: "inset-block-start",
},
output: "a { transition-property: width, inset-block-start; }",
},
],
},
],
},
{
code: "a { will-change: left; }",
errors: [
{
messageId: "notLogicalProperty",
line: 1,
column: 18,
endLine: 1,
endColumn: 22,
data: {
property: "left",
replacement: "inset-inline-start",
},
suggestions: [
{
messageId: "replaceWithLogicalProperty",
data: {
property: "left",
replacement: "inset-inline-start",
},
output: "a { will-change: inset-inline-start; }",
},
],
},
],
},
],
});
Loading