Skip to content

Commit

Permalink
Add warning support
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed May 1, 2023
1 parent 21bd418 commit 3a07e14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ function jsonify (rawtext, opts) {
const results = []
const resultMap = {}
lines.forEach(function (line) {
const originalLine = line
let messageType = 'error'
if (line.endsWith(' (warning)')) {
line = line.slice(0, -10)
messageType = 'warning'
}

const re = /\s*([A-Za-z]:)?([^:]+):([^:]+):([^:]+): (.*?)( \((.*)\))?$/.exec(line)
if (!re) return opts.noisey ? console.error(line) : null
if (!re) return opts.noisey ? console.error(originalLine) : null
if (re[1] === undefined) re[1] = ''

const filePath = re[1] + re[2]
Expand All @@ -27,7 +34,8 @@ function jsonify (rawtext, opts) {
line: re[3],
column: re[4],
message: re[5].trim(),
ruleId: re[7]
ruleId: re[7],
type: messageType
})
})

Expand Down
4 changes: 4 additions & 0 deletions test/data-warning.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
standard: Use JavaScript Standard Style (https://standardjs.com)
standard: Some warnings are present which will be errors in the next version (https://standardjs.com)
/someplace/index.js:2:1: 'hello' is not defined. (no-undef)
/someplace/index.js:2:11: Expected property shorthand. (object-shorthand) (warning)
45 changes: 38 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ test('standard-json', function (t) {
line: '5',
column: '23',
message: 'Strings must use singlequote.',
ruleId: undefined
ruleId: undefined,
type: 'error'
}, {
line: '15',
column: '36',
message: 'Extra semicolon.',
ruleId: undefined
ruleId: undefined,
type: 'error'
}]
}]

Expand All @@ -35,12 +37,14 @@ test('standard-json verbose', function (t) {
line: '5',
column: '23',
message: 'Strings must use singlequote.',
ruleId: 'quotes'
ruleId: 'quotes',
type: 'error'
}, {
line: '15',
column: '36',
message: 'Extra semicolon.',
ruleId: 'semi'
ruleId: 'semi',
type: 'error'
}]
}]

Expand All @@ -58,12 +62,14 @@ test('standard-json verbose with Windows path', function (t) {
line: '5',
column: '23',
message: 'Strings must use singlequote.',
ruleId: 'quotes'
ruleId: 'quotes',
type: 'error'
}, {
line: '15',
column: '36',
message: 'Extra semicolon.',
ruleId: 'semi'
ruleId: 'semi',
type: 'error'
}]
}]

Expand All @@ -75,7 +81,32 @@ test('standard-json verbose with Windows path', function (t) {
test('standard-json parenthesis in message', function (t) {
t.plan(1)
const data = fs.readFileSync(path.join(__dirname, 'data-paren-in-msg.txt'), { encoding: 'utf8' })
const dataJson = [{ filePath: '/someplace/index.js', messages: [{ column: '18', line: '1', message: 'Missing \'()\' invoking a constructor.', ruleId: undefined }] }]
const dataJson = [{ filePath: '/someplace/index.js', messages: [{ column: '18', line: '1', message: 'Missing \'()\' invoking a constructor.', ruleId: undefined, type: 'error' }] }]

const output = jsonify(data)

t.deepEqual(output, dataJson, 'JSON formatted')
})

test('standard-json warning', function (t) {
t.plan(1)
const data = fs.readFileSync(path.join(__dirname, 'data-warning.txt'), { encoding: 'utf8' })
const dataJson = [{
filePath: '/someplace/index.js',
messages: [{
line: '2',
column: '1',
message: "'hello' is not defined.",
ruleId: 'no-undef',
type: 'error'
}, {
line: '2',
column: '11',
message: 'Expected property shorthand.',
ruleId: 'object-shorthand',
type: 'warning'
}]
}]

const output = jsonify(data)

Expand Down

0 comments on commit 3a07e14

Please sign in to comment.