Skip to content
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 crates/oxc_linter/src/generated/rule_runner_impls.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 28 additions & 13 deletions crates/oxc_linter/src/rules/unicorn/prefer_code_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,31 @@ declare_oxc_lint!(

impl Rule for PreferCodePoint {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::CallExpression(call_expr) = node.kind() else {
let AstKind::StaticMemberExpression(member_expr) = node.kind() else {
return;
};

let Some(memb_expr) = call_expr.callee.as_member_expression() else {
return;
};

if memb_expr.is_computed() || memb_expr.optional() || call_expr.optional {
return;
}

let (current, replacement, span) = match memb_expr.static_property_info() {
Some((span, "charCodeAt")) => ("charCodeAt", "codePointAt", span),
Some((span, "fromCharCode")) => ("fromCharCode", "fromCodePoint", span),
let (span, property_name) = member_expr.static_property_info();
let (current, replacement) = match property_name {
"fromCharCode" => {
if !member_expr.object.is_specific_id("String") {
return;
}
("fromCharCode", "fromCodePoint")
}
"charCodeAt" => {
let AstKind::CallExpression(call_expr) = ctx.nodes().parent_kind(node.id()) else {
return;
};
if call_expr.optional
|| call_expr.callee.as_member_expression().and_then(|callee| {
callee.static_property_info().map(|(_, property_name)| property_name)
}) != Some("charCodeAt")
{
return;
}
("charCodeAt", "codePointAt")
}
_ => return,
};

Expand All @@ -82,12 +92,12 @@ fn test() {
"new foo.charCodeAt",
"charCodeAt(0)",
"foo.charCodeAt?.(0)",
"foo?.charCodeAt(0)",
"foo[charCodeAt](0)",
r#"foo["charCodeAt"](0)"#,
"foo.notCharCodeAt(0)",
"String.fromCodePoint(0x1f984)",
"String.fromCodePoint",
"NotString.fromCharCode(foo)",
"new String.fromCodePoint",
"fromCodePoint(foo)",
"String.fromCodePoint?.(foo)",
Expand All @@ -101,9 +111,12 @@ fn test() {

let fail = vec![
"string.charCodeAt(index)",
"string?.charCodeAt(index)",
"(( (( string )).charCodeAt( ((index)), )))",
"String.fromCharCode( code )",
"(( (( String )).fromCharCode( ((code)), ) ))",
"String.fromCharCode.bind(String)",
"const x = String.fromCharCode",
];

let fix = vec![
Expand All @@ -112,6 +125,8 @@ fn test() {
"(( (( String )).fromCharCode( ((code)), ) ))",
"(( (( String )).fromCodePoint( ((code)), ) ))",
),
("String.fromCharCode.bind(String)", "String.fromCodePoint.bind(String)"),
("const x = String.fromCharCode", "const x = String.fromCodePoint"),
(r#""🦄".charCodeAt(0)"#, r#""🦄".codePointAt(0)"#),
("String.fromCharCode(0x1f984);", "String.fromCodePoint(0x1f984);"),
];
Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/snapshots/unicorn_prefer_code_point.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Unicode is better supported in `codePointAt` than `charCodeAt`

⚠ eslint-plugin-unicorn(prefer-code-point): Prefer `codePointAt` over `charCodeAt`
╭─[prefer_code_point.tsx:1:9]
1 │ string?.charCodeAt(index)
· ──────────
╰────
help: Unicode is better supported in `codePointAt` than `charCodeAt`

⚠ eslint-plugin-unicorn(prefer-code-point): Prefer `codePointAt` over `charCodeAt`
╭─[prefer_code_point.tsx:1:17]
1 │ (( (( string )).charCodeAt( ((index)), )))
Expand All @@ -29,3 +36,17 @@ source: crates/oxc_linter/src/tester.rs
· ────────────
╰────
help: Unicode is better supported in `fromCodePoint` than `fromCharCode`

⚠ eslint-plugin-unicorn(prefer-code-point): Prefer `fromCodePoint` over `fromCharCode`
╭─[prefer_code_point.tsx:1:8]
1 │ String.fromCharCode.bind(String)
· ────────────
╰────
help: Unicode is better supported in `fromCodePoint` than `fromCharCode`

⚠ eslint-plugin-unicorn(prefer-code-point): Prefer `fromCodePoint` over `fromCharCode`
╭─[prefer_code_point.tsx:1:18]
1 │ const x = String.fromCharCode
· ────────────
╰────
help: Unicode is better supported in `fromCodePoint` than `fromCharCode`
Loading