Skip to content

Commit

Permalink
fix(trimComments): handle case special comment case
Browse files Browse the repository at this point in the history
  • Loading branch information
saadjutt01 committed May 21, 2021
1 parent f793eb3 commit d391a4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/utils/trimComments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ describe('trimComments', () => {
`)
).toEqual({ statement: 'some code;', commentStarted: false })

expect(
trimComments(`
/*/ some comment */ some code;
`)
).toEqual({ statement: 'some code;', commentStarted: false })

expect(
trimComments(`
some code;/*/ some comment */ some code;
`)
).toEqual({ statement: 'some code; some code;', commentStarted: false })

expect(
trimComments(`/* some comment */
/* some comment */ CODE_Keyword1 /* some comment */ CODE_Keyword2/* some comment */;/* some comment */
Expand Down
10 changes: 5 additions & 5 deletions src/utils/trimComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export const trimComments = (
let trimmed = trimEnd ? (statement || '').trimEnd() : (statement || '').trim()

if (commentStarted || trimmed.startsWith('/*')) {
const parts = trimmed.split('*/')
const parts = trimmed.startsWith('/*')
? trimmed.slice(2).split('*/')
: trimmed.split('*/')
if (parts.length === 2) {
return {
statement: (parts.pop() as string).trim(),
Expand All @@ -20,10 +22,8 @@ export const trimComments = (
}
} else if (trimmed.includes('/*')) {
const statementBeforeCommentStarts = trimmed.slice(0, trimmed.indexOf('/*'))
const remainingStatement = trimmed.slice(
trimmed.indexOf('*/') + 2,
trimmed.length
)
trimmed = trimmed.slice(trimmed.indexOf('/*') + 2)
const remainingStatement = trimmed.slice(trimmed.indexOf('*/') + 2)

const result = trimComments(remainingStatement, false, true)
const completeStatement = statementBeforeCommentStarts + result.statement
Expand Down

0 comments on commit d391a4e

Please sign in to comment.