Skip to content

Commit 9aa9a50

Browse files
committed
Add terminating semicolons to harden syntax
Semicolons were omitted where optional with a trailing line break. This can cause issues when e.g. minimising the script. This change consequently adds trailing semicolons to terminate statements and hence hardens the wider use of the script. Signed-off-by: MichaIng <[email protected]>
1 parent 00af1e6 commit 9aa9a50

File tree

2 files changed

+84
-84
lines changed

2 files changed

+84
-84
lines changed

src/angular-sprintf.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/* global angular, sprintf, vsprintf */
22

33
!function() {
4-
'use strict'
4+
'use strict';
55

66
angular.
77
module('sprintf', []).
88
filter('sprintf', function() {
99
return function() {
10-
return sprintf.apply(null, arguments)
10+
return sprintf.apply(null, arguments);
1111
}
1212
}).
1313
filter('fmt', ['$filter', function($filter) {
14-
return $filter('sprintf')
14+
return $filter('sprintf');
1515
}]).
1616
filter('vsprintf', function() {
1717
return function(format, argv) {
18-
return vsprintf(format, argv)
18+
return vsprintf(format, argv);
1919
}
2020
}).
2121
filter('vfmt', ['$filter', function($filter) {
22-
return $filter('vsprintf')
22+
return $filter('vsprintf');
2323
}])
2424
}(); // eslint-disable-line

src/sprintf.js

+79-79
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global window, exports, define */
22

33
!function() {
4-
'use strict'
4+
'use strict';
55

66
var re = {
77
not_string: /[^s]/,
@@ -23,165 +23,165 @@
2323

2424
function sprintf(key) {
2525
// `arguments` is not an array, but should be fine for this call
26-
return sprintf_format(sprintf_parse(key), arguments)
26+
return sprintf_format(sprintf_parse(key), arguments);
2727
}
2828

2929
function vsprintf(fmt, argv) {
30-
return sprintf.apply(null, [fmt].concat(argv || []))
30+
return sprintf.apply(null, [fmt].concat(argv || []));
3131
}
3232

3333
function sprintf_format(parse_tree, argv) {
34-
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
34+
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign;
3535
for (i = 0; i < tree_length; i++) {
3636
if (typeof parse_tree[i] === 'string') {
37-
output += parse_tree[i]
37+
output += parse_tree[i];
3838
}
3939
else if (typeof parse_tree[i] === 'object') {
40-
ph = parse_tree[i] // convenience purposes only
40+
ph = parse_tree[i]; // convenience purposes only
4141
if (ph.keys) { // keyword argument
42-
arg = argv[cursor]
42+
arg = argv[cursor];
4343
for (k = 0; k < ph.keys.length; k++) {
4444
if (arg == undefined) {
45-
throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
45+
throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]));
4646
}
47-
arg = arg[ph.keys[k]]
47+
arg = arg[ph.keys[k]];
4848
}
4949
}
5050
else if (ph.param_no) { // positional argument (explicit)
51-
arg = argv[ph.param_no]
51+
arg = argv[ph.param_no];
5252
}
5353
else { // positional argument (implicit)
54-
arg = argv[cursor++]
54+
arg = argv[cursor++];
5555
}
5656

5757
if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
58-
arg = arg()
58+
arg = arg();
5959
}
6060

6161
if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
62-
throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
62+
throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg));
6363
}
6464

6565
if (re.number.test(ph.type)) {
66-
is_positive = arg >= 0
66+
is_positive = arg >= 0;
6767
}
6868

