Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 22 additions & 59 deletions src/rules/no-important.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
* @author Yann Bertrand
*/

//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------

import { findOffsets } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -52,77 +46,46 @@ export default {
},

create(context) {
const { sourceCode } = context;

return {
Declaration(node) {
if (node.important) {
const declarationText = context.sourceCode.getText(node);
const declarationText = sourceCode.getText(node);
const textWithoutComments = declarationText.replace(
commentPattern,
match => match.replace(/[^\n]/gu, " "),
);
const importantMatch =
importantPattern.exec(textWithoutComments);

const {
lineOffset: startLineOffset,
columnOffset: startColumnOffset,
} = findOffsets(declarationText, importantMatch.index);

const {
lineOffset: endLineOffset,
columnOffset: endColumnOffset,
} = findOffsets(
declarationText,
importantMatch.index + importantMatch[0].length,
);

const nodeStartLine = node.loc.start.line;
const nodeStartColumn = node.loc.start.column;
const startLine = nodeStartLine + startLineOffset;
const endLine = nodeStartLine + endLineOffset;
const startColumn =
(startLine === nodeStartLine ? nodeStartColumn : 1) +
startColumnOffset;
const endColumn =
(endLine === nodeStartLine ? nodeStartColumn : 1) +
endColumnOffset;
const importantStartOffset = importantMatch.index;
const importantEndOffset =
importantStartOffset + importantMatch[0].length;
const nodeStartOffset = node.loc.start.offset;

context.report({
loc: {
start: {
line: startLine,
column: startColumn,
},
end: {
line: endLine,
column: endColumn,
},
start: sourceCode.getLocFromIndex(
nodeStartOffset + importantStartOffset,
),
end: sourceCode.getLocFromIndex(
nodeStartOffset + importantEndOffset,
),
},
messageId: "unexpectedImportant",
suggest: [
{
messageId: "removeImportant",
fix(fixer) {
const importantStart = importantMatch.index;
const importantEnd =
importantStart +
importantMatch[0].length;

// Find any trailing whitespace before the !important
const valuePart = declarationText.slice(
0,
importantStart,
);
const whitespaceEnd = valuePart.search(
trailingWhitespacePattern,
);

const start =
node.loc.start.offset + whitespaceEnd;
const end =
node.loc.start.offset + importantEnd;

return fixer.removeRange([start, end]);
// Find any trailing whitespace before the `!important`
const whitespaceEndOffset = declarationText
.slice(0, importantStartOffset)
.search(trailingWhitespacePattern);

return fixer.removeRange([
nodeStartOffset + whitespaceEndOffset,
nodeStartOffset + importantEndOffset,
]);
},
},
],
Expand Down
25 changes: 0 additions & 25 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,3 @@ export function isSyntaxMatchError(error) {
export function isSyntaxReferenceError(error) {
return typeof error.reference === "string";
}

/**
* Finds the line and column offsets for a given offset in a string.
* @param {string} text The text to search.
* @param {number} offset The offset to find.
* @returns {{lineOffset:number,columnOffset:number}} The location of the offset.
*/
export function findOffsets(text, offset) {
let lineOffset = 0;
let columnOffset = 0;

for (let i = 0; i < offset; i++) {
if (text[i] === "\n") {
lineOffset++;
columnOffset = 0;
} else {
columnOffset++;
}
}

return {
lineOffset,
columnOffset,
};
}
Loading