Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit bf6ac21

Browse files
authored
fix: don't throw exception on invalid main attr (#109)
Callback with an error instead
1 parent c2f5a85 commit bf6ac21

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

read-json.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ function final (file, data, log, strict, cb) {
522522
function fillTypes (file, data, cb) {
523523
var index = data.main ? data.main : 'index.js'
524524

525+
if (typeof index !== 'string') {
526+
return cb(new TypeError('The "main" attribute must be of type string.'))
527+
}
528+
525529
// TODO exports is much more complicated than this in verbose format
526530
// We need to support for instance
527531

test/fixtures/badmainnonstring.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "invalid_main",
3+
"main": [
4+
"why is this a thing",
5+
"this can't work"
6+
]
7+
}
8+

test/main-non-string.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var tap = require('tap')
2+
var readJson = require('../')
3+
var path = require('path')
4+
var p = path.resolve(__dirname, 'fixtures/badmainnonstring.json')
5+
6+
tap.test('non-string main entries', function (t) {
7+
var logmsgs = []
8+
const warn = (...msg) => logmsgs.push(msg)
9+
readJson(p, warn, function (er, data) {
10+
t.comment(logmsgs.map(msg => 'Warning: ' + msg.join(' ')).join('\n'))
11+
t.match(er, new TypeError('The "main" attribute must be of type string.'))
12+
t.end()
13+
})
14+
})

0 commit comments

Comments
 (0)