-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathssr.js
62 lines (55 loc) · 1.71 KB
/
ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
'use strict';
module.exports.isSSREscape = function isSSREscape(node) {
return (
(node.type === 'IfStatement' || node.type === 'ConditionalExpression') &&
(isMetaEnvCheck(node.test) || isWindowOrDocumentCheck(node.test))
);
};
function isMetaEnvCheck(test) {
let node = test;
if (!(node.type === 'UnaryExpression' && node.operator === '!')) return false;
node = node.argument;
if (
!(
node.type === 'MemberExpression' &&
node.property.type === 'Identifier' &&
node.property.name === 'SSR'
)
)
return false;
node = node.object;
if (
!(
node.type === 'MemberExpression' &&
node.property.type === 'Identifier' &&
node.property.name === 'env'
)
)
return false;
node = node.object; // .meta is not a MemberExpression, it's a MetaProperty in eslint
if (
!(
node.type === 'MetaProperty' &&
node.property.type === 'Identifier' &&
node.property.name === 'meta'
)
)
return false;
node = node.meta;
return node.type && node.name === 'import';
}
function isWindowOrDocumentCheck(node) {
return (
node.type === 'BinaryExpression' &&
node.left.type === 'UnaryExpression' &&
node.left.operator === 'typeof' &&
node.left.argument.type === 'Identifier' &&
(node.left.argument.name === 'document' || node.left.argument.name === 'window')
);
}