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

Use isStaticRequire instead of selectors #2096

Merged
merged 9 commits into from
May 15, 2023
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
16 changes: 8 additions & 8 deletions rules/ast/is-static-require.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const {isStringLiteral} = require('./literal.js');
const {isCallExpression} = require('./call-or-new-expression.js');

const isStaticRequire = node => Boolean(
node?.type === 'CallExpression'
&& node.callee.type === 'Identifier'
&& node.callee.name === 'require'
&& !node.optional
&& node.arguments.length === 1
&& isStringLiteral(node.arguments[0]),
);
const isStaticRequire = node =>
isCallExpression(node, {
name: 'require',
argumentsLength: 1,
optional: false,
})
&& isStringLiteral(node.arguments[0]);

module.exports = isStaticRequire;
38 changes: 21 additions & 17 deletions rules/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
'use strict';
const {methodCallSelector, STATIC_REQUIRE_SELECTOR} = require('./selectors/index.js');
const {methodCallSelector} = require('./selectors/index.js');
const {isStaticRequire} = require('./ast/index.js');

const MESSAGE_ID = 'no-process-exit';
const messages = {
[MESSAGE_ID]: 'Only use `process.exit()` in CLI apps. Throw an error instead.',
};

const importWorkerThreadsSelector = [
// `require('worker_threads')`
[
STATIC_REQUIRE_SELECTOR,
'[arguments.0.value="worker_threads"]',
].join(''),
// `import workerThreads from 'worker_threads'`
[
'ImportDeclaration',
'[source.type="Literal"]',
'[source.value="worker_threads"]',
].join(''),
].join(', ');
const processOnOrOnceCallSelector = methodCallSelector({
object: 'process',
methods: ['on', 'once'],
Expand All @@ -44,9 +32,25 @@ const create = context => {
const problemNodes = [];

return {
// Check `worker_threads` require / import
[importWorkerThreadsSelector]() {
requiredWorkerThreadsModule = true;
CallExpression(callExpression) {
// `require('worker_threads')`
if (
isStaticRequire(callExpression)
// TODO: Support `node:worker_threads`
&& callExpression.arguments[0].value === 'worker_threads'
) {
requiredWorkerThreadsModule = true;
}
},
ImportDeclaration(importDeclaration) {
// `import workerThreads from 'worker_threads'`
if (
importDeclaration.source.type === 'Literal'
// TODO: Support `node:worker_threads`
&& importDeclaration.source.value === 'worker_threads'
) {
requiredWorkerThreadsModule = true;
}
},
// Check `process.on` / `process.once` call
[processOnOrOnceCallSelector](node) {
Expand Down
11 changes: 7 additions & 4 deletions rules/prefer-add-event-listener.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const {isParenthesized} = require('@eslint-community/eslint-utils');
const eventTypes = require('./shared/dom-events.js');
const {STATIC_REQUIRE_SOURCE_SELECTOR} = require('./selectors/index.js');
const {isUndefined, isNullLiteral} = require('./ast/index.js');
const {isUndefined, isNullLiteral, isStaticRequire} = require('./ast/index.js');

const MESSAGE_ID = 'prefer-add-event-listener';
const messages = {
Expand Down Expand Up @@ -73,8 +72,12 @@ const create = context => {
codePathInfo = codePathInfo.upper;
},

[STATIC_REQUIRE_SOURCE_SELECTOR](node) {
if (!isDisabled && excludedPackages.has(node.value)) {
CallExpression(node) {
if (!isStaticRequire(node)) {
return;
}

if (!isDisabled && excludedPackages.has(node.arguments[0].value)) {
isDisabled = true;
}
},
Expand Down
33 changes: 20 additions & 13 deletions rules/prefer-node-protocol.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
'use strict';
const isBuiltinModule = require('is-builtin-module');
const {matches, STATIC_REQUIRE_SOURCE_SELECTOR} = require('./selectors/index.js');
const {replaceStringLiteral} = require('./fix/index.js');
const isStaticRequire = require('./ast/is-static-require.js');

const MESSAGE_ID = 'prefer-node-protocol';
const messages = {
[MESSAGE_ID]: 'Prefer `node:{{moduleName}}` over `{{moduleName}}`.',
};

const importExportSourceSelector = [
':matches(ImportDeclaration, ExportNamedDeclaration, ImportExpression)',
' > ',
'Literal.source',
].join('');

const selector = matches([
importExportSourceSelector,
STATIC_REQUIRE_SOURCE_SELECTOR,
]);

const create = () => ({
[selector](node) {
Literal(node) {
if (!(
(
(
node.parent.type === 'ImportDeclaration'
|| node.parent.type === 'ExportNamedDeclaration'
|| node.parent.type === 'ImportExpression'
)
&& node.parent.source === node
)
|| (
isStaticRequire(node.parent)
&& node.parent.arguments[0] === node
)
)) {
return;
}

const {value} = node;

if (
typeof value !== 'string'
|| value.startsWith('node:')
Expand Down
2 changes: 0 additions & 2 deletions rules/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ module.exports = {
callExpressionSelector: require('./call-or-new-expression-selector.js').callExpressionSelector,
newExpressionSelector: require('./call-or-new-expression-selector.js').newExpressionSelector,
callOrNewExpressionSelector: require('./call-or-new-expression-selector.js').callOrNewExpressionSelector,
STATIC_REQUIRE_SELECTOR: require('./require-selector.js').STATIC_REQUIRE_SELECTOR,
STATIC_REQUIRE_SOURCE_SELECTOR: require('./require-selector.js').STATIC_REQUIRE_SOURCE_SELECTOR,
};
14 changes: 0 additions & 14 deletions rules/selectors/require-selector.js

This file was deleted.