Skip to content

Commit 973030a

Browse files
authored
Merge pull request #48 from GiraffeKey/feat/extra-brackets
feat(extra-brackets): fix extra brackets
2 parents c4ad159 + f93268d commit 973030a

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const comment = (err) => err.found === '/';
5555

5656
const ops = (err) => ['+', '-', '*', '/', '>', '<', '~', '|', '&', '^'].includes(err.found);
5757

58+
const extraBrackets = (err) => err.found === '}';
59+
5860
const runFixer = ({ verbose, lines, start, err }) => {
5961
/* eslint-disable security/detect-object-injection */
6062
let fixedData = [...lines];
@@ -80,6 +82,8 @@ const runFixer = ({ verbose, lines, start, err }) => {
8082
fixedData = fixer.fixComment({ start, fixedData, verbose });
8183
} else if (ops(err)) {
8284
fixedData = fixer.fixOpConcat({ start, fixedData, verbose });
85+
} else if (extraBrackets(err)) {
86+
fixedData = fixer.fixExtraCurlyBrackets({ start, fixedData, verbose });
8387
} else throw new Error(`Unsupported issue: ${err.message} (please open an issue at the repo)`);
8488
return fixedData;
8589
};

src/__tests__/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ describe('fix wrong brackets', () => {
289289
{ verbose: true }
290290
);
291291
});
292+
293+
it('extra brackets', () => {
294+
shouldHaveChanged('extraBrackets', {
295+
error: 'extra brackets'
296+
});
297+
});
292298
});
293299

294300
describe('comments', () => {

src/fixer.js

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

159+
function fixExtraCurlyBrackets({ start, fixedData, verbose }) {
160+
if (verbose) psw(chalk.magenta('Extra curly brackets'));
161+
162+
const targetLine = start.line - 1;
163+
const fullData = fixedData.join('\n');
164+
let fixedLine = removeLinebreak(fixedData[targetLine]);
165+
166+
const data = fullData.split('');
167+
const openingCount = data.filter((c) => c === '{').length;
168+
const closingCount = data.filter((c) => c === '}').length;
169+
const bracketDiff = closingCount - openingCount;
170+
171+
for (let i = 0; i < bracketDiff; i++) {
172+
const index = fixedLine.lastIndexOf('}');
173+
fixedLine = fixedLine.slice(0, index) + fixedLine.slice(index + 1);
174+
}
175+
176+
fixedData[targetLine] = fixedLine;
177+
return fixedData;
178+
}
179+
159180
module.exports = {
160181
fixExtraChar,
161182
fixSingleQuotes,
@@ -164,5 +185,6 @@ module.exports = {
164185
fixSquareBrackets,
165186
fixCurlyBrackets,
166187
fixComment,
167-
fixOpConcat
188+
fixOpConcat,
189+
fixExtraCurlyBrackets
168190
};

test/samples/extraBrackets.json

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

0 commit comments

Comments
 (0)