Skip to content

Commit

Permalink
feat: update Unicode letter detection in capitalized-comments rule (#…
Browse files Browse the repository at this point in the history
…18375)

* feat: simplify `LETTER_PATTERN` regexp

* check characters beyond U+FFFF
  • Loading branch information
fasttime authored Apr 25, 2024
1 parent 1579ce0 commit a498f35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
17 changes: 10 additions & 7 deletions lib/rules/capitalized-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// Requirements
//------------------------------------------------------------------------------

const LETTER_PATTERN = require("./utils/patterns/letters");
const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
Expand All @@ -17,7 +16,8 @@ const astUtils = require("./utils/ast-utils");

const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN,
WHITESPACE = /\s/gu,
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u; // TODO: Combine w/ max-len pattern?
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u, // TODO: Combine w/ max-len pattern?
LETTER_PATTERN = /\p{L}/u;

/*
* Base schema body for defining the basic capitalization rule, ignorePattern,
Expand Down Expand Up @@ -233,7 +233,8 @@ module.exports = {
return true;
}

const firstWordChar = commentWordCharsOnly[0];
// Get the first Unicode character (1 or 2 code units).
const [firstWordChar] = commentWordCharsOnly;

if (!LETTER_PATTERN.test(firstWordChar)) {
return true;
Expand Down Expand Up @@ -273,12 +274,14 @@ module.exports = {
messageId,
fix(fixer) {
const match = comment.value.match(LETTER_PATTERN);
const char = match[0];

return fixer.replaceTextRange(
// Offset match.index by 2 to account for the first 2 characters that start the comment (// or /*)
const charIndex = comment.range[0] + match.index + 2;

// Offset match.index by 2 to account for the first 2 characters that start the comment (// or /*)
[comment.range[0] + match.index + 2, comment.range[0] + match.index + 3],
capitalize === "always" ? match[0].toLocaleUpperCase() : match[0].toLocaleLowerCase()
return fixer.replaceTextRange(
[charIndex, charIndex + char.length],
capitalize === "always" ? char.toLocaleUpperCase() : char.toLocaleLowerCase()
);
}
});
Expand Down
36 changes: 0 additions & 36 deletions lib/rules/utils/patterns/letters.js

This file was deleted.

38 changes: 38 additions & 0 deletions tests/lib/rules/capitalized-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,24 @@ ruleTester.run("capitalized-comments", rule, {
column: 1
}]
},
{
code: "// ꮳꮃꭹ",
output: "// Ꮳꮃꭹ",
errors: [{
messageId: "unexpectedLowercaseComment",
line: 1,
column: 1
}]
},
{
code: "/* 𐳡𐳡𐳡 */", // right-to-left-text
output: "/* 𐲡𐳡𐳡 */",
errors: [{
messageId: "unexpectedLowercaseComment",
line: 1,
column: 1
}]
},

// Using "always" string option
{
Expand Down Expand Up @@ -541,6 +559,26 @@ ruleTester.run("capitalized-comments", rule, {
column: 1
}]
},
{
code: "// Გ", // Georgian Mtavruli Capital Letter Gan (U+1C92)
output: "// გ", // Georgian Letter Gan (U+10D2)
options: ["never"],
errors: [{
messageId: "unexpectedUppercaseComment",
line: 1,
column: 1
}]
},
{
code: "// 𑢢", // Warang Citi Capital Letter Wi (U+118A2)
output: "// 𑣂", // Warang Citi Small Letter Wi (U+118C2)
options: ["never"],
errors: [{
messageId: "unexpectedUppercaseComment",
line: 1,
column: 1
}]
},

// Default ignore words should be warned if there are non-whitespace characters in the way
{
Expand Down

0 comments on commit a498f35

Please sign in to comment.