Skip to content

Commit afb599e

Browse files
authored
Merge pull request #56 from Berkmann18/dev
fix: fixes special characters
2 parents 3a28b1f + 56f23a1 commit afb599e

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const ops = (err) => ['+', '-', '*', '/', '>', '<', '~', '|', '&', '^'].includes
5757

5858
const extraBrackets = (err) => err.found === '}';
5959

60+
const specialChar = (err) => err.found === '"';
61+
6062
const runFixer = ({ verbose, lines, start, err }) => {
6163
/* eslint-disable security/detect-object-injection */
6264
let fixedData = [...lines];
@@ -84,6 +86,8 @@ const runFixer = ({ verbose, lines, start, err }) => {
8486
fixedData = fixer.fixOpConcat({ start, fixedData, verbose });
8587
} else if (extraBrackets(err)) {
8688
fixedData = fixer.fixExtraCurlyBrackets({ start, fixedData, verbose });
89+
} else if (specialChar(err)) {
90+
fixedData = fixer.fixSpecialChar({ start, fixedData, verbose });
8791
} else throw new Error(`Unsupported issue: ${err.message} (please open an issue at the repo)`);
8892
return fixedData;
8993
};

src/__tests__/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,23 @@ describe('multi rounds', () => {
411411
});
412412
});
413413
});
414+
415+
describe('special chars', () => {
416+
it('tab', () => {
417+
shouldHaveChanged('tab', {
418+
Test: '\t'
419+
});
420+
});
421+
422+
it('formatted tab', () => {
423+
shouldHaveChanged('tabs', {
424+
Test: '\t'
425+
});
426+
});
427+
428+
it('new line', () => {
429+
shouldHaveChanged('newLines', {
430+
Broken: '\n'
431+
});
432+
});
433+
});

src/fixer.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const fixOpConcat = ({ start, fixedData, verbose }) => {
156156
return fixedData;
157157
};
158158

159-
function fixExtraCurlyBrackets({ start, fixedData, verbose }) {
159+
const fixExtraCurlyBrackets = ({ start, fixedData, verbose }) => {
160160
if (verbose) psw(chalk.magenta('Extra curly brackets'));
161161

162162
const targetLine = start.line - 1;
@@ -175,7 +175,30 @@ function fixExtraCurlyBrackets({ start, fixedData, verbose }) {
175175

176176
fixedData[targetLine] = fixedLine;
177177
return fixedData;
178-
}
178+
};
179+
180+
const fixSpecialChar = ({ start, fixedData, verbose }) => {
181+
if (verbose) psw(chalk.magenta('Special character'));
182+
const targetLine = start.line - 1;
183+
const brokenLine = fixedData[targetLine];
184+
185+
let fixedLine = brokenLine
186+
.replace(/\f/g, '\\f')
187+
.replace(/\n/g, '\\n')
188+
.replace(/\r/g, '\\r')
189+
.replace(/\t/g, '\\t');
190+
191+
if (brokenLine.endsWith('"') && brokenLine[start.column] === undefined) {
192+
if (verbose) psw(chalk.magenta('New line'));
193+
const removedIndex = targetLine + 1;
194+
const continuation = fixedData[removedIndex];
195+
fixedLine = `${brokenLine}\\n${continuation}`;
196+
fixedData.splice(removedIndex, 1);
197+
}
198+
199+
fixedData[targetLine] = fixedLine;
200+
return fixedData;
201+
};
179202

180203
module.exports = {
181204
fixExtraChar,
@@ -186,5 +209,6 @@ module.exports = {
186209
fixCurlyBrackets,
187210
fixComment,
188211
fixOpConcat,
189-
fixExtraCurlyBrackets
212+
fixExtraCurlyBrackets,
213+
fixSpecialChar
190214
};

test/samples/newLines.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"Broken": "
2+
"}

test/samples/tab.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Test": " "}

test/samples/tabs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Test": " "
3+
}

0 commit comments

Comments
 (0)