Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jeddy3 committed May 19, 2021
1 parent 297d479 commit 4f0cf69
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
Expand Up @@ -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

Expand Down
Expand Up @@ -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 },
],
},
],
Expand Down
23 changes: 19 additions & 4 deletions lib/rules/dashed-ident-no-missing-reference-function/index.js
Expand Up @@ -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');
Expand All @@ -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, {
Expand All @@ -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);

Expand All @@ -34,6 +43,8 @@ function rule(actual) {

if (!isDashedIdent(node)) return;

if (!isKnownCustomProperty(node)) return;

report({
message: messages.rejected(node.value),
node: decl,
Expand All @@ -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;
Expand Down

0 comments on commit 4f0cf69

Please sign in to comment.