mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(linter): clean up prefer_node_protocol and move to restriction (#3171)
closes #3161
This commit is contained in:
parent
e6027beec3
commit
a84454cf87
3 changed files with 12 additions and 91 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use oxc_ast::{
|
||||
ast::{Argument, CallExpression, Expression, ModuleDeclaration},
|
||||
ast::{Expression, ModuleDeclaration},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_diagnostics::{
|
||||
|
|
@ -7,9 +7,10 @@ use oxc_diagnostics::{
|
|||
thiserror::Error,
|
||||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_resolver::NODEJS_BUILTINS;
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, utils::NODE_BUILTINS_MODULE, AstNode};
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("eslint-plugin-unicorn(prefer-node-protocol): Prefer using the `node:` protocol when importing Node.js builtin modules.")]
|
||||
|
|
@ -32,7 +33,7 @@ declare_oxc_lint!(
|
|||
/// import fs from "node:fs";
|
||||
/// ```
|
||||
PreferNodeProtocol,
|
||||
style
|
||||
restriction
|
||||
);
|
||||
|
||||
impl Rule for PreferNodeProtocol {
|
||||
|
|
@ -44,7 +45,9 @@ impl Rule for PreferNodeProtocol {
|
|||
}
|
||||
_ => None,
|
||||
},
|
||||
AstKind::CallExpression(call) if !call.optional => get_static_require_arg(ctx, call),
|
||||
AstKind::CallExpression(call) if !call.optional => {
|
||||
call.common_js_require().map(|s| (s.value.clone(), s.span))
|
||||
}
|
||||
AstKind::ModuleDeclaration(ModuleDeclaration::ImportDeclaration(import)) => {
|
||||
Some((import.source.value.clone(), import.source.span))
|
||||
}
|
||||
|
|
@ -66,7 +69,9 @@ impl Rule for PreferNodeProtocol {
|
|||
} else {
|
||||
string_lit_value.to_string()
|
||||
};
|
||||
if module_name.starts_with("node:") || !NODE_BUILTINS_MODULE.contains(&module_name) {
|
||||
if module_name.starts_with("node:")
|
||||
|| NODEJS_BUILTINS.binary_search(&module_name.as_str()).is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -74,22 +79,6 @@ impl Rule for PreferNodeProtocol {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_static_require_arg<'a>(
|
||||
ctx: &LintContext<'a>,
|
||||
call: &CallExpression<'a>,
|
||||
) -> Option<(Atom<'a>, Span)> {
|
||||
let Expression::Identifier(ref id) = call.callee else { return None };
|
||||
match call.arguments.as_slice() {
|
||||
[Argument::StringLiteral(str)] if id.name == "require" => ctx
|
||||
.semantic()
|
||||
.scopes()
|
||||
.root_unresolved_references()
|
||||
.contains_key(id.name.as_str())
|
||||
.then(|| (str.value.clone(), str.span)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
use crate::tester::Tester;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
mod jest;
|
||||
mod jsdoc;
|
||||
mod nextjs;
|
||||
mod node;
|
||||
mod react;
|
||||
mod react_perf;
|
||||
mod tree_shaking;
|
||||
mod unicorn;
|
||||
|
||||
pub use self::{
|
||||
jest::*, jsdoc::*, nextjs::*, node::*, react::*, react_perf::*, tree_shaking::*, unicorn::*,
|
||||
jest::*, jsdoc::*, nextjs::*, react::*, react_perf::*, tree_shaking::*, unicorn::*,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
pub const NODE_BUILTINS_MODULE: phf::Set<&str> = phf::phf_set![
|
||||
"_http_agent",
|
||||
"_http_client",
|
||||
"_http_common",
|
||||
"_http_incoming",
|
||||
"_http_outgoing",
|
||||
"_http_server",
|
||||
"_stream_duplex",
|
||||
"_stream_passthrough",
|
||||
"_stream_readable",
|
||||
"_stream_transform",
|
||||
"_stream_wrap",
|
||||
"_stream_writable",
|
||||
"_tls_common",
|
||||
"_tls_wrap",
|
||||
"assert",
|
||||
"assert/strict",
|
||||
"async_hooks",
|
||||
"buffer",
|
||||
"child_process",
|
||||
"cluster",
|
||||
"console",
|
||||
"constants",
|
||||
"crypto",
|
||||
"dgram",
|
||||
"diagnostics_channel",
|
||||
"dns",
|
||||
"dns/promises",
|
||||
"domain",
|
||||
"events",
|
||||
"fs",
|
||||
"fs/promises",
|
||||
"http",
|
||||
"http2",
|
||||
"https",
|
||||
"inspector",
|
||||
"module",
|
||||
"net",
|
||||
"os",
|
||||
"path",
|
||||
"path/posix",
|
||||
"path/win32",
|
||||
"perf_hooks",
|
||||
"process",
|
||||
"punycode",
|
||||
"querystring",
|
||||
"readline",
|
||||
"repl",
|
||||
"stream",
|
||||
"stream/consumers",
|
||||
"stream/promises",
|
||||
"stream/web",
|
||||
"string_decoder",
|
||||
"sys",
|
||||
"timers",
|
||||
"timers/promises",
|
||||
"tls",
|
||||
"trace_events",
|
||||
"tty",
|
||||
"url",
|
||||
"util",
|
||||
"util/types",
|
||||
"v8",
|
||||
"vm",
|
||||
"worker_threads",
|
||||
"zlib",
|
||||
];
|
||||
Loading…
Reference in a new issue