Skip to content

Commit

Permalink
Use isStaticRequire instead of selectors (#2096)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 15, 2023
1 parent 7a32edb commit a12831e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 58 deletions.
16 changes: 8 additions & 8 deletions rules/ast/is-static-require.js
@@ -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
@@ -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
@@ -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
@@ -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
Expand Up @@ -16,6 +16,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.

0 comments on commit a12831e

Please sign in to comment.