This repository was archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
70 lines (58 loc) · 2.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const path = require('path');
const getSyntaxBlocks = require('./lib/getSyntaxBlocks');
const splitLines = require('split-lines');
const reindent = require('./lib/reindent');
const sourceToLineMap = new Map();
function transformer(options) {
options = options || {};
options.syntax = options.syntax || 'css';
return function transformCode(code, filepath) {
// Workaround for stylelint bug
// https://github.com/stylelint/stylelint/issues/2195
if (!path.isAbsolute(filepath)) filepath = path.join(process.cwd(), filepath);
const extractedToSourceLineMap = new Map();
let extractedCode = '';
let currentExtractedCodeLine = 0;
getSyntaxBlocks(code, options).forEach((block) => {
let cssContent = block.raw;
if (!cssContent) return;
const reindentData = reindent(cssContent);
cssContent = reindentData.text;
const startLine = block.position.start.line + 1;
const linesWithin = splitLines(cssContent).length;
for (let i = 0; i < linesWithin; i++) {
currentExtractedCodeLine += 1;
extractedToSourceLineMap.set(currentExtractedCodeLine, {
line: startLine + i,
indentColumns: reindentData.indentColumns,
});
}
currentExtractedCodeLine += 1;
extractedCode += cssContent + '\n\n';
});
sourceToLineMap.set(filepath, extractedToSourceLineMap);
return extractedCode;
};
}
function transformResult(result, filepath) {
const extractedToSourceLineMap = sourceToLineMap.get(filepath);
const newWarnings = result.warnings.reduce((memo, warning) => {
const warningSourceMap = extractedToSourceLineMap.get(warning.line);
if (warning.line) {
warning.line = warningSourceMap.line;
}
if (warning.column) {
warning.column = warning.column + warningSourceMap.indentColumns;
}
memo.push(warning);
return memo;
}, []);
return Object.assign(result, { warnings: newWarnings });
}
module.exports = function (options) {
return {
code: transformer(options),
result: transformResult,
};
};