UNPKG

4.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var highlight_1 = require("../highlight/highlight");
4var logger_1 = require("./logger");
5var fs_1 = require("fs");
6var helpers_1 = require("../util/helpers");
7function runSassDiagnostics(context, sassError) {
8 if (!sassError) {
9 return [];
10 }
11 var d = {
12 level: 'error',
13 type: 'sass',
14 language: 'scss',
15 header: 'sass error',
16 code: sassError.status && sassError.status.toString(),
17 relFileName: null,
18 absFileName: null,
19 messageText: sassError.message,
20 lines: []
21 };
22 if (sassError.file) {
23 d.absFileName = sassError.file;
24 d.relFileName = logger_1.Logger.formatFileName(context.rootDir, d.absFileName);
25 d.header = logger_1.Logger.formatHeader('sass', d.absFileName, context.rootDir, sassError.line);
26 if (sassError.line > -1) {
27 try {
28 var sourceText = fs_1.readFileSync(d.absFileName, 'utf8');
29 var srcLines = helpers_1.splitLineBreaks(sourceText);
30 var htmlLines = srcLines;
31 try {
32 htmlLines = helpers_1.splitLineBreaks(highlight_1.highlight(d.language, sourceText, true).value);
33 }
34 catch (e) { }
35 var errorLine = {
36 lineIndex: sassError.line - 1,
37 lineNumber: sassError.line,
38 text: srcLines[sassError.line - 1],
39 html: htmlLines[sassError.line - 1],
40 errorCharStart: sassError.column,
41 errorLength: 0
42 };
43 if (errorLine.html.indexOf('class="hljs') === -1) {
44 try {
45 errorLine.html = highlight_1.highlight(d.language, errorLine.text, true).value;
46 }
47 catch (e) { }
48 }
49 for (var i = errorLine.errorCharStart; i >= 0; i--) {
50 if (STOP_CHARS.indexOf(errorLine.text.charAt(i)) > -1) {
51 break;
52 }
53 errorLine.errorCharStart = i;
54 }
55 for (var j = errorLine.errorCharStart; j <= errorLine.text.length; j++) {
56 if (STOP_CHARS.indexOf(errorLine.text.charAt(j)) > -1) {
57 break;
58 }
59 errorLine.errorLength++;
60 }
61 if (errorLine.errorLength === 0 && errorLine.errorCharStart > 0) {
62 errorLine.errorLength = 1;
63 errorLine.errorCharStart--;
64 }
65 d.lines.push(errorLine);
66 if (errorLine.lineIndex > 0) {
67 var previousLine = {
68 lineIndex: errorLine.lineIndex - 1,
69 lineNumber: errorLine.lineNumber - 1,
70 text: srcLines[errorLine.lineIndex - 1],
71 html: htmlLines[errorLine.lineIndex - 1],
72 errorCharStart: -1,
73 errorLength: -1
74 };
75 if (previousLine.html.indexOf('class="hljs') === -1) {
76 try {
77 previousLine.html = highlight_1.highlight(d.language, previousLine.text, true).value;
78 }
79 catch (e) { }
80 }
81 d.lines.unshift(previousLine);
82 }
83 if (errorLine.lineIndex + 1 < srcLines.length) {
84 var nextLine = {
85 lineIndex: errorLine.lineIndex + 1,
86 lineNumber: errorLine.lineNumber + 1,
87 text: srcLines[errorLine.lineIndex + 1],
88 html: htmlLines[errorLine.lineIndex + 1],
89 errorCharStart: -1,
90 errorLength: -1
91 };
92 if (nextLine.html.indexOf('class="hljs') === -1) {
93 try {
94 nextLine.html = highlight_1.highlight(d.language, nextLine.text, true).value;
95 }
96 catch (e) { }
97 }
98 d.lines.push(nextLine);
99 }
100 }
101 catch (e) {
102 logger_1.Logger.debug("sass loadDiagnostic, " + e);
103 }
104 }
105 }
106 return [d];
107}
108exports.runSassDiagnostics = runSassDiagnostics;
109var STOP_CHARS = ['', '\n', '\r', '\t', ' ', ':', ';', ',', '{', '}', '.', '#', '@', '!', '[', ']', '(', ')', '&', '+', '~', '^', '*', '$'];