Skip to content

Commit 14d001a

Browse files
authored
refactor: switch to const/let and enable eslint no-var rule (#729)
1 parent 37f36a2 commit 14d001a

12 files changed

Lines changed: 335 additions & 334 deletions

File tree

‎.eslintrc.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ overrides:
99
processor: 'markdown/markdown'
1010
rules:
1111
no-param-reassign: error
12+
no-var: error

‎lib/read.js‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* @private
1212
*/
1313

14-
var createError = require('http-errors')
15-
var getBody = require('raw-body')
16-
var iconv = require('iconv-lite')
17-
var onFinished = require('on-finished')
18-
var zlib = require('node:zlib')
19-
var hasBody = require('type-is').hasBody
20-
var { getCharset } = require('./utils')
14+
const createError = require('http-errors')
15+
const getBody = require('raw-body')
16+
const iconv = require('iconv-lite')
17+
const onFinished = require('on-finished')
18+
const zlib = require('node:zlib')
19+
const hasBody = require('type-is').hasBody
20+
const { getCharset } = require('./utils')
2121

2222
/**
2323
* Module exports.
@@ -63,7 +63,7 @@ function read (req, res, next, parse, debug, options) {
6363
return
6464
}
6565

66-
var encoding = null
66+
let encoding = null
6767
if (options?.skipCharset !== true) {
6868
encoding = getCharset(req) || options.defaultCharset
6969

@@ -78,12 +78,12 @@ function read (req, res, next, parse, debug, options) {
7878
}
7979
}
8080

81-
var length
82-
var opts = options
83-
var stream
81+
let length
82+
const opts = options
83+
let stream
8484

8585
// read options
86-
var verify = opts.verify
86+
const verify = opts.verify
8787

8888
try {
8989
// get the content stream
@@ -112,7 +112,7 @@ function read (req, res, next, parse, debug, options) {
112112
debug('read body')
113113
getBody(stream, opts, function (error, body) {
114114
if (error) {
115-
var _error
115+
let _error
116116

117117
if (error.type === 'encoding.unsupported') {
118118
// echo back charset
@@ -153,7 +153,7 @@ function read (req, res, next, parse, debug, options) {
153153
}
154154

155155
// parse
156-
var str = body
156+
let str = body
157157
try {
158158
debug('parse body')
159159
str = typeof body !== 'string' && encoding !== null
@@ -182,8 +182,8 @@ function read (req, res, next, parse, debug, options) {
182182
* @private
183183
*/
184184
function contentstream (req, debug, inflate) {
185-
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
186-
var length = req.headers['content-length']
185+
const encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
186+
const length = req.headers['content-length']
187187

188188
debug('content-encoding "%s"', encoding)
189189

@@ -199,7 +199,7 @@ function contentstream (req, debug, inflate) {
199199
return req
200200
}
201201

202-
var stream = createDecompressionStream(encoding, debug)
202+
const stream = createDecompressionStream(encoding, debug)
203203
req.pipe(stream)
204204
return stream
205205
}

‎lib/types/json.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
* @private
1313
*/
1414

15-
var debug = require('debug')('body-parser:json')
16-
var read = require('../read')
17-
var { normalizeOptions } = require('../utils')
15+
const debug = require('debug')('body-parser:json')
16+
const read = require('../read')
17+
const { normalizeOptions } = require('../utils')
1818

1919
/**
2020
* Module exports.
@@ -33,10 +33,10 @@ module.exports = json
3333
* %x0A / ; Line feed or New line
3434
* %x0D ) ; Carriage return
3535
*/
36-
var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
36+
const FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
3737

38-
var JSON_SYNTAX_CHAR = '#'
39-
var JSON_SYNTAX_REGEXP = /#+/g
38+
const JSON_SYNTAX_CHAR = '#'
39+
const JSON_SYNTAX_REGEXP = /#+/g
4040

4141
/**
4242
* Create a middleware to parse JSON bodies.
@@ -126,8 +126,8 @@ function createJsonParser (options) {
126126
* @private
127127
*/
128128
function createStrictSyntaxError (str, char) {
129-
var index = str.indexOf(char)
130-
var partial = ''
129+
const index = str.indexOf(char)
130+
let partial = ''
131131

132132
if (index !== -1) {
133133
partial = str.substring(0, index) + JSON_SYNTAX_CHAR.repeat(str.length - index)
@@ -153,7 +153,7 @@ function createStrictSyntaxError (str, char) {
153153
* @private
154154
*/
155155
function firstchar (str) {
156-
var match = FIRST_CHAR_REGEXP.exec(str)
156+
const match = FIRST_CHAR_REGEXP.exec(str)
157157

158158
return match
159159
? match[1]
@@ -169,10 +169,10 @@ function firstchar (str) {
169169
* @private
170170
*/
171171
function normalizeJsonSyntaxError (error, obj) {
172-
var keys = Object.getOwnPropertyNames(error)
172+
const keys = Object.getOwnPropertyNames(error)
173173

174-
for (var i = 0; i < keys.length; i++) {
175-
var key = keys[i]
174+
for (let i = 0; i < keys.length; i++) {
175+
const key = keys[i]
176176
if (key !== 'stack' && key !== 'message') {
177177
delete error[key]
178178
}

‎lib/types/raw.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* Module dependencies.
1111
*/
1212

13-
var debug = require('debug')('body-parser:raw')
14-
var read = require('../read')
15-
var { normalizeOptions, passthrough } = require('../utils')
13+
const debug = require('debug')('body-parser:raw')
14+
const read = require('../read')
15+
const { normalizeOptions, passthrough } = require('../utils')
1616

1717
/**
1818
* Module exports.

‎lib/types/text.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* Module dependencies.
1111
*/
1212

13-
var debug = require('debug')('body-parser:text')
14-
var read = require('../read')
15-
var { normalizeOptions, passthrough } = require('../utils')
13+
const debug = require('debug')('body-parser:text')
14+
const read = require('../read')
15+
const { normalizeOptions, passthrough } = require('../utils')
1616

1717
/**
1818
* Module exports.

‎lib/types/urlencoded.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
* @private
1313
*/
1414

15-
var createError = require('http-errors')
16-
var debug = require('debug')('body-parser:urlencoded')
17-
var read = require('../read')
18-
var qs = require('qs')
19-
var { normalizeOptions } = require('../utils')
15+
const createError = require('http-errors')
16+
const debug = require('debug')('body-parser:urlencoded')
17+
const read = require('../read')
18+
const qs = require('qs')
19+
const { normalizeOptions } = require('../utils')
2020

2121
/**
2222
* Module exports.
@@ -60,13 +60,13 @@ function urlencoded (options) {
6060
* @private
6161
*/
6262
function createQueryParser (options) {
63-
var extended = Boolean(options?.extended)
64-
var parameterLimit = options?.parameterLimit !== undefined
63+
const extended = Boolean(options?.extended)
64+
let parameterLimit = options?.parameterLimit !== undefined
6565
? options?.parameterLimit
6666
: 1000
67-
var charsetSentinel = options?.charsetSentinel
68-
var interpretNumericEntities = options?.interpretNumericEntities
69-
var depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0
67+
const charsetSentinel = options?.charsetSentinel
68+
const interpretNumericEntities = options?.interpretNumericEntities
69+
const depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0
7070

7171
if (isNaN(parameterLimit) || parameterLimit < 1) {
7272
throw new TypeError('option parameterLimit must be a positive number')
@@ -83,7 +83,7 @@ function createQueryParser (options) {
8383
return function parse (body, encoding) {
8484
if (!body.length) return {}
8585

86-
var paramCount = parameterCount(body, parameterLimit)
86+
const paramCount = parameterCount(body, parameterLimit)
8787

8888
if (paramCount === undefined) {
8989
debug('too many parameters')
@@ -92,7 +92,7 @@ function createQueryParser (options) {
9292
})
9393
}
9494

95-
var arrayLimit = extended ? Math.max(100, paramCount) : paramCount
95+
const arrayLimit = extended ? Math.max(100, paramCount) : paramCount
9696

9797
debug('parse ' + (extended ? 'extended ' : '') + 'urlencoding')
9898
try {

‎lib/utils.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* Module dependencies.
55
*/
66

7-
var bytes = require('bytes')
8-
var contentType = require('content-type')
9-
var typeis = require('type-is')
7+
const bytes = require('bytes')
8+
const contentType = require('content-type')
9+
const typeis = require('type-is')
1010

1111
/**
1212
* Module exports.
@@ -25,7 +25,7 @@ module.exports = {
2525
* @private
2626
*/
2727
function getCharset (req) {
28-
var header = req.headers['content-type']
28+
const header = req.headers['content-type']
2929
if (!header) return undefined
3030
return contentType.parse(header).parameters.charset?.toLowerCase()
3131
}
@@ -57,13 +57,13 @@ function normalizeOptions (options, defaultType) {
5757
throw new TypeError('defaultType must be provided')
5858
}
5959

60-
var inflate = options?.inflate !== false
61-
var limit = typeof options?.limit === 'undefined' || options?.limit === null
60+
const inflate = options?.inflate !== false
61+
const limit = typeof options?.limit === 'undefined' || options?.limit === null
6262
? 102400 // 100kb default
6363
: bytes.parse(options.limit)
64-
var type = options?.type || defaultType
65-
var verify = options?.verify || false
66-
var defaultCharset = options?.defaultCharset || 'utf-8'
64+
const type = options?.type || defaultType
65+
const verify = options?.verify || false
66+
const defaultCharset = options?.defaultCharset || 'utf-8'
6767

6868
if (limit === null) {
6969
throw new TypeError(`option limit "${String(options.limit)}" is invalid`)
@@ -74,7 +74,7 @@ function normalizeOptions (options, defaultType) {
7474
}
7575

7676
// create the appropriate type checking function
77-
var shouldParse = typeof type !== 'function'
77+
const shouldParse = typeof type !== 'function'
7878
? typeChecker(type)
7979
: type
8080

‎test/body-parser.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
var assert = require('node:assert')
3+
const assert = require('node:assert')
44

5-
var bodyParser = require('..')
5+
const bodyParser = require('..')
66

77
describe('bodyParser()', function () {
88
it('should throw an error', function () {

0 commit comments

Comments
 (0)