🎉 initiate project *astro_rewrite*

This commit is contained in:
sindrekjelsrud 2023-07-19 21:31:30 +02:00
parent ffd4d5e86c
commit 2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions

View file

@ -0,0 +1,3 @@
import { SassTextLine } from '../sassTextLine';
import { FormattingState } from '../state';
export declare function FormatAtForwardOrAtUse(line: SassTextLine, STATE: FormattingState): string;

View file

@ -0,0 +1,22 @@
"use strict";
exports.__esModule = true;
exports.FormatAtForwardOrAtUse = void 0;
var format_convert_1 = require("./format.convert");
var utility_1 = require("../utility");
var logger_1 = require("../logger");
function FormatAtForwardOrAtUse(line, STATE) {
if (utility_1.convertLine(line, STATE)) {
var convertRes = format_convert_1.convertScssOrCss(line.get(), STATE);
line.set(convertRes.text);
}
line.set(line.get().trimStart());
logger_1.PushDebugInfo({
title: '@forward or @use',
lineNumber: STATE.currentLine,
oldLineText: STATE.lines[STATE.currentLine],
newLineText: line.get(),
debug: STATE.CONFIG.debug
});
return line.get();
}
exports.FormatAtForwardOrAtUse = FormatAtForwardOrAtUse;

View file

@ -0,0 +1,2 @@
import { FormattingState } from '../state';
export declare function FormatHandleBlockComment(text: string, STATE: FormattingState): string;

View file

@ -0,0 +1,11 @@
"use strict";
exports.__esModule = true;
exports.FormatHandleBlockComment = void 0;
var utility_1 = require("../utility");
function FormatHandleBlockComment(text, STATE) {
if (/^[\t ]*\/\*/.test(text)) {
return utility_1.replaceSpacesOrTabs(text, STATE);
}
return utility_1.replaceWithOffset(text, utility_1.getIndentationOffset(text, STATE.CONTEXT.blockCommentDistance + 1, STATE.CONFIG.tabSize).offset, STATE);
}
exports.FormatHandleBlockComment = FormatHandleBlockComment;

View file

@ -0,0 +1,6 @@
import { FormattingState } from '../state';
/** converts scss/css to sass. */
export declare function convertScssOrCss(text: string, STATE: FormattingState): {
lastSelector: string;
text: string;
};

View file

