Skip to content

Commit

Permalink
@W-17070457 Allow multi conditional !import.meta.env.SSR statements (#…
Browse files Browse the repository at this point in the history
…171)

Co-authored-by: Nolan Lawson <[email protected]>
  • Loading branch information
a-chabot and nolanlawson authored Dec 4, 2024
1 parent c9ac717 commit c57c51f
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 3 deletions.
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 a `!` UnaryExpression, call isMetaEnvCheck().
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.
if (node.type === 'LogicalExpression' && node.operator === '&&') {
const rightNodeConditional = checkConditionalStatements(node.right);
return rightNodeConditional || checkConditionalStatements(node.left);
}

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',
},
],
},
],
});

0 comments on commit c57c51f

Please sign in to comment.