From 96d4ef77225fd7faef911d83c90277f79261d374 Mon Sep 17 00:00:00 2001 From: kawaguchi Date: Thu, 3 Mar 2022 07:55:18 +0900 Subject: [PATCH 1/8] Add `isStandardSyntaxProperty` --- lib/rules/custom-property-pattern/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/rules/custom-property-pattern/index.js b/lib/rules/custom-property-pattern/index.js index 3e90727ba9..6fc655caee 100644 --- a/lib/rules/custom-property-pattern/index.js +++ b/lib/rules/custom-property-pattern/index.js @@ -8,6 +8,7 @@ const validateOptions = require('../../utils/validateOptions'); const declarationValueIndex = require('../../utils/declarationValueIndex'); const { isRegExp, isString } = require('../../utils/validateTypes'); const { isValueFunction } = require('../../utils/typeGuards'); +const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty'); const ruleName = 'custom-property-pattern'; @@ -47,6 +48,8 @@ const rule = (primary) => { const { value: firstNodeValue } = nodes[0]; + if (!isStandardSyntaxProperty(firstNodeValue)) return; + if (!isCustomProperty(firstNodeValue)) return; if (regexpPattern.test(firstNodeValue.slice(2))) return; @@ -54,6 +57,10 @@ const rule = (primary) => { complain(declarationValueIndex(decl) + sourceIndex, decl); }); + if (!isStandardSyntaxProperty(prop)) { + return; + } + if (!isCustomProperty(prop)) { return; } From 63fb03d323637236a7133f5986df4aaf6ee86208 Mon Sep 17 00:00:00 2001 From: kawaguchi Date: Thu, 3 Mar 2022 07:56:15 +0900 Subject: [PATCH 2/8] Refactoring --- lib/rules/custom-property-pattern/index.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/rules/custom-property-pattern/index.js b/lib/rules/custom-property-pattern/index.js index 6fc655caee..b9419ba015 100644 --- a/lib/rules/custom-property-pattern/index.js +++ b/lib/rules/custom-property-pattern/index.js @@ -57,17 +57,11 @@ const rule = (primary) => { complain(declarationValueIndex(decl) + sourceIndex, decl); }); - if (!isStandardSyntaxProperty(prop)) { - return; - } + if (!isStandardSyntaxProperty(prop)) return; - if (!isCustomProperty(prop)) { - return; - } + if (!isCustomProperty(prop)) return; - if (regexpPattern.test(prop.slice(2))) { - return; - } + if (regexpPattern.test(prop.slice(2))) return; complain(0, decl); }); From da17d46f4aba1d3c072011250e8ae88265758c41 Mon Sep 17 00:00:00 2001 From: kawaguchi Date: Thu, 3 Mar 2022 07:58:16 +0900 Subject: [PATCH 3/8] Add 3 test cases --- lib/rules/custom-property-pattern/__tests__/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/rules/custom-property-pattern/__tests__/index.js b/lib/rules/custom-property-pattern/__tests__/index.js index 6eaaed257c..cce701d815 100644 --- a/lib/rules/custom-property-pattern/__tests__/index.js +++ b/lib/rules/custom-property-pattern/__tests__/index.js @@ -138,5 +138,14 @@ testRule({ { code: 'a { color: var(tokens.$bar-color); }', }, + { + code: 'a { --#{$variable-prefix}table-bg: rgba(#a1f2b0, 0.3); }', + }, + { + code: 'a { color: var( #{$variable-color} ); }', + }, + { + code: 'a { color: var( --#{$variable-color} ); }', + }, ], }); From 7fe77924b2b1e1d0b2fd0c84a915f50f5509c128 Mon Sep 17 00:00:00 2001 From: kawaguchi <30929824+kawaguchi1102@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:30:26 +0900 Subject: [PATCH 4/8] Update lib/rules/custom-property-pattern/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/custom-property-pattern/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/custom-property-pattern/__tests__/index.js b/lib/rules/custom-property-pattern/__tests__/index.js index cce701d815..117a52e753 100644 --- a/lib/rules/custom-property-pattern/__tests__/index.js +++ b/lib/rules/custom-property-pattern/__tests__/index.js @@ -136,7 +136,7 @@ testRule({ code: 'a { color: var($foo-color); }', }, { - code: 'a { color: var(tokens.$bar-color); }', + code: 'a { color: var(bar.$baz); }', }, { code: 'a { --#{$variable-prefix}table-bg: rgba(#a1f2b0, 0.3); }', From 16d52e8198f2b4e3e4468fdd2d5bb7f6f73946e3 Mon Sep 17 00:00:00 2001 From: kawaguchi <30929824+kawaguchi1102@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:30:33 +0900 Subject: [PATCH 5/8] Update lib/rules/custom-property-pattern/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/custom-property-pattern/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/custom-property-pattern/__tests__/index.js b/lib/rules/custom-property-pattern/__tests__/index.js index 117a52e753..58ed209b27 100644 --- a/lib/rules/custom-property-pattern/__tests__/index.js +++ b/lib/rules/custom-property-pattern/__tests__/index.js @@ -139,7 +139,7 @@ testRule({ code: 'a { color: var(bar.$baz); }', }, { - code: 'a { --#{$variable-prefix}table-bg: rgba(#a1f2b0, 0.3); }', + code: 'a { --#{$bar}: 0; }', }, { code: 'a { color: var( #{$variable-color} ); }', From 10357df154ebea07bb69c04dae81a706ee3d593d Mon Sep 17 00:00:00 2001 From: kawaguchi <30929824+kawaguchi1102@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:30:39 +0900 Subject: [PATCH 6/8] Update lib/rules/custom-property-pattern/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/custom-property-pattern/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/custom-property-pattern/__tests__/index.js b/lib/rules/custom-property-pattern/__tests__/index.js index 58ed209b27..47b86117a7 100644 --- a/lib/rules/custom-property-pattern/__tests__/index.js +++ b/lib/rules/custom-property-pattern/__tests__/index.js @@ -142,7 +142,7 @@ testRule({ code: 'a { --#{$bar}: 0; }', }, { - code: 'a { color: var( #{$variable-color} ); }', + code: 'a { color: var( #{$bar} ); }', }, { code: 'a { color: var( --#{$variable-color} ); }', From 503ed7bbd5ecea7695da446f0f0592b69318870e Mon Sep 17 00:00:00 2001 From: kawaguchi <30929824+kawaguchi1102@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:30:44 +0900 Subject: [PATCH 7/8] Update lib/rules/custom-property-pattern/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/custom-property-pattern/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/custom-property-pattern/__tests__/index.js b/lib/rules/custom-property-pattern/__tests__/index.js index 47b86117a7..aba026064b 100644 --- a/lib/rules/custom-property-pattern/__tests__/index.js +++ b/lib/rules/custom-property-pattern/__tests__/index.js @@ -145,7 +145,7 @@ testRule({ code: 'a { color: var( #{$bar} ); }', }, { - code: 'a { color: var( --#{$variable-color} ); }', + code: 'a { color: var( --#{$bar} ); }', }, ], }); From b56e6e328e55fb29358fad1939628e628e5db60d Mon Sep 17 00:00:00 2001 From: kawaguchi Date: Sun, 6 Mar 2022 01:28:54 +0900 Subject: [PATCH 8/8] refactor --- lib/rules/custom-property-pattern/index.js | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/rules/custom-property-pattern/index.js b/lib/rules/custom-property-pattern/index.js index b9419ba015..3f54ed07a0 100644 --- a/lib/rules/custom-property-pattern/index.js +++ b/lib/rules/custom-property-pattern/index.js @@ -34,6 +34,18 @@ const rule = (primary) => { const regexpPattern = isString(primary) ? new RegExp(primary) : primary; + /** + * @param {string} property + * @returns {boolean} + */ + function check(property) { + return ( + !isStandardSyntaxProperty(property) || + !isCustomProperty(property) || + regexpPattern.test(property.slice(2)) + ); + } + root.walkDecls((decl) => { const { prop, value } = decl; @@ -48,20 +60,12 @@ const rule = (primary) => { const { value: firstNodeValue } = nodes[0]; - if (!isStandardSyntaxProperty(firstNodeValue)) return; - - if (!isCustomProperty(firstNodeValue)) return; - - if (regexpPattern.test(firstNodeValue.slice(2))) return; + if (check(firstNodeValue)) return; complain(declarationValueIndex(decl) + sourceIndex, decl); }); - if (!isStandardSyntaxProperty(prop)) return; - - if (!isCustomProperty(prop)) return; - - if (regexpPattern.test(prop.slice(2))) return; + if (check(prop)) return; complain(0, decl); });