Refactor: Remove normalizeNewlines function and update tests to preserve newline types (#5697)

* refactor: remove `normalizeNewlines` function and update tests to preserve newline types
This commit is contained in:
Pragadesh-45
2025-10-07 18:43:19 +05:30
committed by GitHub
parent 608a9d1954
commit cf17539a47
3 changed files with 6 additions and 15 deletions

View File

@@ -153,7 +153,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
},
multilinetextblock(_1, content, _2) {
return content.ast
.split('\n')
.split(/\r\n|\r|\n/)
.map((line) => line.slice(indentLevel)) // Remove 4-space indentation
.join('\n')
.trim();

View File

@@ -7,22 +7,14 @@ const safeParseJson = (json) => {
}
};
const normalizeNewlines = (str) => {
if (!str || typeof str !== 'string') {
return str || '';
}
// "\r\n" is windows, "\r" is old mac, "\n" is linux
return str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
};
const indentString = (str) => {
if (!str || !str.length) {
return str || '';
}
return normalizeNewlines(str)
.split('\n')
return str
.split(/\r\n|\r|\n/)
.map((line) => ' ' + line)
.join('\n');
};
@@ -32,8 +24,8 @@ const outdentString = (str) => {
return str || '';
}
return normalizeNewlines(str)
.split('\n')
return str
.split(/\r\n|\r|\n/)
.map((line) => line.replace(/^ /, ''))
.join('\n');
};
@@ -56,7 +48,6 @@ const getValueString = (value) => {
module.exports = {
safeParseJson,
normalizeNewlines,
indentString,
outdentString,
getValueString

View File

@@ -10,7 +10,7 @@ describe('getValueString', () => {
});
it('normalizes different newline types', () => {
expect(getValueString('line1\r\nline2\rline3\nline4')).toBe("'''\n line1\n line2\n line3\n line4\n'''");
expect(getValueString('line1\r\nline2\rline3\nline4')).toBe('\'\'\'\n line1\n line2\n line3\n line4\n\'\'\'');
});
it('returns empty string for empty/null/undefined', () => {