Skip to content

Commit 397cb62

Browse files
committed
[eslint] additional cleanup
1 parent aaa9d1f commit 397cb62

File tree

6 files changed

+132
-141
lines changed

6 files changed

+132
-141
lines changed

.eslintrc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,18 @@
55

66
"rules": {
77
"array-bracket-newline": 0,
8-
"array-callback-return": 1,
9-
"brace-style": 1,
108
"complexity": 0,
11-
"consistent-return": 1,
12-
"curly": 1,
139
"eqeqeq": 1,
14-
"func-style": 1,
10+
"func-style": [2, "declaration"],
1511
"max-depth": 0,
16-
"max-lines-per-function": 1,
12+
"max-lines-per-function": 0,
1713
"max-statements": 0,
1814
"multiline-comment-style": 0,
19-
"no-else-return": 1,
20-
"no-lonely-if": 1,
2115
"no-negated-condition": 1,
2216
"no-param-reassign": 1,
17+
"no-lonely-if": 1,
2318
"no-shadow": 1,
2419
"no-template-curly-in-string": 0,
25-
"no-use-before-define": 1,
26-
"no-useless-escape": 1,
27-
"nonblock-statement-body-position": 1,
28-
"prefer-regex-literals": 1,
29-
"quotes": 1,
30-
"wrap-regex": 1,
3120
},
3221