@ -0,0 +1,94 @@
"use strict";
exports.__esModule = true;
exports.convertScssOrCss = void 0;
var regex_1 = require("../regex/regex");
var logger_1 = require("../logger");
var utility_1 = require("../utility");
var format_property_1 = require("./format.property");
/** converts scss/css to sass. */
function convertScssOrCss(text, STATE) {
var isMultiple = regex_1.isMoreThanOneClassOrId(text);
var lastSelector = STATE.CONTEXT.convert.lastSelector;
// if NOT interpolated class, id or partial
if (!/[\t ]*[#.%]\{.*?}/.test(text)) {
if (lastSelector && new RegExp('^.*' + regex_1.escapeRegExp(lastSelector)).test(text)) {
/*istanbul ignore if */
if (STATE.CONFIG.debug)
logger_1.SetConvertData({ type: 'LAST SELECTOR', text: text });
return {
lastSelector: lastSelector,
text: utility_1.replaceWithOffset(removeInvalidChars(text.replaceAll(lastSelector, '&')).trimEnd(), STATE.CONFIG.tabSize, STATE)
};
}
else if (regex_1.isCssOneLiner(text)) {
/*istanbul ignore if */
if (STATE.CONFIG.debug)
logger_1.SetConvertData({ type: 'ONE LINER', text: text });
var split = text.split('{');
var properties = split[1].split(';');
// Set isProp to true so that it Sets the property space.
STATE.LOCAL_CONTEXT.isProp = true;
var selector = split[0].trim();
return {
lastSelector: selector,
text: selector.concat('\n', properties
.map(function (v) {
return utility_1.replaceWithOffset(format_property_1.setPropertyValueSpaces(STATE, removeInvalidChars(v)).trim(), STATE.CONFIG.tabSize, STATE);
})
.join('\n')).trimEnd()
};
}
else if (regex_1.isCssPseudo(text) && !isMultiple) {
/*istanbul ignore if */
if (STATE.CONFIG.debug)
logger_1.SetConvertData({ type: 'PSEUDO', text: text });
return {
lastSelector: lastSelector,
text: removeInvalidChars(text).trimEnd()
};
}
else if (regex_1.isCssSelector(text)) {
/*istanbul ignore if */
if (STATE.CONFIG.debug)
logger_1.SetConvertData({ type: 'SELECTOR', text: text });
lastSelector = removeInvalidChars(text).trimEnd();
return { text: lastSelector, lastSelector: lastSelector };
}
}
/*istanbul ignore if */
if (STATE.CONFIG.debug)
logger_1.SetConvertData({ type: 'DEFAULT', text: text });
return { text: removeInvalidChars(text).trimEnd(), lastSelector: lastSelector };
}
exports.convertScssOrCss = convertScssOrCss;
function removeInvalidChars(text) {
var newText = '';
var isInQuotes = false;
var isInComment = false;
var isInInterpolation = false;
var quoteChar = '';
for (var i = 0; i < text.length; i++) {
var char = text[i];
if (!isInQuotes && char === '/' && text[i + 1] === '/') {
isInComment = true;
}
else if (/['"]/.test(char)) {
if (!isInQuotes || char === quoteChar) {
isInQuotes = !isInQuotes;
if (isInQuotes) {
quoteChar = char;
}
}
}
else if (/#/.test(char) && /{/.test(text[i + 1])) {
isInInterpolation = true;
}
else if (isInInterpolation && /}/.test(text[i - 1])) {
isInInterpolation = false;
}
if (!/[;\{\}]/.test(char) || isInQuotes || isInComment || isInInterpolation) {
newText += char;
}
}
return newText;
}

View file

@ -0,0 +1,3 @@
import { SassTextLine } from '../sassTextLine';
import { FormattingState } from '../state';
export declare function FormatBlockHeader(line: SassTextLine, STATE: FormattingState): string;

View file

@ -0,0 +1,102 @@
"use strict";
exports.__esModule = true;
exports.FormatBlockHeader = void 0;
var logger_1 = require("../logger");
var regex_1 = require("../regex/regex");
var utility_1 = require("../utility");
var format_convert_1 = require("./format.convert");
function FormatBlockHeader(line, STATE) {
var replaceSpaceOrTabs = false;
var hasBeenConverted = false;
var edit = line.get();
// First Convert then set Offset.
if (STATE.CONFIG.convert &&
regex_1.isScssOrCss(line.get()) &&
!regex_1.isComment(line.get())) {
var convertRes = format_convert_1.convertScssOrCss(line.get(), STATE);
STATE.CONTEXT.convert.lastSelector = convertRes.lastSelector;
line.set(convertRes.text);
STATE.LOCAL_CONTEXT.indentation = utility_1.getIndentationOffset(line.get(), STATE.CONTEXT.indentation, STATE.CONFIG.tabSize);
hasBeenConverted = true;
}
// Set offset.
var offset = STATE.LOCAL_CONTEXT.isAdjacentSelector && STATE.CONTEXT.wasLastLineSelector
? STATE.CONTEXT.lastSelectorIndentation - STATE.LOCAL_CONTEXT.indentation.distance
: utility_1.getBlockHeaderOffset(STATE.LOCAL_CONTEXT.indentation.distance, STATE.CONFIG.tabSize, STATE.CONTEXT.indentation, STATE.LOCAL_CONTEXT.isReset);
if (STATE.LOCAL_CONTEXT.isElse && STATE.CONTEXT["if"].isIn) {
offset = (STATE.CONTEXT["if"].indentation - STATE.CONFIG.tabSize) - STATE.LOCAL_CONTEXT.indentation.distance;
}
else if (!STATE.LOCAL_CONTEXT.isIf) {
STATE.CONTEXT.keyframes.isIn =
STATE.LOCAL_CONTEXT.isAtKeyframes || STATE.LOCAL_CONTEXT.isAtKeyframesPoint;
}
STATE.CONTEXT.allowSpace = false;
if (!hasBeenConverted && STATE.LOCAL_CONTEXT.isClassOrIdSelector) {
STATE.CONTEXT.convert.lastSelector = '';
}
STATE.CONTEXT.convert.wasLastLineCss = hasBeenConverted;
if (STATE.CONTEXT.firstCommaHeader.exists) {
offset = STATE.CONTEXT.firstCommaHeader.distance - STATE.LOCAL_CONTEXT.indentation.distance;
}
if (line.get().trim().endsWith(',')) {
if (STATE.CONTEXT.firstCommaHeader.exists !== true) {
STATE.CONTEXT.firstCommaHeader.distance = STATE.LOCAL_CONTEXT.indentation.distance + offset;
}
STATE.CONTEXT.firstCommaHeader.exists = true;
}
else {
STATE.CONTEXT.firstCommaHeader.exists = false;
}
// Convert Spaces to tabs or vice versa depending on the config.
if (STATE.CONFIG.insertSpaces ? /\t/g.test(line.get()) : / /g.test(line.get())) {
line.set(utility_1.replaceSpacesOrTabs(line.get(), STATE));
replaceSpaceOrTabs = true;
}
// Set edit or just return the line text.
if (offset !== 0) {
edit = utility_1.replaceWithOffset(line.get(), offset, STATE).trimRight();
logger_1.PushDebugInfo({
title: 'BLOCK HEADER: MOVE',
lineNumber: STATE.currentLine,
oldLineText: STATE.lines[STATE.currentLine],
newLineText: edit,
debug: STATE.CONFIG.debug,
replaceSpaceOrTabs: replaceSpaceOrTabs,
offset: offset
});
}
else {
edit = line.get().trimRight();
logger_1.PushDebugInfo({
title: 'BLOCK HEADER: DEFAULT',
lineNumber: STATE.currentLine,
oldLineText: STATE.lines[STATE.currentLine],
newLineText: edit,
debug: STATE.CONFIG.debug,
replaceSpaceOrTabs: replaceSpaceOrTabs
});
}
STATE.CONTEXT.lastSelectorIndentation = Math.max(STATE.LOCAL_CONTEXT.indentation.distance + offset, 0);
if (STATE.LOCAL_CONTEXT.isReset) {
STATE.CONTEXT.indentation = Math.max(0, STATE.LOCAL_CONTEXT.indentation.distance + offset);
}
else {
STATE.CONTEXT.indentation = Math.max(0, STATE.LOCAL_CONTEXT.indentation.distance +
offset + // keep in mind that +offset can decrease the number.
STATE.CONFIG.tabSize);
}
if (STATE.LOCAL_CONTEXT.isAtKeyframes) {
STATE.CONTEXT.keyframes.indentation = STATE.CONTEXT.indentation;
}
if (STATE.LOCAL_CONTEXT.isIf) {
STATE.CONTEXT["if"].indentation = STATE.CONTEXT.indentation;
STATE.CONTEXT["if"].isIn = true;
}
else {
STATE.CONTEXT["if"].isIn = false;
}
STATE.CONTEXT.wasLastHeaderIncludeMixin = STATE.LOCAL_CONTEXT.isInclude;
STATE.CONTEXT.wasLastHeaderNestedProp = STATE.LOCAL_CONTEXT.isNestPropHead;
return edit;
}
exports.FormatBlockHeader = FormatBlockHeader;

View file

@ -0,0 +1,4 @@
import { SassTextLine } from '../sassTextLine';
import { FormattingState } from '../state';
export declare function FormatProperty(line: SassTextLine, STATE: FormattingState): string;
export declare function setPropertyValueSpaces(STATE: FormattingState, text: string): string;

View file

@ -0,0 +1,109 @@
"use strict";
exports.__esModule = true;
exports.setPropertyValueSpaces = exports.FormatProperty = void 0;
var logger_1 = require("../logger");
var regex_1 = require("../regex/regex");
var utility_1 = require("../utility");
var format_convert_1 = require("./format.convert");
function FormatProperty(line, STATE) {
var convert = false;
var replaceSpaceOrTabs = false;
var edit = line.get();
var isComment = regex_1.isComment(line.get());
line.set(setPropertyValueSpaces(STATE, line.get()));
if (utility_1.convertLine(line, STATE)) {
var convertRes = format_convert_1.convertScssOrCss(line.get(), STATE);
line.set(convertRes.text);
convert = true;
}
// Set Context Vars
STATE.CONTEXT.convert.wasLastLineCss = convert;
var move = STATE.LOCAL_CONTEXT.indentation.offset !== 0 && !isComment;
if (!move && canReplaceSpacesOrTabs(STATE, line.get())) {
line.set(utility_1.replaceSpacesOrTabs(line.get(), STATE).trimRight());
replaceSpaceOrTabs = true;
}
// Return
if (move) {
var offset = STATE.LOCAL_CONTEXT.indentation.offset;
var distance = STATE.LOCAL_CONTEXT.indentation.distance;
if (STATE.CONTEXT.wasLastHeaderIncludeMixin || STATE.CONTEXT.wasLastHeaderNestedProp) {
if (distance >= STATE.CONTEXT.indentation - STATE.CONFIG.tabSize) {
offset = utility_1.getBlockHeaderOffset(distance, STATE.CONFIG.tabSize, STATE.CONTEXT.indentation, false);
}
else {
offset = (STATE.CONTEXT.indentation - STATE.CONFIG.tabSize) - distance;
STATE.CONTEXT.wasLastHeaderIncludeMixin = false;
STATE.CONTEXT.wasLastHeaderNestedProp = false;
STATE.CONTEXT.indentation = STATE.CONTEXT.indentation - STATE.CONFIG.tabSize;
}
}
else if (STATE.LOCAL_CONTEXT.isVariable || STATE.LOCAL_CONTEXT.isImport) {
offset = utility_1.getBlockHeaderOffset(distance, STATE.CONFIG.tabSize, STATE.CONTEXT.indentation, false);
}
edit = utility_1.replaceWithOffset(line.get(), offset, STATE).trimRight();
logger_1.PushDebugInfo({
title: 'PROPERTY: MOVE',
lineNumber: STATE.currentLine,
oldLineText: STATE.lines[STATE.currentLine],
newLineText: edit,
debug: STATE.CONFIG.debug,
offset: offset,
originalOffset: STATE.LOCAL_CONTEXT.indentation.offset,
replaceSpaceOrTabs: replaceSpaceOrTabs
});
}
else {
edit = line.get().trimRight();
logger_1.PushDebugInfo({
title: 'PROPERTY: DEFAULT',
lineNumber: STATE.currentLine,
oldLineText: STATE.lines[STATE.currentLine],
newLineText: edit,
debug: STATE.CONFIG.debug,
replaceSpaceOrTabs: replaceSpaceOrTabs
});
}
if (STATE.CONTEXT.keyframes.isIn && STATE.LOCAL_CONTEXT.isAtKeyframesPoint) {
STATE.CONTEXT.indentation = Math.max(0, STATE.CONTEXT.indentation + STATE.CONFIG.tabSize);
}
return edit;
}
exports.FormatProperty = FormatProperty;
function canReplaceSpacesOrTabs(STATE, text) {
return STATE.CONFIG.insertSpaces
? /\t/g.test(text)
: new RegExp(' '.repeat(STATE.CONFIG.tabSize), 'g').test(text);
}
function setPropertyValueSpaces(STATE, text) {
if (text &&
(!STATE.LOCAL_CONTEXT.isHtmlTag &&
(STATE.LOCAL_CONTEXT.isProp || STATE.LOCAL_CONTEXT.isInterpolatedProp || STATE.LOCAL_CONTEXT.isVariable) &&
STATE.CONFIG.setPropertySpace)) {
var newPropValue = '';
var _a = text.split(/:(.*)/), propName = _a[0], propValue = _a[1];
var wasLastCharSpace = true;
for (var i = 0; i < propValue.length; i++) {
var char = propValue[i];
switch (char) {
case ' ':
if (!wasLastCharSpace) {
newPropValue += char;
wasLastCharSpace = true;
}
break;
case '.':
wasLastCharSpace = true;
newPropValue += char;
break;
default:
wasLastCharSpace = false;
newPropValue += char;
break;
}
}
return propName.trimEnd() + ":" + (propValue ? ' ' + newPropValue : '');
}
return text;
}
exports.setPropertyValueSpaces = setPropertyValueSpaces;