Skip to content

Commit 4e804e5

Browse files
committed
fix: fixes special characters
1 parent 69ea4c6 commit 4e804e5

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-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/fixer.js

Lines changed: 28 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,31 @@ 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+
.replace(/\\/g, '\\');
191+
192+
if (brokenLine.endsWith('"') && brokenLine[start.column] === undefined) {
193+
if (verbose) psw(chalk.magenta('New line'));
194+
const removedIndex = targetLine + 1;
195+
const continuation = fixedData[removedIndex];
196+
fixedLine = `${brokenLine}\\n${continuation}`;
197+
fixedData.splice(removedIndex, 1);
198+
}
199+
200+
fixedData[targetLine] = fixedLine;
201+
return fixedData;
202+
};
179203

180204
module.exports = {
181205
fixExtraChar,
@@ -186,5 +210,6 @@ module.exports = {
186210
fixCurlyBrackets,
187211
fixComment,
188212
fixOpConcat,
189-
fixExtraCurlyBrackets
213+
fixExtraCurlyBrackets,
214+
fixSpecialChar
190215
};

0 commit comments

Comments
 (0)