Skip to content

Commit

Permalink
feat: normalize comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzilong committed Jul 25, 2021
1 parent 6ec6f2a commit 62a5568
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ yarn add @biu/jsonlint
# API

```js
const { lint } = require( '@biu/jsonlint' )
const lint = require( '@biu/jsonlint' )

lint( string, options )
```
Expand All @@ -42,6 +42,16 @@ Default: `'pretty'`

Values: `'pretty' | 'json'`

If you use `json` reporter, the return value looks like:

```js
{
source: '', // source code
errors: [], // with keys: `{ line, column, message, severity }`
comments: [], // with keys: `{ start: { line, column }, end: { line, column } }`
}
```

### options.silent

Type: `Boolean`
Expand Down
17 changes: 16 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function lint( text = '', options = {} ) {
const result = {
source: text,
errors: normalizeErrors( errors, allowComments ? [] : comments ),
comments: comments,
comments: normalizeComments( comments ),
}

if ( reporter === 'pretty' ) {
Expand Down Expand Up @@ -73,6 +73,21 @@ function normalizeErrors( errors = [], comments = [] ) {
)
}

function normalizeComments( comments = [] ) {
return comments.map( comment => {
return {
start: {
line: comment.start.line + 1,
column: comment.start.character + 1,
},
end: {
line: comment.end.line + 1,
column: comment.end.character + 1,
},
}
} )
}

function prettyReport( text, { errors = [] } = {} ) {
if ( errors.length === 0 ) {
return ''
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ test( 'lint - reporter json', () => {
reporter: 'json',
allowComments: true
} )

console.log( result.comments )

expect( typeof result ).toBe( 'object' )
expect( result.source ).toBe( string )
Expand Down

0 comments on commit 62a5568

Please sign in to comment.