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

Fix column position for *-no-important rules #5957

Merged
merged 1 commit into from
Mar 8, 2022
Merged
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
6 changes: 3 additions & 3 deletions lib/rules/declaration-no-important/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ testRule({
description: 'with !important',
message: messages.rejected,
line: 1,
column: 18,
column: 17,
},
{
code: 'a { color: pink ! important; }',
description: 'with ! important',
message: messages.rejected,
line: 1,
column: 19,
column: 17,
},
{
code: 'a { color: pink!important; }',
description: 'with value!important',
message: messages.rejected,
line: 1,
column: 17,
column: 16,
},
],
});
8 changes: 7 additions & 1 deletion lib/rules/declaration-no-important/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const getImportantPosition = require('../../utils/getImportantPosition');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { assert } = require('../../utils/validateTypes');

const ruleName = 'declaration-no-important';

Expand All @@ -28,10 +30,14 @@ const rule = (primary) => {
return;
}

const pos = getImportantPosition(decl.toString());

assert(pos);

report({
message: messages.rejected,
node: decl,
word: 'important',
index: pos.index,
result,
ruleName,
});
Expand Down
14 changes: 7 additions & 7 deletions lib/rules/keyframe-declaration-no-important/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,49 @@ testRule({
description: 'with !important',
message: messages.rejected,
line: 1,
column: 44,
column: 43,
},
{
code: '@-webkit-keyframes important { from { margin: 1px !important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 52,
column: 51,
},
{
code: '@-WEBKIT-KEYFRAMES important { from { margin: 1px !important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 52,
column: 51,
},
{
code: '@keyframes important { from { margin: 1px!important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 43,
column: 42,
},
{
code: '@keyframes important { from { margin: 1px ! important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 45,
column: 43,
},
{
code: '@kEyFrAmEs important { from { margin: 1px !important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 44,
column: 43,
},
{
code: '@KEYFRAMES important { from { margin: 1px !important; } }',
description: 'with !important',
message: messages.rejected,
line: 1,
column: 44,
column: 43,
},
],
});
8 changes: 7 additions & 1 deletion lib/rules/keyframe-declaration-no-important/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const getImportantPosition = require('../../utils/getImportantPosition');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { assert } = require('../../utils/validateTypes');

const ruleName = 'keyframe-declaration-no-important';

Expand All @@ -29,10 +31,14 @@ const rule = (primary) => {
return;
}

const pos = getImportantPosition(decl.toString());

assert(pos);

report({
message: messages.rejected,
node: decl,
word: 'important',
index: pos.index,
result,
ruleName,
});
Expand Down
33 changes: 33 additions & 0 deletions lib/utils/__tests__/getImportantPosition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const getImportantPosition = require('../getImportantPosition');

describe('getImportantPosition', () => {
it('returns a position of "!important"', () => {
expect(getImportantPosition('color: pink !important')).toEqual({ index: 12, endIndex: 22 });
});

it('returns a position of "!important" with upper-case', () => {
expect(getImportantPosition('color: pink !IMPORTANT')).toEqual({ index: 12, endIndex: 22 });
});

it('returns a position of "!important" including whitespaces', () => {
expect(getImportantPosition('color: pink ! important')).toEqual({ index: 12, endIndex: 25 });
});

it('returns a position of "!important" including whitespaces with upper-case', () => {
expect(getImportantPosition('color: pink ! IMPORTANT')).toEqual({ index: 12, endIndex: 25 });
});

it('returns undefined if no "!important"', () => {
expect(getImportantPosition('color: pink')).toBeUndefined();
});

it('returns undefined if just a "important"', () => {
expect(getImportantPosition('color: important')).toBeUndefined();
});

it('returns undefined if just a typo of "!important"', () => {
expect(getImportantPosition('color: !importanttt')).toBeUndefined();
});
});
17 changes: 17 additions & 0 deletions lib/utils/getImportantPosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

/**
* Returns a position of `!important` (or `! important` including whitespaces)
* from the specified CSS source code. If not found, returns `undefined`.
*
* @param {string} source
* @returns {{ index: number, endIndex: number } | undefined}
*/
module.exports = function getImportantPosition(source) {
const pattern = /!\s*important\b/gi;
const match = pattern.exec(source);

if (!match) return;

return { index: match.index, endIndex: pattern.lastIndex };
};