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 custom parameter messages to *-pattern rules #264

Merged
merged 3 commits into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 30 additions & 23 deletions __tests__/index.test.js
Expand Up @@ -4,69 +4,76 @@ const config = require('../');
const fs = require('fs');
const stylelint = require('stylelint');

const validCss = fs.readFileSync('./__tests__/valid.css', 'utf-8');
const invalidCss = fs.readFileSync('./__tests__/invalid.css', 'utf-8');

describe('flags no warnings with valid css', () => {
const validCss = fs.readFileSync('./__tests__/valid.css', 'utf-8');
let result;

beforeEach(() => {
result = stylelint.lint({
beforeEach(async () => {
result = await stylelint.lint({
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] The change uses async/await to simplify the test code.

code: validCss,
config,
});
});

it('did not error', () => {
return result.then((data) => expect(data.errored).toBeFalsy());
expect(result.errored).toBe(false);
});

it('flags no warnings', () => {
return result.then((data) => expect(data.results[0].warnings).toHaveLength(0));
expect(result.results[0].warnings).toHaveLength(0);
});
});

describe('flags warnings with invalid css', () => {
const invalidCss = fs.readFileSync('./__tests__/invalid.css', 'utf-8');
let result;

beforeEach(() => {
result = stylelint.lint({
beforeEach(async () => {
result = await stylelint.lint({
code: invalidCss,
config,
});
});

it('did error', () => {
return result.then((data) => expect(data.errored).toBeTruthy());
expect(result.errored).toBe(true);
});

it('flags one warning', () => {
return result.then((data) => expect(data.results[0].warnings).toHaveLength(1));
it('flags warnings', () => {
expect(result.results[0].warnings).toHaveLength(6);
});

it('correct warning text', () => {
return result.then((data) =>
expect(data.results[0].warnings[0].text).toBe(
'Expected a leading zero (number-leading-zero)',
),
);
expect(result.results[0].warnings.map((w) => w.text)).toEqual([
'Expected custom media query name "--FOO" to be kebab-case',
'Expected custom property name "--FOO" to be kebab-case',
'Expected keyframe name "FOO" to be kebab-case',
'Expected a leading zero (number-leading-zero)',
'Expected class selector ".FOO" to be kebab-case',
'Expected id selector "#FOO" to be kebab-case',
]);
});

it('correct rule flagged', () => {
return result.then((data) =>
expect(data.results[0].warnings[0].rule).toBe('number-leading-zero'),
);
expect(result.results[0].warnings.map((w) => w.rule)).toEqual([
'custom-media-pattern',
'custom-property-pattern',
'keyframes-name-pattern',
'number-leading-zero',
'selector-class-pattern',
'selector-id-pattern',
]);
});

it('correct severity flagged', () => {
return result.then((data) => expect(data.results[0].warnings[0].severity).toBe('error'));
expect(result.results[0].warnings[0].severity).toBe('error');
});

it('correct line number', () => {
return result.then((data) => expect(data.results[0].warnings[0].line).toBe(2));
expect(result.results[0].warnings[0].line).toBe(5);
});

it('correct column number', () => {
return result.then((data) => expect(data.results[0].warnings[0].column).toBe(8));
expect(result.results[0].warnings[0].column).toBe(15);
});
});
10 changes: 10 additions & 0 deletions __tests__/invalid.css
@@ -1,3 +1,13 @@
a {
top: .2em;
}

@custom-media --FOO;

:root { --FOO: 1px; }

@keyframes FOO { /* ... */ }

.FOO { /* ... */ }

#FOO { /* ... */ }
10 changes: 5 additions & 5 deletions index.js
Expand Up @@ -54,13 +54,13 @@ module.exports = {
'custom-media-pattern': [
'^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
{
message: 'Expected custom media query name to be kebab-case',
message: (name) => `Expected custom media query name "${name}" to be kebab-case`,
},
],
'custom-property-pattern': [
'^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
{
message: 'Expected custom property name to be kebab-case',
message: (name) => `Expected custom property name "${name}" to be kebab-case`,
},
],
'declaration-bang-space-after': 'never',
Expand Down Expand Up @@ -98,7 +98,7 @@ module.exports = {
'keyframes-name-pattern': [
'^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
{
message: 'Expected keyframe name to be kebab-case',
message: (name) => `Expected keyframe name "${name}" to be kebab-case`,
},
],
'length-zero-no-unit': true,
Expand Down Expand Up @@ -136,7 +136,7 @@ module.exports = {
'selector-class-pattern': [
'^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
{
message: 'Expected class selector to be kebab-case',
message: (selector) => `Expected class selector "${selector}" to be kebab-case`,
},
],
'selector-combinator-space-after': 'always',
Expand All @@ -145,7 +145,7 @@ module.exports = {
'selector-id-pattern': [
'^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
{
message: 'Expected id selector to be kebab-case',
message: (selector) => `Expected id selector "${selector}" to be kebab-case`,
},
],
'selector-list-comma-newline-after': 'always',
Expand Down
29 changes: 14 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -60,7 +60,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"remark-cli": "^11.0.0",
"stylelint": "^14.12.1"
"stylelint": "github:stylelint/stylelint#add-custom-message-formatting-to-standard-config-rules"
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"stylelint": "^14.11.0"
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
Expand Down