From 4f0cf698649365b2111b07e9eb1834184f53c237 Mon Sep 17 00:00:00 2001 From: jeddy3 Date: Wed, 17 Feb 2021 13:14:10 +0000 Subject: [PATCH] wip --- .../README.md | 4 ++- .../__tests__/index.js | 29 +++++++++++-------- .../index.js | 23 ++++++++++++--- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/rules/dashed-ident-no-missing-reference-function/README.md b/lib/rules/dashed-ident-no-missing-reference-function/README.md index f67092c4ad..a524435bba 100644 --- a/lib/rules/dashed-ident-no-missing-reference-function/README.md +++ b/lib/rules/dashed-ident-no-missing-reference-function/README.md @@ -9,7 +9,9 @@ Disallow missing reference functions for dashed-idents. * This function and this dashed-ident */ ``` -[Dashed-idents](https://drafts.csswg.org/css-values-4/#dashed-idents) are most often referenced using functions like `var()` and `env()`. This rule reports dashed-idents that are missing a reference function. +[Dashed-idents](https://drafts.csswg.org/css-values-4/#dashed-idents) are most often referenced using functions like `var()` and `env()`. + +This rule reports dashed-idents that are missing a reference function. ## Options diff --git a/lib/rules/dashed-ident-no-missing-reference-function/__tests__/index.js b/lib/rules/dashed-ident-no-missing-reference-function/__tests__/index.js index 67ccb7960a..4047e7c4b6 100644 --- a/lib/rules/dashed-ident-no-missing-reference-function/__tests__/index.js +++ b/lib/rules/dashed-ident-no-missing-reference-function/__tests__/index.js @@ -45,45 +45,50 @@ testRule({ reject: [ { - code: 'a { color: --foo; }', + code: 'a { --foo: red; color: --foo; }', message: messages.rejected('--foo'), line: 1, - column: 12, + column: 24, }, { - code: 'a { --foo: --bar; }', + code: ':root { --bar: 0; } a { --foo: --bar; }', message: messages.rejected('--bar'), line: 1, - column: 12, + column: 32, }, { - code: 'a { color: calc(var(--foo) + --bar)); }', + code: ':root { --bar: 0; } a { color: calc(var(--foo) + --bar)); }', message: messages.rejected('--bar'), line: 1, - column: 30, + column: 50, }, { - code: 'a { color: --foo, red; }', + code: ':root { --foo: pink; } a { color: --foo, red; }', message: messages.rejected('--foo'), line: 1, - column: 12, + column: 36, }, { - code: 'a { color: --foo(--bar); }', + code: ':root { --bar: 0; } a { color: --foo(--bar); }', message: messages.rejected('--bar'), line: 1, - column: 18, + column: 38, }, { code: stripIndent` + :root { + --bar: 0; + --baz: 0; + } + a { --foo: --bar; color: --baz; } `, warnings: [ - { message: messages.rejected('--bar'), line: 2, column: 9 }, - { message: messages.rejected('--baz'), line: 3, column: 9 }, + { message: messages.rejected('--bar'), line: 7, column: 9 }, + { message: messages.rejected('--baz'), line: 8, column: 9 }, ], }, ], diff --git a/lib/rules/dashed-ident-no-missing-reference-function/index.js b/lib/rules/dashed-ident-no-missing-reference-function/index.js index 5ec4f7941f..cec4fe3321 100644 --- a/lib/rules/dashed-ident-no-missing-reference-function/index.js +++ b/lib/rules/dashed-ident-no-missing-reference-function/index.js @@ -5,6 +5,7 @@ const valueParser = require('postcss-value-parser'); const declarationValueIndex = require('../../utils/declarationValueIndex'); +const isCustomProperty = require('../../utils/isCustomProperty'); const report = require('../../utils/report'); const ruleMessages = require('../../utils/ruleMessages'); const validateOptions = require('../../utils/validateOptions'); @@ -18,6 +19,8 @@ const messages = ruleMessages(ruleName, { const REFERENCE_FUNCTIONS = new Set(['color', 'env', 'var']); +const customProperties = new Set(); + function rule(actual) { return (root, result) => { const validOptions = validateOptions(result, ruleName, { @@ -26,6 +29,12 @@ function rule(actual) { if (!validOptions) return; + root.walkDecls(({ prop }) => { + if (isCustomProperty(prop)) customProperties.add(prop); + }); + + console.log(customProperties); + root.walkDecls((decl) => { const parsedValue = valueParser(decl.value); @@ -34,6 +43,8 @@ function rule(actual) { if (!isDashedIdent(node)) return; + if (!isKnownCustomProperty(node)) return; + report({ message: messages.rejected(node.value), node: decl, @@ -48,12 +59,16 @@ function rule(actual) { }; } -function isDashedIdent(node) { - return node.type === 'word' && node.value.startsWith('--'); +function isDashedIdent({ type, value }) { + return type === 'word' && value.startsWith('--'); +} + +function isReferenceFunction({ type, value }) { + return type === 'function' && REFERENCE_FUNCTIONS.has(value); } -function isReferenceFunction(node) { - return node.type === 'function' && REFERENCE_FUNCTIONS.has(node.value); +function isKnownCustomProperty({ value }) { + return customProperties.has(value); } rule.ruleName = ruleName;