Skip to content

Commit

Permalink
fix(stylelint): fix indentation errors (fixes #693)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Nov 29, 2021
1 parent ad84d6d commit 4790c89
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
1 change: 0 additions & 1 deletion packages/stylelint/src/index.ts
Expand Up @@ -7,6 +7,5 @@ export default {
'value-no-vendor-prefix': true,
'no-empty-source': null,
'no-extra-semicolons': null,
'no-missing-end-of-source-newline': null,
},
};
83 changes: 55 additions & 28 deletions packages/stylelint/src/preprocessor.ts
Expand Up @@ -35,9 +35,21 @@ type LintResult = {
warnings: Warning[];
};

interface IPosition {
line: number;
column: number;
}

interface ISourceOffset {
generated: IPosition;
name: string;
original: IPosition;
}

function preprocessor() {
const errors: Errors = {};
const cache: Cache = {};
const offsets: Record<string, ISourceOffset[] | undefined> = {};

return {
code(input: string, filename: string) {
Expand All @@ -50,8 +62,10 @@ function preprocessor() {

cache[filename] = undefined;
errors[filename] = undefined;
offsets[filename] = [];
} catch (e) {
cache[filename] = undefined;
offsets[filename] = undefined;
errors[filename] = e;

// Ignore parse errors here
Expand All @@ -66,41 +80,55 @@ function preprocessor() {
}

// Construct a CSS-ish file from the unprocessed style rules
let cssText = '';

Object.keys(rules).forEach((selector) => {
const rule = rules[selector];

// Append new lines until we get to the start line number
let line = cssText.split('\n').length;

while (rule.start && line < rule.start.line) {
cssText += '\n';
line++;
}

cssText += `.${rule.displayName} {`;

// Append blank spaces until we get to the start column number
const last = cssText.split('\n').pop();

let column = last ? last.length : 0;

while (rule.start && column < rule.start.column) {
cssText += ' ';
column++;
}
let generatedLineNumber = 1;
let cssText = Object.values(rules)
.map((rule) => {
const ruleText = `.${rule.className} {${rule.cssText}}`;

if (rule.start && 'line' in rule.start) {
offsets[filename]?.push({
generated: {
line: generatedLineNumber,
column: 1,
},
original: {
...rule.start,
},
name: rule.displayName,
});

generatedLineNumber += 1;
}

cssText += `${rule.cssText} }`;
});
generatedLineNumber += ruleText.split('\n').length + 2;
return ruleText;
})
.join('\n\n');

cache[filename] = replacements;
offsets[filename] = offsets[filename]?.reverse();

console.log(cssText);

return cssText;
return cssText + '\n';
},
result(result: LintResult, filename: string) {
const error = errors[filename];
const replacements = cache[filename];
const sourceMap = offsets[filename];

if (sourceMap) {
result.warnings = result.warnings.map((warning) => {
const offset = sourceMap.find(
(o) => o.generated.line <= warning.line
);
if (offset) {
warning.line += offset.original.line - offset.generated.line;
}

return warning;
});
}

if (error) {
// Babel adds this to the error message
Expand Down Expand Up @@ -150,7 +178,6 @@ function preprocessor() {
// Correct the line and column numbers to what's replaced
result.warnings.forEach((w) => {
/* eslint-disable no-param-reassign */

if (w.line === original.start.line) {
// If the error is on the same line where an interpolation started, we need to adjust the line and column numbers
// Because a replacement would have increased or decreased the column numbers
Expand Down

0 comments on commit 4790c89

Please sign in to comment.