Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-17070457 Allow multi conditional !import.meta.env.SSR statements #171

Merged
merged 23 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/util/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,38 @@
'use strict';

module.exports.isSSREscape = function isSSREscape(node) {
return (
if (
(node.type === 'IfStatement' || node.type === 'ConditionalExpression') &&
(isMetaEnvCheck(node.test) || isWindowOrDocumentCheck(node.test))
);
checkConditionalStatements(node.test)
) {
return true;
}

return false;
};

function checkConditionalStatements(test) {
let node = test;

// Base Case: If the node is UnaryExpression, call isMetaEnvCheck().
a-chabot marked this conversation as resolved.
Show resolved Hide resolved
if (node.type === 'UnaryExpression' && node.operator === '!') {
return isMetaEnvCheck(node);
}

// Base Case: If the node is a `!==` BinaryExpresion, call isWindowOrDocumentCheck().
if (node.type === 'BinaryExpression' && node.operator === '!==') {
return isWindowOrDocumentCheck(node);
}

// Recursive Case: If the node is a logical expression, check its left and right parts.
a-chabot marked this conversation as resolved.
Show resolved Hide resolved
if (node.type === 'LogicalExpression' && node.operator === '&&') {
const rightNodeConditional = checkConditionalStatements(node.right);
return !!rightNodeConditional || checkConditionalStatements(node.left);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to be consistent about returning booleans (!!) then we'll also need to fix isMetaEnvCheck since it can return undefined if it returns node.type.

Or we can just remove the !! because it's probably not important to return a boolean versus truthy/falsy. 🙂

}

return false;
}

function isMetaEnvCheck(test) {
let node = test;
if (!(node.type === 'UnaryExpression' && node.operator === '!')) return false;
Expand Down
179 changes: 179 additions & 0 deletions test/lib/rules/no-unsupported-ssr-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,95 @@ testRule('no-unsupported-ssr-properties', {
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (!import.meta.env.SSR && !randomOtherCheck) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (!import.meta.env.SSR && randomOtherCheck) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (randomOtherCheck && !import.meta.env.SSR) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (!a && b && !c && d && !import.meta.env.SSR) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (a && (b && !import.meta.env.SSR)) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
return !import.meta.env.SSR ? this.querySelector('button') : null;
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (randomOtherCheck && typeof window !== 'undefined') {
this.querySelector('span').getAttribute('role');
}
}
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -364,5 +453,95 @@ testRule('no-unsupported-ssr-properties', {
},
],
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (!a && b && !c && d) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
errors: [
{
messageId: 'propertyAccessFound',
},
],
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (a && (b || !import.meta.env.SSR)) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
errors: [
{
messageId: 'propertyAccessFound',
},
],
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (a || (b || !import.meta.env.SSR)) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
errors: [
{
messageId: 'propertyAccessFound',
},
],
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (a || (b && !import.meta.env.SSR)) {
this.querySelector('span').getAttribute('role');
}
}
}
`,
errors: [
{
messageId: 'propertyAccessFound',
},
],
},
{
code: `
import { LightningElement } from 'lwc';

export default class Foo extends LightningElement {
connectedCallback() {
if (randomOtherCheck && typeof window == 'undefined') {
this.querySelector('span').getAttribute('role');
}
}
}
`,
errors: [
{
messageId: 'propertyAccessFound',
},
],
},
],
});