Skip to content
Open
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
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fixupConfigRules } from "@eslint/compat"
import { FlatCompat } from "@eslint/eslintrc"
import js from "@eslint/js"
import myPlugin from "@ota-meshi/eslint-plugin"
import pere from "eslint-plugin-pere"
import regexp from "eslint-plugin-regexp"
import unicorn from "eslint-plugin-unicorn"
import tseslint from "typescript-eslint"
Expand Down Expand Up @@ -169,6 +170,12 @@ export default [
"no-implicit-globals": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/prefer-string-starts-ends-with": [
"error",
{
allowSingleElementEquality: "always",
},
],
},
},
{
Expand Down Expand Up @@ -253,4 +260,11 @@ export default [
"unicorn/prefer-string-raw": "error",
},
},
{
files: ["**/*.{ts,mts}"],
plugins: { pere },
rules: {
"pere/no-startswith-one-char": "error",
},
},
]
2 changes: 1 addition & 1 deletion lib/rules/match-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default createRule("match-any", {

if (
regexpNode.type === "CharacterClass" &&
preference.startsWith("[") &&
preference[0] === "[" &&
preference.endsWith("]")
) {
// We know that the first and last character are the same,
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-control-character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default createRule("no-control-character", {
function isAllowedEscapeRaw(raw: string): boolean {
return (
ALLOWED_CONTROL_CHARS.test(raw) ||
(raw.startsWith("\\") &&
(raw[0] === "\\" &&
ALLOWED_CONTROL_CHARS.test(raw.slice(1)))
)
}
Expand All @@ -78,7 +78,7 @@ export default createRule("no-control-character", {

return (
isBadEscapeRaw(char.raw, char.value) ||
(char.raw.startsWith("\\") &&
(char.raw[0] === "\\" &&
isBadEscapeRaw(char.raw.slice(1), char.value))
)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default createRule("no-useless-escape", {
onExpressionCharacterClassLeave: () =>
characterClassStack.shift(),
onCharacterEnter(cNode) {
if (cNode.raw.startsWith("\\")) {
if (cNode.raw[0] === "\\") {
// escapes
const char = cNode.raw.slice(1)
const escapedChar = String.fromCodePoint(cNode.value)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default createRule("no-useless-range", {
}
}

if (rawAfter.startsWith("-")) {
if (rawAfter[0] === "-") {
// the next "-" might be interpreted as a range
// operator now, so we have to escape it
// e.g. /[a-a-z]/ -> /[a\-z]/
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-character-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function elementsToCharacterClass(elements: CharElementArray): string {
}
})

if (parts.length > 0 && parts[0].startsWith("^")) {
if (parts.length > 0 && parts[0][0] === "^") {
parts[0] = `\\${parts[0]}`
}

Expand Down
6 changes: 3 additions & 3 deletions lib/rules/sort-character-class-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default createRule("sort-character-class-elements", {
*/
function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) {
let raw = node.raw
if (raw.startsWith("-")) {
if (raw[0] === "-") {
const parent = target.parent as CharacterClass
const elements: (
| UnicodeSetsCharacterClassElement
Expand All @@ -296,7 +296,7 @@ function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) {
) {
raw = `\\${raw}`
}
} else if (raw.startsWith("^")) {
} else if (raw[0] === "^") {
const parent = target.parent as CharacterClass
const elements: (
| UnicodeSetsCharacterClassElement
Expand All @@ -306,7 +306,7 @@ function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) {
raw = `\\${raw}`
}
}
if (target.raw.startsWith("-")) {
if (target.raw[0] === "-") {
if (node.type === "Character" || node.type === "CharacterSet") {
raw = `${raw}\\`
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
return
}

if (cNode.raw.startsWith("\\")) {
if (cNode.raw[0] === "\\") {
const identity = cNode.raw.slice(1)
const syntaxChars = insideCharClass
? CHARACTER_CLASS_SYNTAX_CHARACTERS
Expand Down Expand Up @@ -256,7 +256,7 @@
// There is a bug in regexpp that causes it throw a
// syntax error for all non-Unicode regexes with named
// backreferences.
// TODO: Remove this workaround when the bug is fixed.

Check warning on line 259 in lib/rules/strict.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Remove this workaround when the...'

Check warning on line 259 in lib/rules/strict.ts

View workflow job for this annotation

GitHub Actions / update

Unexpected 'todo' comment: 'TODO: Remove this workaround when the...'
return
}

Expand Down
9 changes: 4 additions & 5 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ export function mightCreateNewElement(before: string, after: string): boolean {
if (
(/(?:^|[^\\])(?:\\{2})*\\[1-9]\d*$/u.test(before) &&
/^\d/u.test(after)) ||
(/(?:^|[^\\])(?:\\{2})*\\k$/u.test(before) && after.startsWith("<")) ||
(/(?:^|[^\\])(?:\\{2})*\\k$/u.test(before) && after[0] === "<") ||
/(?:^|[^\\])(?:\\{2})*\\k<[^<>]*$/u.test(before)
) {
return true
Expand All @@ -959,8 +959,7 @@ export function mightCreateNewElement(before: string, after: string): boolean {
/^[\d,}]/u.test(after)) ||
(/(?:^|[^\\])(?:\\{2})*\{\d+,$/u.test(before) &&
/^(?:\d+(?:\}|$)|\})/u.test(after)) ||
(/(?:^|[^\\])(?:\\{2})*\{\d+,\d*$/u.test(before) &&
after.startsWith("}"))
(/(?:^|[^\\])(?:\\{2})*\{\d+,\d*$/u.test(before) && after[0] === "}")
) {
return true
}
Expand Down Expand Up @@ -1014,13 +1013,13 @@ export function fixRemoveCharacterClassElement(
if (
// ... the text character is a dash
// e.g. [a\w-b] -> [a\-b], [\w-b] -> [-b], [\s\w-b] -> [\s-b]
(textAfter.startsWith("-") &&
(textAfter[0] === "-" &&
elementBefore &&
elementBefore.type === "Character") ||
// ... the next character is a caret and the caret will then be the
// first character in the character class
// e.g. [a^b] -> [\^b], [ba^] -> [b^]
(textAfter.startsWith("^") && !cc.negate && !elementBefore)
(textAfter[0] === "^" && !cc.negate && !elementBefore)
) {
return "\\"
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/regex-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export enum EscapeSequenceKind {
* Returns which escape sequence kind was used for the given raw of a character literal.
*/
export function getEscapeSequenceKind(raw: string): EscapeSequenceKind | null {
if (!raw.startsWith("\\")) {
if (raw[0] !== "\\") {
return null
}
if (isOctalEscape(raw)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/type-tracker/jsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function findJSDocComment(node: ES.Node, sourceCode: SourceCode) {
if (
tokenBefore &&
tokenBefore.type === "Block" &&
tokenBefore.value.startsWith("*")
tokenBefore.value[0] === "*"
) {
return tokenBefore
}
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"eslint-plugin-jsonc": "^2.16.0",
"eslint-plugin-markdown": "^5.0.0",
"eslint-plugin-n": "^17.9.0",
"eslint-plugin-pere": "^0.0.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-regexp": "~2.10.0",
"eslint-plugin-unicorn": "^61.0.0",
Expand Down
4 changes: 2 additions & 2 deletions tools/update-unicode-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function* getUnicodePropertyAliases(): AsyncIterable<UnicodePropertyAlias>
if (!line) {
continue
}
if (line.startsWith("#")) {
if (line[0] === "#") {
if (
/^#\s*=+$/u.test(dbLines[index - 1]) &&
/^#\s*=+$/u.test(dbLines[index + 1])
Expand Down Expand Up @@ -137,7 +137,7 @@ async function* getUnicodePropertyValueAliases(): AsyncIterable<UnicodePropertyV
logger.log("Fetching data... (%s)", DB_URL)
const dbContent = await fetch(DB_URL).then((res) => res.text())
for (const line of dbContent.split("\n")) {
if (!line || line.startsWith("#")) {
if (!line || line[0] === "#") {
continue
}
const [propertyAlias, short, long, ...other] = line
Expand Down
Loading