Skip to content

Commit 155e563

Browse files
authored
Merge pull request #174 from erunion/feat/js-consistency
feat: updating javascript targets to use `const` instead of `var`
2 parents e631159 + cd034d0 commit 155e563

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+247
-250
lines changed

src/targets/javascript/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function (source, options) {
4444
break
4545

4646
case 'multipart/form-data':
47-
code.push('var form = new FormData();')
47+
code.push('const form = new FormData();')
4848

4949
source.postData.params.forEach(function (param) {
5050
code.push(

src/targets/javascript/jquery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function (source, options) {
3838
break
3939

4040
case 'multipart/form-data':
41-
code.push('var form = new FormData();')
41+
code.push('const form = new FormData();')
4242

4343
source.postData.params.forEach(function (param) {
4444
code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
@@ -62,7 +62,7 @@ module.exports = function (source, options) {
6262
}
6363
}
6464

65-
code.push('var settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form'))
65+
code.push('const settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form') + ';')
6666
.blank()
6767
.push('$.ajax(settings).done(function (response) {')
6868
.push(1, 'console.log(response);')

src/targets/javascript/xhr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ module.exports = function (source, options) {
2222

2323
switch (source.postData.mimeType) {
2424
case 'application/json':
25-
code.push('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
25+
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
2626
.push(null)
2727
break
2828

2929
case 'multipart/form-data':
30-
code.push('var data = new FormData();')
30+
code.push('const data = new FormData();')
3131

3232
source.postData.params.forEach(function (param) {
3333
code.push('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
@@ -42,11 +42,11 @@ module.exports = function (source, options) {
4242
break
4343

4444
default:
45-
code.push('var data = %s;', JSON.stringify(source.postData.text || null))
45+
code.push('const data = %s;', JSON.stringify(source.postData.text || null))
4646
.blank()
4747
}
4848

49-
code.push('var xhr = new XMLHttpRequest();')
49+
code.push('const xhr = new XMLHttpRequest();')
5050

5151
if (opts.cors) {
5252
code.push('xhr.withCredentials = true;')

src/targets/node/native.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ module.exports = function (source, options) {
2828
headers: source.allHeaders
2929
}
3030

31-
code.push('var http = require("%s");', source.uriObj.protocol.replace(':', ''))
31+
code.push('const http = require("%s");', source.uriObj.protocol.replace(':', ''))
3232

3333
code.blank()
34-
.push('var options = %s;', JSON.stringify(reqOpts, null, opts.indent))
34+
.push('const options = %s;', JSON.stringify(reqOpts, null, opts.indent))
3535
.blank()
36-
.push('var req = http.request(options, function (res) {')
37-
.push(1, 'var chunks = [];')
36+
.push('const req = http.request(options, function (res) {')
37+
.push(1, 'const chunks = [];')
3838
.blank()
3939
.push(1, 'res.on("data", function (chunk) {')
4040
.push(2, 'chunks.push(chunk);')
4141
.push(1, '});')
4242
.blank()
4343
.push(1, 'res.on("end", function () {')
44-
.push(2, 'var body = Buffer.concat(chunks);')
44+
.push(2, 'const body = Buffer.concat(chunks);')
4545
.push(2, 'console.log(body.toString());')
4646
.push(1, '});')
4747
.push('});')
@@ -50,7 +50,7 @@ module.exports = function (source, options) {
5050
switch (source.postData.mimeType) {
5151
case 'application/x-www-form-urlencoded':
5252
if (source.postData.paramsObj) {
53-
code.unshift('var qs = require("querystring");')
53+
code.unshift('const qs = require("querystring");')
5454
code.push('req.write(qs.stringify(%s));', stringifyObject(source.postData.paramsObj, {
5555
indent: ' ',
5656
inlineCharacterLimit: 80

src/targets/node/request.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (source, options) {
2222
var includeFS = false
2323
var code = new CodeBuilder(opts.indent)
2424

25-
code.push('var request = require("request");')
25+
code.push("const request = require('request');")
2626
.blank()
2727

2828
var reqOpts = {
@@ -90,21 +90,21 @@ module.exports = function (source, options) {
9090
if (source.cookies.length) {
9191
reqOpts.jar = 'JAR'
9292

93-
code.push('var jar = request.jar();')
93+
code.push('const jar = request.jar();')
9494

9595
var url = source.url
9696

9797
source.cookies.forEach(function (cookie) {
98-
code.push('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)
98+
code.push("jar.setCookie(request.cookie('%s=%s'), '%s');", encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)
9999
})
100100
code.blank()
101101
}
102102

103103
if (includeFS) {
104-
code.unshift('var fs = require("fs");')
104+
code.unshift("const fs = require('fs');")
105105
}
106106

107-
code.push('var options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
107+
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
108108
.blank()
109109

110110
code.push(util.format('request(options, %s', 'function (error, response, body) {'))

src/targets/node/unirest.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ module.exports = function (source, options) {
2020
var includeFS = false
2121
var code = new CodeBuilder(opts.indent)
2222

23-
code.push('var unirest = require("unirest");')
23+
code.push('const unirest = require("unirest");')
2424
.blank()
25-
.push('var req = unirest("%s", "%s");', source.method, source.url)
25+
.push('const req = unirest("%s", "%s");', source.method, source.url)
2626
.blank()
2727

2828
if (source.cookies.length) {
29-
code.push('var CookieJar = unirest.jar();')
29+
code.push('const CookieJar = unirest.jar();')
3030

3131
source.cookies.forEach(function (cookie) {
3232
code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)
3333
})
3434

3535
code.push('req.jar(CookieJar);')
36-
.blank()
36+
.blank()
3737
}
3838

3939
if (Object.keys(source.queryObj).length) {
@@ -50,13 +50,15 @@ module.exports = function (source, options) {
5050
case 'application/x-www-form-urlencoded':
5151
if (source.postData.paramsObj) {
5252
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
53+
.blank()
5354
}
5455
break
5556

5657
case 'application/json':
5758
if (source.postData.jsonObj) {
5859
code.push('req.type("json");')
5960
.push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
61+
.blank()
6062
}
6163
break
6264

@@ -84,20 +86,21 @@ module.exports = function (source, options) {
8486
})
8587

8688
code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))
89+
.blank()
8790
break
8891

8992
default:
9093
if (source.postData.text) {
91-
code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
94+
code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
95+
.blank()
9296
}
9397
}
9498

9599
if (includeFS) {
96-
code.unshift('var fs = require("fs");')
100+
code.unshift('const fs = require("fs");')
97101
}
98102

99-
code.blank()
100-
.push('req.end(function (res) {')
103+
code.push('req.end(function (res) {')
101104
.push(1, 'if (res.error) throw new Error(res.error);')
102105
.blank()
103106
.push(1, 'console.log(res.body);')

test/fixtures/output/javascript/fetch/multipart-data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var form = new FormData();
1+
const form = new FormData();
22
form.append("foo", "Hello World");
33

44
fetch("http://mockbin.com/har", {

test/fixtures/output/javascript/fetch/multipart-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var form = new FormData();
1+
const form = new FormData();
22
form.append("foo", "test/fixtures/files/hello.txt");
33

44
fetch("http://mockbin.com/har", {

test/fixtures/output/javascript/fetch/multipart-form-data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var form = new FormData();
1+
const form = new FormData();
22
form.append("foo", "bar");
33

44
fetch("http://mockbin.com/har", {

test/fixtures/output/javascript/jquery/application-form-encoded.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var settings = {
1+
const settings = {
22
"async": true,
33
"crossDomain": true,
44
"url": "http://mockbin.com/har",
@@ -10,7 +10,7 @@ var settings = {
1010
"foo": "bar",
1111
"hello": "world"
1212
}
13-
}
13+
};
1414

1515
$.ajax(settings).done(function (response) {
1616
console.log(response);

0 commit comments

Comments
 (0)