Skip to content

Commit afec4e2

Browse files
committed
fix: adds tests for window + proxy
1 parent 136db62 commit afec4e2

File tree

5 files changed

+99
-24
lines changed

5 files changed

+99
-24
lines changed

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,25 @@ describe('ES12 / ES2021 Feature Tests', () => {
343343
});
344344
});
345345
});
346+
347+
describe('ES6 / Proxy Feature Tests', () => {
348+
it('👌 Es Check should fail when checking a file with `new Proxy(...)` as es5', (done) => {
349+
exec('node index.js es5 ./tests/proxy.js --checkFeatures', (err, stdout, stderr) => {
350+
console.log(stdout);
351+
assert(err, 'Expected an error but command ran successfully');
352+
done();
353+
});
354+
});
355+
356+
it('🎉 Es Check should pass when checking a file with `new Proxy(...)` as es6', (done) => {
357+
exec('node index.js es6 ./tests/proxy.js --checkFeatures', (err, stdout, stderr) => {
358+
if (err) {
359+
console.error(err.stack);
360+
console.error(stdout.toString());
361+
console.error(stderr.toString());
362+
return done(err);
363+
}
364+
done();
365+
});
366+
});
367+
});

tests/es5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ function test() {
44
function bfunc() {}
55
bfunc();
66
}
7+
window.getElementById('test');
78
test();

tests/proxy.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
function demoProxy() {
4+
const proxiedObject = new Proxy({}, {
5+
get(target, prop) {
6+
return `Intercepted: ${String(prop)}`;
7+
},
8+
});
9+
10+
console.log(proxiedObject.foo);
11+
}
12+
13+
demoProxy();

utils.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,40 @@ const checkDefault = () => {
3838
* - objectMethod => object: 'Object', property: 'fromEntries', etc.
3939
*/
4040
const checkCallExpression = (node, astInfo) => {
41-
// Must be `CallExpression`
4241
if (node.type !== 'CallExpression') return false;
43-
44-
// We might check if node.callee is a MemberExpression, e.g. array.includes(...)
45-
// or if node.callee is an Identifier, e.g. Symbol(...).
4642
if (node.callee.type === 'MemberExpression') {
4743
const { object, property } = astInfo;
48-
// e.g. object: 'Object', property: 'entries'
49-
// => node.callee.object.name === 'Object' && node.callee.property.name === 'entries'
50-
if (object) {
51-
if (
52-
!node.callee.object ||
53-
node.callee.object.type !== 'Identifier' ||
54-
node.callee.object.name !== object
55-
) {
56-
return false;
44+
45+
if (object || property) {
46+
47+
if (object) {
48+
if (
49+
!node.callee.object ||
50+
node.callee.object.type !== 'Identifier' ||
51+
node.callee.object.name !== object
52+
) {
53+
return false;
54+
}
5755
}
58-
}
59-
if (property) {
60-
// e.g. property: 'includes'
61-
if (!node.callee.property || node.callee.property.name !== property) {
62-
return false;
56+
57+
if (property) {
58+
if (
59+
!node.callee.property ||
60+
node.callee.property.type !== 'Identifier' ||
61+
node.callee.property.name !== property
62+
) {
63+
return false;
64+
}
6365
}
66+
return true;
6467
}
65-
return true;
66-
} else if (node.callee.type === 'Identifier') {
67-
// e.g. Symbol("desc")
68+
return false;
69+
}
70+
71+
if (node.callee.type === 'Identifier') {
6872
const { callee } = astInfo;
69-
// If astInfo.callee is "Symbol", check node.callee.name
70-
if (callee && node.callee.name === callee) {
71-
return true;
73+
if (callee && !astInfo.object && !astInfo.property) {
74+
return node.callee.name === callee;
7275
}
7376
}
7477

utils.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,40 @@ describe('Check Functions', function () {
126126
});
127127
});
128128

129+
describe('checkCallExpression', () => {
130+
const { checkMap } = require('./utils');
131+
132+
it('correctly distinguishes between global calls and member expressions', () => {
133+
const symbolNode = {
134+
type: 'CallExpression',
135+
callee: {
136+
type: 'Identifier',
137+
name: 'Symbol'
138+
}
139+
};
140+
141+
const getElementNode = {
142+
type: 'CallExpression',
143+
callee: {
144+
type: 'MemberExpression',
145+
object: {
146+
type: 'Identifier',
147+
name: 'window'
148+
},
149+
property: {
150+
type: 'Identifier',
151+
name: 'getElementById'
152+
}
153+
}
154+
};
155+
156+
assert.strictEqual(checkMap.CallExpression(symbolNode, { callee: 'Symbol' }), true);
157+
assert.strictEqual(checkMap.CallExpression(getElementNode, { callee: 'getElementById' }), false);
158+
assert.strictEqual(checkMap.CallExpression(getElementNode, {
159+
object: 'window',
160+
property: 'getElementById'
161+
}), true);
162+
});
163+
});
164+
129165
});

0 commit comments

Comments
 (0)