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 13 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
42 changes: 38 additions & 4 deletions lib/util/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,46 @@
'use strict';

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

Choose a reason for hiding this comment

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

Since typeof window !== 'undefined' is basically the same as !import.meta.env.SSR, I think we should handle the recursive case here too:

if (randomOtherCheck && typeof window !== 'undefined') {

return true;
}
}

return false;
};

function checkConditionalStatements(test) {
let node = test;

// Recursive Case: If the node is a logical expression, check its left and right parts.
if (node.type === 'LogicalExpression' || node.type === 'BinaryExpression') {
a-chabot marked this conversation as resolved.
Show resolved Hide resolved
if (node.operator === '||') {
return false;
}

const rightNodeConditional = checkConditionalStatements(node.right);

if (!rightNodeConditional) {
return checkConditionalStatements(node.left);
} else if (rightNodeConditional) {
return true;
}
a-chabot marked this conversation as resolved.
Show resolved Hide resolved
}

// Base Case: If the node is an Identifier or MemberExpression, don't need to continue.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

@a-chabot a-chabot Nov 19, 2024

Choose a reason for hiding this comment

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

If the node is an Identifier or MemberExpression, then there is no way it can be !import.meta.env.SSR. This is a base case to break out of the recursion. Since it's not the base case we care about (UnaryExpression), there is nothing more to check.

Copy link
Contributor

Choose a reason for hiding this comment

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

This would be a helpful comment! 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, this if seems redundant to me since we fall through to return false on line 47 anyway. I think we can remove it entirely.

if (node.type === 'Identifier' || node.type === 'MemberExpression' || node.type === 'Literal') {
return false;
}

// Base Case: If the node is UnaryExpression, call isMetaEnvCheck().
if (node.type === 'UnaryExpression' && node.operator === '!') {
return isMetaEnvCheck(node);
}

return false;
}

function isMetaEnvCheck(test) {
let node = test;
if (!(node.type === 'UnaryExpression' && node.operator === '!')) return false;
Expand Down
148 changes: 148 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,82 @@ 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 isCSR ? this.template.querySelector('button') : null;
}
}
`,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look correct at all. This test is only passing because it's using this.template.querySelector rather than this.querySelector. We should open a separate issue to handle this.template.querySelector the same as this.querySelector.

Suggested change
{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
return isCSR ? this.template.querySelector('button') : null;
}
}
`,
},
{
code: `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {
connectedCallback() {
return isCSR ? this.template.querySelector('button') : null;
}
}
`,
},

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test to be this:

import { LightningElement } from 'lwc';

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

Seems to pass!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure an issue was opened but we have plans to migrate all isCSR into !import.meta.env.SSR because they were not getting detected by the linter.

],
invalid: [
{
Expand Down Expand Up @@ -364,5 +440,77 @@ 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',
}
]
},
],
});