3322
"overrides": [

index.js

Lines changed: 111 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ exports.quote = function (xs) {
44
return xs.map(function (s) {
55
if (s && typeof s === 'object') {
66
return s.op.replace(/(.)/g, '\\$1');
7-
}
8-
else if (/["\s]/.test(s) && !/'/.test(s)) {
7+
} else if ((/["\s]/).test(s) && !(/'/).test(s)) {
98
return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
10-
}
11-
else if (/["'\s]/.test(s)) {
9+
} else if ((/["'\s]/).test(s)) {
1210
return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
1311
}
14-
else {
15-
return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@\[\\\]^`{|}])/g, '$1\\$2');
16-
}
12+
return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, '$1\\$2');
1713
}).join(' ');
1814
};
1915

@@ -32,50 +28,56 @@ for (var i = 0; i < 4; i++) {
3228
TOKEN += (Math.pow(16, 8) * Math.random()).toString(16);
3329
}
3430

35-
exports.parse = function (s, env, opts) {
36-
var mapped = parse(s, env, opts);
37-
if (typeof env !== 'function') return mapped;
38-
return mapped.reduce(function (acc, s) {
39-
if (typeof s === 'object') return acc.concat(s);
40-
var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
41-
if (xs.length === 1) return acc.concat(xs[0]);
42-
return acc.concat(xs.filter(Boolean).map(function (x) {
43-
if (RegExp('^' + TOKEN).test(x)) {
44-
return JSON.parse(x.split(TOKEN)[1]);
45-
}
46-
else return x;
47-
}));
48-
}, []);
49-
};
50-
5131
function parse(s, env, opts) {
5232
var chunker = new RegExp([
5333
'(' + CONTROL + ')', // control chars
5434
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
5535
].join('|'), 'g');
5636
var match = s.match(chunker).filter(Boolean);
37+
38+
if (!match) {
39+
return [];
40+
}
41+
if (!env) {
42+
env = {};
43+
}
44+
if (!opts) {
45+
opts = {};
46+
}
47+
5748
var commented = false;
5849

59-
if (!match) return [];
60-
if (!env) env = {};
61-
if (!opts) opts = {};
50+
function getVar(_, pre, key) {
51+
var r = typeof env === 'function' ? env(key) : env[key];
52+
if (r === undefined && key != '') {
53+
r = '';
54+
} else if (r === undefined) {
55+
r = '$';
56+
}
57+
58+
if (typeof r === 'object') {
59+
return pre + TOKEN + JSON.stringify(r) + TOKEN;
60+
}
61+
return pre + r;
62+
}
63+
6264
return match.map(function (s, j) {
6365
if (commented) {
64-
return;
66+
return void undefined;
6567
}
6668
if (RegExp('^' + CONTROL + '$').test(s)) {
6769
return { op: s };
6870
}
6971

7072
// Hand-written scanner/parser for Bash quoting rules:
7173
//
72-
// 1. inside single quotes, all characters are printed literally.
73-
// 2. inside double quotes, all characters are printed literally
74-
// except variables prefixed by '$' and backslashes followed by
75-
// either a double quote or another backslash.
76-
// 3. outside of any quotes, backslashes are treated as escape
77-
// characters and not printed (unless they are themselves escaped)
78-
// 4. quote context can switch mid-token if there is no whitespace
74+
// 1. inside single quotes, all characters are printed literally.
75+
// 2. inside double quotes, all characters are printed literally
76+
// except variables prefixed by '$' and backslashes followed by
77+
// either a double quote or another backslash.
78+
// 3. outside of any quotes, backslashes are treated as escape
79+
// characters and not printed (unless they are themselves escaped)
80+
// 4. quote context can switch mid-token if there is no whitespace
7981
// between the two quote contexts (e.g. all'one'"token" parses as
8082
// "allonetoken")
8183
var SQ = "'";
@@ -86,22 +88,52 @@ function parse(s, env, opts) {
8688
var esc = false;
8789
var out = '';
8890
var isGlob = false;
91+
var i;
8992

90-
for (var i = 0, len = s.length; i < len; i++) {
93+
function parseEnvVar() {
94+
i += 1;
95+
var varend;
96+
var varname;
97+
// debugger
98+
if (s.charAt(i) === '{') {
99+
i += 1;
100+
if (s.charAt(i) === '}') {
101+
throw new Error('Bad substitution: ' + s.substr(i - 2, 3));
102+
}
103+
varend = s.indexOf('}', i);
104+
if (varend < 0) {
105+
throw new Error('Bad substitution: ' + s.substr(i));
106+
}
107+
varname = s.substr(i, varend - i);
108+
i = varend;
109+
} else if ((/[*@#?$!_-]/).test(s.charAt(i))) {
110+
varname = s.charAt(i);
111+
i += 1;
112+
} else {
113+
varend = s.substr(i).match(/[^\w\d_]/);
114+
if (!varend) {
115+
varname = s.substr(i);
116+
i = s.length;
117+
} else {
118+
varname = s.substr(i, varend.index);
119+
i += varend.index - 1;
120+
}
121+
}
122+
return getVar(null, '', varname);
123+
}
124+
125+
for (i = 0; i < s.length; i++) {
91126
var c = s.charAt(i);
92127
isGlob = isGlob || (!quote && (c === '*' || c === '?'));
93128
if (esc) {
94129
out += c;
95130
esc = false;
96-
}
97-
else if (quote) {
131+
} else if (quote) {
98132
if (c === quote) {
99133
quote = false;
100-
}
101-
else if (quote == SQ) {
134+
} else if (quote == SQ) {
102135
out += c;
103-
}
104-
else { // Double quote
136+
} else { // Double quote
105137
if (c === BS) {
106138
i += 1;
107139
c = s.charAt(i);
@@ -110,92 +142,62 @@ function parse(s, env, opts) {
110142
} else {
111143
out += BS + c;
112144
}
113-
}
114-
else if (c === DS) {
145+
} else if (c === DS) {
115146
out += parseEnvVar();
116-
}
117-
else {
147+
} else {
118148
out += c;
119149
}
120150
}
121-
}
122-
else if (c === DQ || c === SQ) {
151+
} else if (c === DQ || c === SQ) {
123152
quote = c;
124-
}
125-
else if (RegExp('^' + CONTROL + '$').test(c)) {
153+
} else if (RegExp('^' + CONTROL + '$').test(c)) {
126154
return { op: s };
127-
}
128-
else if (RegExp('^#$').test(c)) {
155+
} else if ((/^#$/).test(c)) {
129156
commented = true;
130157
if (out.length) {
131158
return [out, { comment: s.slice(i + 1) + match.slice(j + 1).join(' ') }];
132159
}
133160
return [{ comment: s.slice(i + 1) + match.slice(j + 1).join(' ') }];
134-
}
135-
else if (c === BS) {
161+
} else if (c === BS) {
136162
esc = true;
137-
}
138-
else if (c === DS) {
163+
} else if (c === DS) {
139164
out += parseEnvVar();
165+
} else {
166+
out += c;
140167
}
141-
else out += c;
142168
}
143169

144-
if (isGlob) return { op: 'glob', pattern: out };
170+
if (isGlob) {
171+
return { op: 'glob', pattern: out };
172+
}
145173

146174
return out;
147-
148-
function parseEnvVar() {
149-
i += 1;
150-
var varend, varname;
151-
// debugger
152-
if (s.charAt(i) === '{') {
153-
i += 1;
154-
if (s.charAt(i) === '}') {
155-
throw new Error("Bad substitution: " + s.substr(i - 2, 3));
156-
}
157-
varend = s.indexOf('}', i);
158-
if (varend < 0) {
159-
throw new Error("Bad substitution: " + s.substr(i));
160-
}
161-
varname = s.substr(i, varend - i);
162-
i = varend;
163-
}
164-
else if (/[*@#?$!_\-]/.test(s.charAt(i))) {
165-
varname = s.charAt(i);
166-
i += 1;
167-
}
168-
else {
169-
varend = s.substr(i).match(/[^\w\d_]/);
170-
if (!varend) {
171-
varname = s.substr(i);
172-
i = s.length;
173-
} else {
174-
varname = s.substr(i, varend.index);
175-
i += varend.index - 1;
176-
}
177-
}
178-
return getVar(null, '', varname);
175+
}).reduce(function (prev, arg) { // finalize parsed aruments
176+
if (arg === undefined) {
177+
return prev;
179178
}
180-
})
181-
// finalize parsed aruments
182-
.reduce(function (prev, arg) {
183-
if (arg === undefined) {
184-
return prev;
185-
}
186-
return prev.concat(arg);
187-
}, []);
188-
189-
function getVar(_, pre, key) {
190-
var r = typeof env === 'function' ? env(key) : env[key];
191-
if (r === undefined && key != '')
192-
r = '';
193-
else if (r === undefined)
194-
r = '$';
179+
return prev.concat(arg);
180+
}, []);
181+
}
195182

196-
if (typeof r === 'object') {
197-
return pre + TOKEN + JSON.stringify(r) + TOKEN;
198-
}
199-
else return pre + r;
183+
exports.parse = function (s, env, opts) {
184+
var mapped = parse(s, env, opts);
185+
if (typeof env !== 'function') {
186+
return mapped;
200187
}
201-
}
188+
return mapped.reduce(function (acc, s) {
189+
if (typeof s === 'object') {
190+
return acc.concat(s);
191+
}
192+
var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
193+
if (xs.length === 1) {
194+
return acc.concat(xs[0]);
195+
}
196+
return acc.concat(xs.filter(Boolean).map(function (x) {
197+
if (RegExp('^' + TOKEN).test(x)) {
198+
return JSON.parse(x.split(TOKEN)[1]);
199+
}
200+
return x;
201+
}));
202+
}, []);
203+
};

test/env.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ test('expand environment variables', function (t) {
1111
t.same(parse("'-$X-$Y-'", { X: 'a', Y: 'b' }), ['-$X-$Y-']);
1212
t.same(parse('qrs"$zzz"wxy', { zzz: 'tuv' }), ['qrstuvwxy']);
1313
t.same(parse("qrs'$zzz'wxy", { zzz: 'tuv' }), ['qrs$zzzwxy']);
14-
t.same(parse("qrs${zzz}wxy"), ['qrswxy']);
15-
t.same(parse("qrs$wxy $"), ['qrs', '$']);
14+
t.same(parse('qrs${zzz}wxy'), ['qrswxy']);
15+
t.same(parse('qrs$wxy $'), ['qrs', '$']);
1616
t.same(parse('grep "xy$"'), ['grep', 'xy$']);
17-
t.same(parse("ab$x", { x: 'c' }), ['abc']);
18-
t.same(parse("ab\\$x", { x: 'c' }), ['ab$x']);
19-
t.same(parse("ab${x}def", { x: 'c' }), ['abcdef']);
20-
t.same(parse("ab\\${x}def", { x: 'c' }), ['ab${x}def']);
17+
t.same(parse('ab$x', { x: 'c' }), ['abc']);
18+
t.same(parse('ab\\$x', { x: 'c' }), ['ab$x']);
19+
t.same(parse('ab${x}def', { x: 'c' }), ['abcdef']);
20+
t.same(parse('ab\\${x}def', { x: 'c' }), ['ab${x}def']);
2121
t.same(parse('"ab\\${x}def"', { x: 'c' }), ['ab${x}def']);
2222

2323
t.end();

test/env_fn.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
var test = require('tape');
44
var parse = require('../').parse;
55

6+
function getEnv() {
7+
return 'xxx';
8+
}
9+
10+
function getEnvObj() {
11+
return { op: '@@' };
12+
}
13+
614
test('functional env expansion', function (t) {
715
t.plan(4);
816

917
t.same(parse('a $XYZ c', getEnv), ['a', 'xxx', 'c']);
1018
t.same(parse('a $XYZ c', getEnvObj), ['a', { op: '@@' }, 'c']);
1119
t.same(parse('a${XYZ}c', getEnvObj), ['a', { op: '@@' }, 'c']);
1220
t.same(parse('"a $XYZ c"', getEnvObj), ['a ', { op: '@@' }, ' c']);
13-
14-
function getEnv() {
15-
return 'xxx';
16-
}
17-
18-
function getEnvObj() {
19-
return { op: '@@' };
20-
}
2121
});

test/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('parse shell commands', function (t) {
1414
t.same(parse('echo "foo = \\"foo\\""'), ['echo', 'foo = "foo"']);
1515
t.same(parse(''), []);
1616
t.same(parse(' '), []);
17-
t.same(parse("\t"), []);
17+
t.same(parse('\t'), []);
1818
t.same(parse('a"b c d"e'), ['ab c de']);
1919
t.same(parse('a\\ b"c d"\\ e f'), ['a bc d e', 'f']);
2020
t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);

test/quote.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ test('quote', function (t) {
1414
'\\$ \\` "\'"'
1515
);
1616
t.equal(quote([]), '');
17-
t.equal(quote(["a\nb"]), "'a\nb'");
17+
t.equal(quote(['a\nb']), "'a\nb'");
1818
t.equal(quote([' #(){}*|][!']), "' #(){}*|][!'");
1919
t.equal(quote(["'#(){}*|][!"]), '"\'#(){}*|][\\!"');
20-
t.equal(quote(["X#(){}*|][!"]), "X\\#\\(\\)\\{\\}\\*\\|\\]\\[\\!");
21-
t.equal(quote(["a\n#\nb"]), "'a\n#\nb'");
20+
t.equal(quote(['X#(){}*|][!']), 'X\\#\\(\\)\\{\\}\\*\\|\\]\\[\\!');
21+
t.equal(quote(['a\n#\nb']), "'a\n#\nb'");
2222
t.equal(quote(['><;{}']), '\\>\\<\\;\\{\\}');
2323
t.equal(quote(['a', 1, true, false]), 'a 1 true false');
2424
t.equal(quote(['a', 1, null, undefined]), 'a 1 null undefined');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy