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

feat: eslint9 compatibility #148

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions lib/rule-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const { analyze } = require('./analyze-component');
const { isSSREscape } = require('./util/ssr');
const { isGlobalIdentifier } = require('./util/scope');
const { getScope, getAncestors } = require('./util/context');

/**
* Visitors for detecting methods/functions that are reachable during SSR
Expand Down Expand Up @@ -133,7 +134,7 @@ const noReferenceParentQualifiers = new Set([
const globalAccessQualifiers = new Set(['CallExpression', 'MemberExpression']);

function inModuleScope(node, context) {
for (const ancestor of context.getAncestors()) {
for (const ancestor of getAncestors(context, node)) {
if (moduleScopeDisqualifiers.has(ancestor.type)) {
return false;
}
Expand Down Expand Up @@ -172,7 +173,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
node.object.name === 'window') &&
node.property.type === 'Identifier' &&
forbiddenGlobalNames.has(node.property.name) &&
isGlobalIdentifier(node.object, context.getScope())
isGlobalIdentifier(node.object, getScope(context, node))
) {
// Prevents expressions like:
// globalThis.document.addEventListener('click', () => { ... });
Expand All @@ -197,7 +198,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
node.object.name === 'globalThis' &&
node.property.type === 'Identifier' &&
forbiddenGlobalNames.has(node.property.name) &&
isGlobalIdentifier(node.object, context.getScope())
isGlobalIdentifier(node.object, getScope(context, node))
) {
// Prevents expressions like:
// globalThis.addEventListener('click', () => { ... });
Expand All @@ -218,7 +219,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
node.parent.type !== 'MemberExpression' &&
node.object.type === 'Identifier' &&
forbiddenGlobalNames.has(node.object.name) &&
isGlobalIdentifier(node.object, context.getScope())
isGlobalIdentifier(node.object, getScope(context, node))
) {
// Prevents expressions like:
// window.addEventListener('click', () => { ... });
Expand Down Expand Up @@ -248,7 +249,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
if (
noReferenceParentQualifiers.has(node.parent.type) &&
forbiddenGlobalNames.has(node.name) &&
isGlobalIdentifier(node, context.getScope())
isGlobalIdentifier(node, getScope(context, node))
) {
// Prevents expressions like:
// doSomethingWith(window);
Expand All @@ -268,7 +269,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
node.parent.operator === 'instanceof' &&
node.parent.right === node &&
forbiddenGlobalNames.has(node.name) &&
isGlobalIdentifier(node, context.getScope())
isGlobalIdentifier(node, getScope(context, node))
) {
// Prevents expressions like:
// if (value instanceof Element) { ... }
Expand All @@ -288,7 +289,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'Identifier' &&
forbiddenGlobalNames.has(node.expression.callee.name) &&
isGlobalIdentifier(node, context.getScope())
isGlobalIdentifier(node, getScope(context, node))
) {
// Prevents global expressions like:
// addEventListener('resize', () => {...});
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/consistent-component-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path');

const { docUrl } = require('../util/doc-url');
const { isComponent } = require('../util/component');
const { getSourceCode } = require('../util/context');

module.exports = {
meta: {
Expand All @@ -26,7 +27,7 @@ module.exports = {
},
create(context) {
const fileName = context.getFilename();
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const fileBasename = path.basename(fileName, path.extname(fileName));
const expectComponentName = fileBasename.charAt(0).toUpperCase() + fileBasename.slice(1);
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-async-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const { docUrl } = require('../util/doc-url');
const { isGlobalIdentifier } = require('../util/scope');
const { getScope } = require('../util/context');

const GLOBAL_OBJECT = 'window';

Expand All @@ -29,7 +30,7 @@ module.exports = {
return {
CallExpression(node) {
const { callee } = node;
const scope = context.getScope();
const scope = getScope(context, node);

// Check for direct invocation of global restricted APIs.
// eg. setTimeout() or requestAnimationFrame();
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-leaky-event-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const { docUrl } = require('../util/doc-url');
const { isGlobalIdentifier } = require('../util/scope');
const { getScope } = require('../util/context');

module.exports = {
meta: {
Expand Down Expand Up @@ -83,7 +84,7 @@ module.exports = {
return {
CallExpression(node) {
const { callee } = node;
const scope = context.getScope();
const scope = getScope(context, node);

// Handle cases where the method is attached on the global object:
// - addEventListener('click', () => {});
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-unexpected-wire-adapter-usages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const { Minimatch } = require('minimatch');
const { docUrl } = require('../util/doc-url');
const { isWireDecorator } = require('../util/decorator');
const { getScope } = require('../util/context');

function getImportedIdentifier(specifierNode) {
// Namespace imports are not analyzed because it is impossible to track accurately the usage of
Expand Down Expand Up @@ -66,7 +67,7 @@ module.exports = {

return {
ImportDeclaration(node) {
const scope = context.getScope();
const scope = getScope(context, node);
const moduleIdentifier = node.source.value;

for (const specifier of node.specifiers) {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-unknown-wire-adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const { Minimatch } = require('minimatch');

const { docUrl } = require('../util/doc-url');
const { getScope } = require('../util/context');

module.exports = {
meta: {
Expand Down Expand Up @@ -76,7 +77,7 @@ module.exports = {
const adapterName = adapterNode.name;

// Let's resolve the reference to the wire adapter identifier in the current scope.
const scope = context.getScope();
const scope = getScope(context, node);
const adapterVariable = scope.references.find(
(r) => r.identifier === adapterNode,
).resolved;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/prefer-custom-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const { docUrl } = require('../util/doc-url');
const { isGlobalIdentifier } = require('../util/scope');
const { getScope } = require('../util/context');

module.exports = {
meta: {
Expand All @@ -25,7 +26,7 @@ module.exports = {
return {
NewExpression(node) {
const { callee } = node;
const scope = context.getScope();
const scope = getScope(context, node);

if (
callee.type === 'Identifier' &&
Expand Down
4 changes: 3 additions & 1 deletion lib/util/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
'use strict';

const { getAncestors } = require('./context');

/**
* Returns true if the node is a LWC component.
*
Expand All @@ -19,7 +21,7 @@ function isComponent(node, context) {
return false;
}

const program = context.getAncestors(node).find(({ type }) => type === 'Program');
const program = getAncestors(context, node).find(({ type }) => type === 'Program');

const importDeclaration = program.body
.filter(({ type }) => type === 'ImportDeclaration')
Expand Down
41 changes: 41 additions & 0 deletions lib/util/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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';

function getSourceCode(context) {
if ('sourceCode' in context) {
return context.sourceCode;
}

return context.getSourceCode();
}

function getScope(context, node) {
const sourceCode = getSourceCode(context);

if (sourceCode && sourceCode.getScope) {
return sourceCode.getScope(node);
}

return context.getScope();
}

function getAncestors(context, node) {
const sourceCode = getSourceCode(context);

if (sourceCode && sourceCode.getAncestors) {
return sourceCode.getAncestors(node);
}

return context.getAncestors();
}

module.exports = {
getSourceCode,
getScope,
getAncestors,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"peerDependencies": {
"@babel/eslint-parser": "^7",
"eslint": "^7 || ^8"
"eslint": "^7 || ^8 || ^9"
},
"repository": {
"type": "git",
Expand Down