6969
switch (ph.type) {
7070
case 'b':
71-
arg = parseInt(arg, 10).toString(2)
72-
break
71+
arg = parseInt(arg, 10).toString(2);
72+
break;
7373
case 'c':
74-
arg = String.fromCharCode(parseInt(arg, 10))
75-
break
74+
arg = String.fromCharCode(parseInt(arg, 10));
75+
break;
7676
case 'd':
7777
case 'i':
78-
arg = parseInt(arg, 10)
79-
break
78+
arg = parseInt(arg, 10);
79+
break;
8080
case 'j':
81-
arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
82-
break
81+
arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0);
82+
break;
8383
case 'e':
84-
arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
85-
break
84+
arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential();
85+
break;
8686
case 'f':
87-
arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
88-
break
87+
arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg);
88+
break;
8989
case 'g':
90-
arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
91-
break
90+
arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg);
91+
break;
9292
case 'o':
93-
arg = (parseInt(arg, 10) >>> 0).toString(8)
94-
break
93+
arg = (parseInt(arg, 10) >>> 0).toString(8);
94+
break;
9595
case 's':
96-
arg = String(arg)
97-
arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
98-
break
96+
arg = String(arg);
97+
arg = (ph.precision ? arg.substring(0, ph.precision) : arg);
98+
break;
9999
case 't':
100-
arg = String(!!arg)
101-
arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
102-
break
100+
arg = String(!!arg);
101+
arg = (ph.precision ? arg.substring(0, ph.precision) : arg);
102+
break;
103103
case 'T':
104-
arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
105-
arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
106-
break
104+
arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase();
105+
arg = (ph.precision ? arg.substring(0, ph.precision) : arg);
106+
break;
107107
case 'u':
108-
arg = parseInt(arg, 10) >>> 0
109-
break
108+
arg = parseInt(arg, 10) >>> 0;
109+
break;
110110
case 'v':
111-
arg = arg.valueOf()
112-
arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
113-
break
111+
arg = arg.valueOf();
112+
arg = (ph.precision ? arg.substring(0, ph.precision) : arg);
113+
break;
114114
case 'x':
115-
arg = (parseInt(arg, 10) >>> 0).toString(16)
116-
break
115+
arg = (parseInt(arg, 10) >>> 0).toString(16);
116+
break;
117117
case 'X':
118-
arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
119-
break
118+
arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase();
119+
break;
120120
}
121121
if (re.json.test(ph.type)) {
122-
output += arg
122+
output += arg;
123123
}
124124
else {
125125
if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
126-
sign = is_positive ? '+' : '-'
127-
arg = arg.toString().replace(re.sign, '')
126+
sign = is_positive ? '+' : '-';
127+
arg = arg.toString().replace(re.sign, '');
128128
}
129129
else {
130-
sign = ''
130+
sign = '';
131131
}
132-
pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
133-
pad_length = ph.width - (sign + arg).length
134-
pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
135-
output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
132+
pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' ';
133+
pad_length = ph.width - (sign + arg).length;
134+
pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : '';
135+
output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg);
136136
}
137137
}
138138
}
139-
return output
139+
return output;
140140
}
141141

142-
var sprintf_cache = Object.create(null)
142+
var sprintf_cache = Object.create(null);
143143

144144
function sprintf_parse(fmt) {
145145
if (sprintf_cache[fmt]) {
146-
return sprintf_cache[fmt]
146+
return sprintf_cache[fmt];
147147
}
148148

149-
var _fmt = fmt, match, parse_tree = [], arg_names = 0
149+
var _fmt = fmt, match, parse_tree = [], arg_names = 0;
150150
while (_fmt) {
151151
if ((match = re.text.exec(_fmt)) !== null) {
152-
parse_tree.push(match[0])
152+
parse_tree.push(match[0]);
153153
}
154154
else if ((match = re.modulo.exec(_fmt)) !== null) {
155-
parse_tree.push('%')
155+
parse_tree.push('%');
156156
}
157157
else if ((match = re.placeholder.exec(_fmt)) !== null) {
158158
if (match[2]) {
159-
arg_names |= 1
160-
var field_list = [], replacement_field = match[2], field_match = []
159+
arg_names |= 1;
160+
var field_list = [], replacement_field = match[2], field_match = [];
161161
if ((field_match = re.key.exec(replacement_field)) !== null) {
162-
field_list.push(field_match[1])
162+
field_list.push(field_match[1]);
163163
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
164164
if ((field_match = re.key_access.exec(replacement_field)) !== null) {
165-
field_list.push(field_match[1])
165+
field_list.push(field_match[1]);
166166
}
167167
else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
168-
field_list.push(field_match[1])
168+
field_list.push(field_match[1]);
169169
}
170170
else {
171-
throw new SyntaxError('[sprintf] failed to parse named argument key')
171+
throw new SyntaxError('[sprintf] failed to parse named argument key');
172172
}
173173
}
174174
}
175175
else {
176-
throw new SyntaxError('[sprintf] failed to parse named argument key')
176+
throw new SyntaxError('[sprintf] failed to parse named argument key');
177177
}
178-
match[2] = field_list
178+
match[2] = field_list;
179179
}
180180
else {
181-
arg_names |= 2
181+
arg_names |= 2;
182182
}
183183
if (arg_names === 3) {
184-
throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
184+
throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported');
185185
}
186186

187187
parse_tree.push(
@@ -199,24 +199,24 @@
199199
)
200200
}
201201
else {
202-
throw new SyntaxError('[sprintf] unexpected placeholder')
202+
throw new SyntaxError('[sprintf] unexpected placeholder');
203203
}
204-
_fmt = _fmt.substring(match[0].length)
204+
_fmt = _fmt.substring(match[0].length);
205205
}
206-
return sprintf_cache[fmt] = parse_tree
206+
return sprintf_cache[fmt] = parse_tree;
207207
}
208208

209209
/**
210210
* export to either browser or node.js
211211
*/
212212
/* eslint-disable quote-props */
213213
if (typeof exports !== 'undefined') {
214-
exports['sprintf'] = sprintf
215-
exports['vsprintf'] = vsprintf
214+
exports['sprintf'] = sprintf;
215+
exports['vsprintf'] = vsprintf;
216216
}
217217
if (typeof window !== 'undefined') {
218-
window['sprintf'] = sprintf
219-
window['vsprintf'] = vsprintf
218+
window['sprintf'] = sprintf;
219+
window['vsprintf'] = vsprintf;
220220

221221
if (typeof define === 'function' && define['amd']) {
222222
define(function() {

0 commit comments

Comments
 (0)