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!: update dependency to eslint v9 @W-17445058 #176

Merged
merged 5 commits into from
Dec 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2.1

supported-eslint-versions: &supported-eslint-versions ["local", "7"]
supported-eslint-versions: &supported-eslint-versions ["local", "9"]

deploy_filters: &deploy_filters
filters:
Expand Down
15 changes: 0 additions & 15 deletions .eslintrc

This file was deleted.

22 changes: 22 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const globals = require('globals');
const js = require('@eslint/js');

module.exports = [
js.configs.recommended,
{
languageOptions: {
globals: {
...globals.mocha,
...globals.node,
},

sourceType: 'commonjs',
},

rules: {
strict: ['error', 'global'],
},
},
];
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 { getAncestors, getScope } = 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 @@ -168,7 +169,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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 @@ -193,7 +194,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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 @@ -214,7 +215,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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 @@ -244,7 +245,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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 @@ -264,7 +265,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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 @@ -284,7 +285,7 @@ function noReferenceDuringSSR(forbiddenGlobalNames, messageIds, context) {
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
5 changes: 3 additions & 2 deletions 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 @@ -25,8 +26,8 @@ module.exports = {
schema: [],
},
create(context) {
const fileName = context.getFilename();
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const fileName = context.filename ?? context.getFilename();

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 @@ -6,6 +6,7 @@
*/
'use strict';

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

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 @@ -6,6 +6,7 @@
*/
'use strict';

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

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 @@ -6,6 +6,7 @@
*/
'use strict';

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

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
44 changes: 44 additions & 0 deletions lib/util/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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';

/**
* Get the source code AST for the rule context.
* @param {RuleContext} context The context for the ESLint rule
* @returns {SourceCode} Object representing the source code AST
*/
function getSourceCode(context) {
return context.sourceCode ?? context.getSourceCode();
}

/**
* Get the ancestor nodes of a given node.
* @param {RuleContext} context The context for the ESLint rule
* @param {ASTNode} node An AST node
* @returns {ASTNode[]} Anscestor nodes
*/
function getAncestors(context, node) {
const sourceCode = getSourceCode(context);
return sourceCode.getAncestors ? sourceCode.getAncestors(node) : context.getAncestors();
}

/**
* Get the scope for a given node.
* @param {RuleContext} context The context for the ESLint rule
* @param {ASTNode} node An AST node
* @returns {Scope} The scope for a given node
*/
function getScope(context, node) {
const sourceCode = getSourceCode(context);
return sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
}

module.exports = {
getAncestors,
getScope,
getSourceCode,
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"release:publish": "npm publish --registry=https://registry.npmjs.org"
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@babel/eslint-parser": "^7.24.1",
"@babel/core": "^7.26.0",
"@babel/eslint-parser": "^7.25.9",
"@babel/preset-typescript": "^7.24.6",
"eslint": "^8.57.0",
"@eslint/js": "^9.17.0",
"eslint": "^9.17.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"mocha": "^10.4.0",
Expand All @@ -37,7 +38,7 @@
},
"peerDependencies": {
"@babel/eslint-parser": "^7",
"eslint": "^7 || ^8"
"eslint": "^9"
},
"repository": {
"type": "git",
Expand Down
29 changes: 3 additions & 26 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,16 @@
*/
'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const eslint = require('eslint');

const SCOPE_DIRECTORY = path.resolve(__dirname, '../node_modules/@lwc');
const PACKAGE_DIRECTORY = path.resolve(SCOPE_DIRECTORY, 'eslint-plugin-lwc');

before(() => {
if (!fs.existsSync(SCOPE_DIRECTORY)) {
fs.mkdirSync(SCOPE_DIRECTORY);
}

if (!fs.existsSync(PACKAGE_DIRECTORY)) {
fs.symlinkSync(path.resolve(__dirname, '..'), PACKAGE_DIRECTORY, 'dir');
}
});

after(() => {
if (fs.existsSync(PACKAGE_DIRECTORY)) {
fs.unlinkSync(PACKAGE_DIRECTORY);
}

if (fs.existsSync(SCOPE_DIRECTORY)) {
fs.rmdirSync(SCOPE_DIRECTORY);
}
});
const local = require('../lib/index');

it('should resolve plugin rules', async () => {
const cli = new eslint.ESLint({
useEslintrc: false,
overrideConfigFile: true,
overrideConfig: {
plugins: ['@lwc/eslint-plugin-lwc'],
plugins: { '@lwc/lwc': local },
rules: {
'@lwc/lwc/no-document-query': 'error',
'@lwc/lwc/no-inner-html': 'warn',
Expand Down
7 changes: 5 additions & 2 deletions test/lib/rules/no-leaky-event-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
'use strict';

const globals = require('globals');
const { testRule } = require('../shared');

function buildCases({ handlers }) {
Expand Down Expand Up @@ -94,8 +95,10 @@ testRule('no-leaky-event-listeners', {
code: `
addEventListener('test', () => handleTest());
`,
env: {
browser: true,
languageOptions: {
globals: {
...globals.browser,
},
},
errors: [
{
Expand Down
Loading