Skip to content

Commit

Permalink
fix: allow import.meta.env.SSR check (#122)
Browse files Browse the repository at this point in the history
* fix: account for import.meta.env.SSR for SSR

* fix: add example in docs
  • Loading branch information
abdulsattar authored Jun 21, 2023
1 parent 6dd691f commit 8e526d2
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
12 changes: 12 additions & 0 deletions docs/rules/no-restricted-browser-globals-during-ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export default class Foo extends LightningElement {
}
```

```js
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
renderedCallback() {
if (!import.meta.env.SSR) {
const parser = new DOMParser();
}
}
}
```
## Options
The rule takes one option, an object, which has one key `restricted-globals` which is an object. The keys in the object
Expand Down
55 changes: 49 additions & 6 deletions lib/util/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,56 @@
'use strict';

module.exports.isSSREscape = function isSSREscape(node) {
// TODO(117): replace this with `imports.meta.env` when we land support for that.
return (
node.type === 'IfStatement' &&
node.test.type === 'BinaryExpression' &&
node.test.left.type === 'UnaryExpression' &&
node.test.left.operator === 'typeof' &&
node.test.left.argument.type === 'Identifier' &&
(node.test.left.argument.name === 'document' || node.test.left.argument.name === 'window')
(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')
);
}
71 changes: 71 additions & 0 deletions test/lib/rules/no-restricted-browser-globals-during-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ tester.run('no-browser-globals-during-ssr', rule, {
}
`,
},
{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
if(!import.meta.env.SSR) {
window.x = 1;
}
}
}
`,
},
{ code: `const f = new Foo();` },
{ code: `const name = new Foo();` },
{
Expand Down Expand Up @@ -394,5 +407,63 @@ tester.run('no-browser-globals-during-ssr', rule, {
},
],
},

{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
if(!import.meta.env.notSSR) {
window.x = 1;
}
}
}
`,
errors: [
{
messageId: 'prohibitedBrowserAPIUsage',
data: { identifier: 'window' },
},
],
},
{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
if(!import.meta.notenv.SSR) {
window.x = 1;
}
}
}
`,
errors: [
{
messageId: 'prohibitedBrowserAPIUsage',
data: { identifier: 'window' },
},
],
},
{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
if(!notimport.meta.env.SSR) {
window.x = 1;
}
}
}
`,
errors: [
{
messageId: 'prohibitedBrowserAPIUsage',
data: { identifier: 'window' },
},
],
},
],
});

0 comments on commit 8e526d2

Please sign in to comment.