1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
14 | if (k2 === undefined) k2 = k;
|
15 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
16 | }) : (function(o, m, k, k2) {
|
17 | if (k2 === undefined) k2 = k;
|
18 | o[k2] = m[k];
|
19 | }));
|
20 | exports.__esModule = true;
|
21 | exports.SassFormatter = exports.defaultSassFormatterConfig = void 0;
|
22 | var utility_1 = require("./utility");
|
23 | var suf_regex_1 = require("suf-regex");
|
24 | var state_1 = require("./state");
|
25 | var format_header_1 = require("./formatters/format.header");
|
26 | var format_property_1 = require("./formatters/format.property");
|
27 | var format_blockComment_1 = require("./formatters/format.blockComment");
|
28 | var format_atForwardOrAtUse_1 = require("./formatters/format.atForwardOrAtUse");
|
29 | var sassTextLine_1 = require("./sassTextLine");
|
30 | var logger_1 = require("./logger");
|
31 | var config_1 = require("./config");
|
32 | __createBinding(exports, config_1, "defaultSassFormatterConfig");
|
33 | var SassFormatter = (function () {
|
34 | function SassFormatter() {
|
35 | }
|
36 | SassFormatter.Format = function (text, config) {
|
37 | var STATE = new state_1.FormattingState();
|
38 | STATE.lines = text.split(/\r?\n/);
|
39 | STATE.CONFIG = __assign(__assign({}, STATE.CONFIG), config);
|
40 | STATE.LINE_ENDING = STATE.CONFIG.lineEnding === 'LF' ? '\n' : '\r\n';
|
41 | for (var i = 0; i < STATE.lines.length; i++) {
|
42 | STATE.currentLine = i;
|
43 | this.formatLine(new sassTextLine_1.SassTextLine(STATE.lines[i]), STATE);
|
44 | }
|
45 | if (!STATE.RESULT.endsWith(STATE.LINE_ENDING)) {
|
46 | this.addNewLine(STATE);
|
47 | }
|
48 | if (STATE.CONFIG.debug) {
|
49 | logger_1.LogDebugResult(STATE.RESULT);
|
50 | logger_1.ResetDebugLog();
|
51 | }
|
52 | return STATE.RESULT;
|
53 | };
|
54 | SassFormatter.formatLine = function (line, STATE) {
|
55 | if (suf_regex_1.isBlockCommentStart(line.get())) {
|
56 | STATE.CONTEXT.isInBlockComment = true;
|
57 | STATE.CONTEXT.blockCommentDistance = suf_regex_1.getDistance(line.get(), STATE.CONFIG.tabSize);
|
58 | }
|
59 | else if (STATE.CONTEXT.isInBlockComment &&
|
60 | STATE.CONTEXT.blockCommentDistance >= suf_regex_1.getDistance(line.get(), STATE.CONFIG.tabSize)) {
|
61 | STATE.CONTEXT.isInBlockComment = false;
|
62 | STATE.CONTEXT.blockCommentDistance = 0;
|
63 | }
|
64 | if (STATE.CONTEXT.ignoreLine) {
|
65 | STATE.CONTEXT.ignoreLine = false;
|
66 | this.addNewLine(STATE);
|
67 | STATE.RESULT += line.get();
|
68 | logger_1.PushDebugInfo({
|
69 | title: 'IGNORED',
|
70 | lineNumber: STATE.currentLine,
|
71 | oldLineText: line.get(),
|
72 | debug: STATE.CONFIG.debug,
|
73 | newLineText: 'NULL'
|
74 | });
|
75 | }
|
76 | else if (STATE.CONTEXT.isInBlockComment) {
|
77 | this.handleCommentBlock(STATE, line);
|
78 | }
|
79 | else {
|
80 | if (suf_regex_1.isIgnore(line.get())) {
|
81 | STATE.CONTEXT.ignoreLine = true;
|
82 | this.addNewLine(STATE);
|
83 | STATE.RESULT += line.get();
|
84 | logger_1.PushDebugInfo({
|
85 | title: 'IGNORE',
|
86 | lineNumber: STATE.currentLine,
|
87 | oldLineText: line.get(),
|
88 | debug: STATE.CONFIG.debug,
|
89 | newLineText: 'NULL'
|
90 | });
|
91 | }
|
92 | else {
|
93 | if (suf_regex_1.isSassSpace(line.get())) {
|
94 | STATE.CONTEXT.allowSpace = true;
|
95 | }
|
96 |
|
97 | if (line.isEmptyOrWhitespace ||
|
98 | (STATE.CONFIG.convert ? suf_regex_1.isBracketOrWhitespace(line.get()) : false)) {
|
99 | this.handleEmptyLine(STATE, line);
|
100 | }
|
101 | else {
|
102 | STATE.setLocalContext({
|
103 | isAtKeyframesPoint: utility_1.isKeyframePointAndSetIndentation(line, STATE),
|
104 | indentation: utility_1.getIndentationOffset(line.get(), STATE.CONTEXT.indentation, STATE.CONFIG.tabSize),
|
105 | isIf: /[\t ]*@if/i.test(line.get()),
|
106 | isElse: /[\t ]*@else/i.test(line.get()),
|
107 | isAtKeyframes: suf_regex_1.isKeyframes(line.get()),
|
108 | isReset: suf_regex_1.isReset(line.get()),
|
109 | isAnd: suf_regex_1.isAnd(line.get()),
|
110 | isProp: suf_regex_1.isProperty(line.get()),
|
111 | isAdjacentSelector: suf_regex_1.isAdjacentSelector(line.get()),
|
112 | isHtmlTag: suf_regex_1.isHtmlTag(line.get().trim().split(' ')[0]),
|
113 | isClassOrIdSelector: suf_regex_1.isClassOrId(line.get()),
|
114 | isAtExtend: suf_regex_1.isAtExtend(line.get()),
|
115 | isInterpolatedProp: suf_regex_1.isInterpolatedProperty(line.get()),
|
116 | isInclude: suf_regex_1.isInclude(line.get()),
|
117 | isVariable: suf_regex_1.isVar(line.get()),
|
118 | isImport: suf_regex_1.isAtImport(line.get()),
|
119 | isNestPropHead: /^[\t ]* \S*[\t ]*:[\t ]*$/.test(line.get())
|
120 | });
|
121 | if (STATE.CONFIG.debug) {
|
122 | if (/\/\/[\t ]*info[\t ]*$/.test(line.get())) {
|
123 | logger_1.SetDebugLOCAL_CONTEXT(STATE.LOCAL_CONTEXT);
|
124 | }
|
125 | }
|
126 |
|
127 | if (suf_regex_1.isAtForwardOrAtUse(line.get())) {
|
128 | this.addNewLine(STATE);
|
129 | STATE.RESULT += format_atForwardOrAtUse_1.FormatAtForwardOrAtUse(line, STATE);
|
130 | }
|
131 |
|
132 | else if (this.isBlockHeader(line, STATE)) {
|
133 | this.addNewLine(STATE);
|
134 | STATE.RESULT += format_header_1.FormatBlockHeader(line, STATE);
|
135 | }
|
136 |
|
137 | else if (this.isProperty(STATE)) {
|
138 | STATE.CONTEXT.firstCommaHeader.exists = false;
|
139 | this.addNewLine(STATE);
|
140 | STATE.RESULT += format_property_1.FormatProperty(line, STATE);
|
141 | }
|
142 | else {
|
143 | logger_1.PushDebugInfo({
|
144 | title: 'NO CHANGE',
|
145 | lineNumber: STATE.currentLine,
|
146 | oldLineText: line.get(),
|
147 | debug: STATE.CONFIG.debug,
|
148 | newLineText: 'NULL'
|
149 | });
|
150 | this.addNewLine(STATE);
|
151 | STATE.RESULT += line.get();
|
152 | }
|
153 |
|
154 | STATE.CONTEXT.wasLastLineSelector =
|
155 | STATE.LOCAL_CONTEXT.isClassOrIdSelector ||
|
156 | STATE.LOCAL_CONTEXT.isAdjacentSelector ||
|
157 | STATE.LOCAL_CONTEXT.isHtmlTag;
|
158 | }
|
159 | }
|
160 | }
|
161 | };
|
162 | SassFormatter.handleCommentBlock = function (STATE, line) {
|
163 | this.addNewLine(STATE);
|
164 | var edit = format_blockComment_1.FormatHandleBlockComment(line.get(), STATE);
|
165 | STATE.RESULT += edit;
|
166 | if (suf_regex_1.isBlockCommentEnd(line.get())) {
|
167 | STATE.CONTEXT.isInBlockComment = false;
|
168 | }
|
169 | logger_1.PushDebugInfo({
|
170 | title: 'COMMENT BLOCK',
|
171 | lineNumber: STATE.currentLine,
|
172 | oldLineText: STATE.lines[STATE.currentLine],
|
173 | newLineText: edit,
|
174 | debug: STATE.CONFIG.debug
|
175 | });
|
176 | };
|
177 | SassFormatter.handleEmptyLine = function (STATE, line) {
|
178 | STATE.CONTEXT.firstCommaHeader.exists = false;
|
179 | var pass = true;
|
180 |
|
181 | if (STATE.CONFIG.deleteEmptyRows && !STATE.CONTEXT.isLastLine) {
|
182 | var nextLine = new sassTextLine_1.SassTextLine(STATE.lines[STATE.currentLine + 1]);
|
183 | var compact = !suf_regex_1.isProperty(nextLine.get());
|
184 | var nextLineWillBeDeleted = STATE.CONFIG.convert
|
185 | ? suf_regex_1.isBracketOrWhitespace(nextLine.get())
|
186 | : false;
|
187 | if ((compact && !STATE.CONTEXT.allowSpace && nextLine.isEmptyOrWhitespace) ||
|
188 | (compact && !STATE.CONTEXT.allowSpace && nextLineWillBeDeleted)) {
|
189 | logger_1.PushDebugInfo({
|
190 | title: 'EMPTY LINE: DELETE',
|
191 | nextLine: nextLine,
|
192 | lineNumber: STATE.currentLine,
|
193 | oldLineText: STATE.lines[STATE.currentLine],
|
194 | newLineText: 'DELETED',
|
195 | debug: STATE.CONFIG.debug
|
196 | });
|
197 | pass = false;
|
198 | }
|
199 | }
|
200 | if (line.get().length > 0 && pass) {
|
201 | logger_1.PushDebugInfo({
|
202 | title: 'EMPTY LINE: WHITESPACE',
|
203 | lineNumber: STATE.currentLine,
|
204 | oldLineText: STATE.lines[STATE.currentLine],
|
205 | newLineText: 'NEWLINE',
|
206 | debug: STATE.CONFIG.debug
|
207 | });
|
208 | this.addNewLine(STATE);
|
209 | }
|
210 | else if (pass) {
|
211 | logger_1.PushDebugInfo({
|
212 | title: 'EMPTY LINE',
|
213 | lineNumber: STATE.currentLine,
|
214 | oldLineText: STATE.lines[STATE.currentLine],
|
215 | newLineText: 'NEWLINE',
|
216 | debug: STATE.CONFIG.debug
|
217 | });
|
218 | this.addNewLine(STATE);
|
219 | }
|
220 | };
|
221 | SassFormatter.isBlockHeader = function (line, STATE) {
|
222 | return (!STATE.LOCAL_CONTEXT.isInterpolatedProp &&
|
223 | !STATE.LOCAL_CONTEXT.isAtExtend &&
|
224 | !STATE.LOCAL_CONTEXT.isImport &&
|
225 | (STATE.LOCAL_CONTEXT.isAdjacentSelector ||
|
226 | STATE.LOCAL_CONTEXT.isReset ||
|
227 | STATE.LOCAL_CONTEXT.isAnd ||
|
228 | (STATE.LOCAL_CONTEXT.isHtmlTag && !/^[\t ]*style[\t ]*:/.test(line.get())) ||
|
229 | STATE.LOCAL_CONTEXT.isInclude ||
|
230 | STATE.LOCAL_CONTEXT.isNestPropHead ||
|
231 | suf_regex_1.isPseudo(line.get()) ||
|
232 | suf_regex_1.isSelectorOperator(line.get()) ||
|
233 | suf_regex_1.isStar(line.get()) ||
|
234 | suf_regex_1.isBracketSelector(line.get()) ||
|
235 | suf_regex_1.isCssSelector(line.get()))
|
236 | );
|
237 | };
|
238 | SassFormatter.isProperty = function (STATE) {
|
239 | return (STATE.LOCAL_CONTEXT.isImport ||
|
240 | STATE.LOCAL_CONTEXT.isAtExtend ||
|
241 | STATE.LOCAL_CONTEXT.isVariable ||
|
242 | STATE.LOCAL_CONTEXT.isInterpolatedProp ||
|
243 | STATE.LOCAL_CONTEXT.isProp ||
|
244 | STATE.LOCAL_CONTEXT.isAtKeyframesPoint);
|
245 | };
|
246 |
|
247 | SassFormatter.addNewLine = function (STATE) {
|
248 | if (!STATE.CONTEXT.isFirstLine) {
|
249 | STATE.RESULT += STATE.LINE_ENDING;
|
250 | }
|
251 | else {
|
252 | STATE.CONTEXT.isFirstLine = false;
|
253 | }
|
254 | };
|
255 | return SassFormatter;
|
256 | }());
|
257 | exports.SassFormatter = SassFormatter;
|