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

Add color-hex-alpha rule #4891

Closed
wants to merge 21 commits into from
Closed
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
1 change: 1 addition & 0 deletions docs/user-guide/rules/list.md
Expand Up @@ -88,6 +88,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps.
### Color

- [`color-function-notation`](../../../lib/rules/color-function-notation/README.md): Specify modern or legacy notation for applicable color-functions (Autofixable).
- [`color-hex-alpha`](../../../lib/rules/color-hex-alpha/README.md): Require or disallow alpha channel for hex colors.
- [`color-named`](../../../lib/rules/color-named/README.md): Require (where possible) or disallow named colors.
- [`color-no-hex`](../../../lib/rules/color-no-hex/README.md): Disallow hex colors.

Expand Down
66 changes: 66 additions & 0 deletions lib/rules/color-hex-alpha/README.md
@@ -0,0 +1,66 @@
# color-hex-alpha

Require or disallow alpha channel for hex colors.

<!-- prettier-ignore -->
```css
a { color: #ffff }
/** ↑
* This hex alpha */
```

## Options

`string`: `"always"|"never"`

### `"always"`

The following patterns are considered violations:

<!-- prettier-ignore -->
```css
a { color: #fff; }
```

<!-- prettier-ignore -->
```css
a { color: #ffffff; }
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
a { color: #fffa; }
```

<!-- prettier-ignore -->
```css
a { color: #ffffffaa; }
```

### `"never"`

The following patterns are considered violations:

<!-- prettier-ignore -->
```css
a { color: #fffa; }
```

<!-- prettier-ignore -->
```css
a { color: #ffffffaa; }
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
a { color: #fff; }
```

<!-- prettier-ignore -->
```css
a { color: #ffffff; }
```
87 changes: 87 additions & 0 deletions lib/rules/color-hex-alpha/__tests__/index.js
@@ -0,0 +1,87 @@
'use strict';
ssivanatarajan marked this conversation as resolved.
Show resolved Hide resolved

const { messages, ruleName } = require('..');

testRule({
ruleName,
config: 'never',
accept: [
{
code: 'a { color: #fff; }',
},
{
code: 'a { color: #ffffff; }',
},
],
reject: [
{
code: 'a { color: #ffff; }',
message: messages.unexpected('#ffff'),
line: 1,
column: 11,
},
],
});

testRule({
ruleName,
config: 'always',
accept: [
{
code: 'a { color: #ffff; }',
},
{
code: 'a { color: #ffffffff; }',
},
{
code: 'a { background: linear-gradient(to left, #fffa, red 100%); }',
},
{
code: 'a { background: url(#fff); }',
},
],
reject: [
{
code: 'a { color:#fff; }',
message: messages.expected('#fff'),
line: 1,
column: 11,
},
{
code: 'a { color:#ffffff; }',
message: messages.expected('#ffffff'),
line: 1,
column: 11,
},
{
code: 'a { background: linear-gradient(to left, #fff, red 100%); }',
message: messages.expected('#fff'),
line: 1,
column: 42,
},
],
});
testRule({
ruleName,
config: ['never'],
syntax: 'scss',

accept: [
{
code: 'a { color: #{ff}; }',
description: 'scss interpolation',
},
{
code: 'a { $var: #fff;}',
description: 'scss interpolation',
},
],
reject: [
{
code: 'a { $var: #fffa;}',
message: messages.expected('#fff'),
line: 1,
column: 8,
},
],
Copy link
Member

Choose a reason for hiding this comment

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

Let's add an accept test for:

$var: #fff;

And a reject test for:

$var: #fffa;

The latter will likely fail until the if (!isStandardSyntaxDeclaration(decl)) return; conditional is removed.

});
ssivanatarajan marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions lib/rules/color-hex-alpha/index.js
@@ -0,0 +1,65 @@
// @ts-nocheck
'use strict';

const declarationValueIndex = require('../../utils/declarationValueIndex');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');

const ruleName = 'color-hex-alpha';

const messages = ruleMessages(ruleName, {
expected: (hex) => `Expected alpha channel in "${hex}"`,
unexpected: (hex) => `Unexpected alpha channel in "${hex}"`,
});

function rule(primary) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['always', 'never'],
});

if (!validOptions) return;

root.walkDecls((decl) => {
if (!isStandardSyntaxDeclaration(decl)) return;
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove this as per #5010 (comment).


const parsedValue = valueParser(decl.value);

parsedValue.walk((node) => {
if (node.type !== 'word') return;

if (node.value.startsWith('#')) return;

let hexValue = node.value;

if (!isStandardSyntaxValue(hexValue)) return;

if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) {
return;
}

if (primary === 'never' && (hexValue.length === 4 || hexValue.length === 7)) {
return;
}

report({
message:
primary === 'never' ? messages.unexpected(hexValue) : messages.expected(hexValue),
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
});
});
});
};
}

rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -55,6 +55,7 @@ const rules = {
require('./block-opening-brace-space-before'),
)(),
'color-function-notation': importLazy(() => require('./color-function-notation'))(),
'color-hex-alpha': importLazy(() => require('./color-hex-alpha'))(),
'color-hex-case': importLazy(() => require('./color-hex-case'))(),
'color-hex-length': importLazy(() => require('./color-hex-length'))(),
'color-named': importLazy(() => require('./color-named'))(),
Expand Down