Skip to content

Commit 561a1ee

Browse files
author
David Reed
committed
run ESLint
Several bugs were found (and fixed): src/client/index.js:71 the following if() was equivalent to if (false in obj) src/client/dom/element.js:207 the following if() was equivalent to if (!element || false in element) src/client/rewrite/html.js:179 an undefined iterate() was referenced
1 parent f0611a9 commit 561a1ee

19 files changed

+161
-220
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"worker": true,
5+
"serviceworker": true,
6+
"es6": true,
7+
"node": true
8+
},
9+
"globals": { "globalThis": true, "importScripts": true },
10+
"parserOptions": { "sourceType": "module", "ecmaVersion": "latest" },
11+
"extends": ["eslint:recommended"]
12+
}

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

package-lock.json

+76-137
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"devDependencies": {
3333
"copy-webpack-plugin": "^11.0.0",
3434
"cross-env": "^7.0.3",
35-
"eslint": "^8.8.0",
35+
"eslint": "^8.28.0",
3636
"prettier": "^2.7.1",
3737
"terser-webpack-plugin": "^5.3.6",
3838
"webpack": "^5.74.0",

src/client/dom/document.js

-15
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,6 @@ class DocumentHook extends EventEmitter {
193193
},
194194
});
195195
}
196-
overrideReferrer() {
197-
this.ctx.overrideDescriptor(this.docProto, 'referrer', {
198-
get: (target, that) => {
199-
const event = new HookEvent(
200-
{ value: target.call(that) },
201-
target,
202-
that
203-
);
204-
this.emit('referrer', event);
205-
206-
if (event.intercepted) return event.returnValue;
207-
return event.data.value;
208-
},
209-
});
210-
}
211196
overrideCookie() {
212197
this.ctx.overrideDescriptor(this.docProto, 'cookie', {
213198
get: (target, that) => {

src/client/dom/element.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class ElementApi extends EventEmitter {
204204
);
205205
}
206206
hookProperty(element, prop, handler) {
207-
if (!element || !prop in element) return false;
207+
if (!element || !(prop in element)) return false;
208208

209209
if (this.ctx.nativeMethods.isArray(element)) {
210210
for (const elem of element) {

src/client/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class UVClient extends EventEmitter {
6868
);
6969
}
7070
override(obj, prop, wrapper, construct) {
71-
if (!prop in obj) return false;
71+
if (!(prop in obj)) return false;
7272
const wrapped = this.wrap(obj, prop, wrapper, construct);
7373
return (obj[prop] = wrapped);
7474
}
@@ -92,7 +92,7 @@ class UVClient extends EventEmitter {
9292
},
9393
}.attach;
9494

95-
if (!!construct) {
95+
if (construct) {
9696
wrapped.prototype = fn.prototype;
9797
wrapped.prototype.constructor = wrapped;
9898
}

src/client/location.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class LocationApi extends EventEmitter {
3737

3838
for (const key of this.keys) {
3939
this.ctx.overrideDescriptor(this.workerLocProto, key, {
40-
get: (target, that) => {
40+
get: () => {
4141
return parse(uv.href.get.call(this.location))[key];
4242
},
4343
});
@@ -78,9 +78,12 @@ class LocationApi extends EventEmitter {
7878
);
7979
break;
8080
default:
81-
const url = new URL(emulation.href);
82-
url[key] = val;
83-
that.location.href = wrap(url.href);
81+
{
82+
const url = new URL(emulation.href);
83+
url[key] = val;
84+
that.location.href = wrap(url.href);
85+
}
86+
break;
8487
}
8588
}
8689
: undefined,

src/rewrite/cookie.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function validateCookie(cookie, meta, js = false) {
2121

2222
async function db(openDB) {
2323
const db = await openDB('__op', 1, {
24-
upgrade(db, oldVersion, newVersion, transaction) {
24+
upgrade(db) {
2525
const store = db.createObjectStore('cookies', {
2626
keyPath: 'id',
2727
});

src/rewrite/html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class P5Element extends EventEmitter {
176176
if (this.stream) return null;
177177

178178
let str = '';
179-
iterate(this.node, (node) => {
179+
this.iterate(this.node, (node) => {
180180
if (node.nodeName === '#text') str += node.value;
181181
});
182182

src/rewrite/parsel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export default (function (exports) {
365365
* Calculate specificity of a selector.
366366
* If the selector is a list, the max specificity is returned.
367367
*/
368-
function specificity(selector, { format = 'array' } = {}) {
368+
function specificity(selector) {
369369
let ast =
370370
typeof selector === 'object'
371371
? selector

src/rewrite/rewrite.html.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function attributes(ctx, meta = ctx.meta) {
2-
const { html, js, css, attributePrefix, handlerScript, bundleScript } = ctx;
2+
const { html, js, attributePrefix } = ctx;
33
const origPrefix = attributePrefix + '-attr-';
44

55
html.on('attr', (attr, type) => {
@@ -68,8 +68,8 @@ function attributes(ctx, meta = ctx.meta) {
6868
});
6969
}
7070

71-
function text(ctx, meta = ctx.meta) {
72-
const { html, js, css, attributePrefix } = ctx;
71+
function text(ctx) {
72+
const { html, js, css } = ctx;
7373

7474
html.on('text', (text, type) => {
7575
if (text.element.tagName === 'script') {
@@ -181,8 +181,7 @@ function isEvent(name) {
181181
}
182182

183183
function injectHead(ctx) {
184-
const { html, js, css, attributePrefix } = ctx;
185-
const origPrefix = attributePrefix + '-attr-';
184+
const { html } = ctx;
186185
html.on('element', (element, type) => {
187186
if (type !== 'rewrite') return false;
188187
if (element.tagName !== 'head') return false;

src/rewrite/rewrite.script.js

+42-35
Original file line numberDiff line numberDiff line change
@@ -261,54 +261,61 @@ function unwrap(ctx) {
261261

262262
switch (node.callee.property.name) {
263263
case '$wrap':
264-
if (
265-
!node.arguments ||
266-
node.parent.type !== Syntax.MemberExpression ||
267-
node.parent.property !== node
268-
)
269-
return false;
270-
const [property] = node.arguments;
264+
{
265+
if (
266+
!node.arguments ||
267+
node.parent.type !== Syntax.MemberExpression ||
268+
node.parent.property !== node
269+
)
270+
return false;
271+
const [property] = node.arguments;
271272

272-
data.changes.push({
273-
start: node.callee.start,
274-
end: property.start,
275-
});
276-
277-
node.iterateEnd = function () {
278273
data.changes.push({
279-
start: node.end - 2,
280-
end: node.end,
274+
start: node.callee.start,
275+
end: property.start,
281276
});
282-
};
277+
278+
node.iterateEnd = function () {
279+
data.changes.push({
280+
start: node.end - 2,
281+
end: node.end,
282+
});
283+
};
284+
}
283285
break;
284286
case '$get':
285287
case 'rewriteUrl':
286-
const [arg] = node.arguments;
288+
{
289+
const [arg] = node.arguments;
287290

288-
data.changes.push({
289-
start: node.callee.start,
290-
end: arg.start,
291-
});
292-
293-
node.iterateEnd = function () {
294291
data.changes.push({
295-
start: node.end - 1,
296-
end: node.end,
292+
start: node.callee.start,
293+
end: arg.start,
297294
});
298-
};
295+
296+
node.iterateEnd = function () {
297+
data.changes.push({
298+
start: node.end - 1,
299+
end: node.end,
300+
});
301+
};
302+
}
299303
break;
300304
case 'rewrite':
301-
const [script] = node.arguments;
302-
data.changes.push({
303-
start: node.callee.start,
304-
end: script.start,
305-
});
306-
node.iterateEnd = function () {
305+
{
306+
const [script] = node.arguments;
307307
data.changes.push({
308-
start: node.end - 1,
309-
end: node.end,
308+
start: node.callee.start,
309+
end: script.start,
310310
});
311-
};
311+
node.iterateEnd = function () {
312+
data.changes.push({
313+
start: node.end - 1,
314+
end: node.end,
315+
});
316+
};
317+
}
318+
break;
312319
}
313320
});
314321
}

src/sw.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*global UVServiceWorker*/
12
importScripts('/uv.bundle.js');
23
importScripts('/uv.config.js');
34
importScripts('/uv.sw.js');

src/uv.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*global Ultraviolet*/
12
self.__uv$config = {
23
prefix: '/service/',
34
bare: 'https://incog.dev/bare/',

src/uv.handler.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ function __uvHook(window, config = {}, bare = '/bare/') {
481481
});
482482

483483
client.overrideDescriptor(window, 'origin', {
484-
get: (target, that) => {
484+
get: () => {
485485
return __uv.location.origin;
486486
},
487487
});
@@ -1318,20 +1318,13 @@ function __uvHook(window, config = {}, bare = '/bare/') {
13181318
});
13191319

13201320
// Proper hash emulation.
1321-
if (!!window.window) {
1322-
__uv.addEventListener.call(window, 'hashchange', (event) => {
1323-
if (event.__uv$dispatched) return false;
1324-
event.stopImmediatePropagation();
1325-
const hash = window.location.hash;
1326-
client.history.replaceState.call(
1327-
window.history,
1328-
'',
1329-
'',
1330-
event.oldURL
1331-
);
1332-
__uv.location.hash = hash;
1333-
});
1334-
}
1321+
__uv.addEventListener.call(window, 'hashchange', (event) => {
1322+
if (event.__uv$dispatched) return false;
1323+
event.stopImmediatePropagation();
1324+
const hash = window.location.hash;
1325+
client.history.replaceState.call(window.history, '', '', event.oldURL);
1326+
__uv.location.hash = hash;
1327+
});
13351328

13361329
client.location.on('hashchange', (oldUrl, newUrl, ctx) => {
13371330
if (ctx.HashChangeEvent && client.history.replaceState) {

src/uv.sw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*globals __uv$config*/
12
// Users must import the config (and bundle) prior to importing uv.sw.js
23
// This is to allow us to produce a generic bundle with no hard-coded paths.
34

@@ -29,7 +30,6 @@ const cspHeaders = [
2930
'x-xss-protection',
3031
];
3132
const emptyMethods = ['GET', 'HEAD'];
32-
const emptyStatus = [204, 304];
3333

3434
class UVServiceWorker extends Ultraviolet.EventEmitter {
3535
constructor(config = __uv$config) {

webpack.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import webpack from 'webpack';
21
import { fileURLToPath } from 'url';
32
import TerserPlugin from 'terser-webpack-plugin';
43
import CopyPlugin from 'copy-webpack-plugin';

0 commit comments

Comments
 (0)