Skip to content

Commit

Permalink
feat: no-uppercase-chars rule
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 22, 2024
1 parent ce79e8a commit 7e92daf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/runtime/shared/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RuleNoErrorResponse from './inspections/no-error-response-status'
import RuleNoJavascript from './inspections/no-javascript'
import RuleNoMissingHref from './inspections/no-missing-href'
import RuleNoNonAsciiChars from './inspections/no-non-ascii-chars'
import RuleNoUppercaseChars from './inspections/no-uppercase-chars'
import RuleNoWhitespace from './inspections/no-whitespace'
import RuleTrailingSlash from './inspections/trailing-slash'
import RuleRedirects from './redirects'
Expand All @@ -25,6 +26,7 @@ export const AllInspections = [
RuleNoDocumentRelative(),
RuleNoJavascript(),
RuleTrailingSlash(),
RuleNoUppercaseChars(),
RuleAbsoluteSiteUrls(),
RuleRedirects(),
RuleDescriptiveLinkText(),
Expand Down
18 changes: 18 additions & 0 deletions src/runtime/shared/inspections/no-uppercase-chars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineRule } from './util'

export default function RuleNoUppercaseChars() {
return defineRule({
id: 'no-uppercase-chars',
test({ report, link }) {
if (link.match(/[A-Z]/)) {
report({
name: 'no-uppercase-chars',
scope: 'warning',
message: 'Links should not contain uppercase characters.',
fix: link.toLowerCase(),
fixDescription: 'Convert to lowercase.',
})
}
},
})
}
46 changes: 46 additions & 0 deletions test/unit/rules/no-uppercase-chars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { RuleTestContext } from '../../../src/runtime/types'
import { describe, expect, it } from 'vitest'
import RuleNoUppercaseChars from '../../../src/runtime/shared/inspections/no-uppercase-chars'
import { runRule } from './util'

describe('rule no-uppercase-chars', () => {
it('works', () => {
expect(runRule({ link: 'https://example.com/PAGE' } as RuleTestContext, RuleNoUppercaseChars())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "https://example.com/page",
"link": "https://example.com/PAGE",
"passes": false,
"textContent": undefined,
"warning": [
{
"fix": "https://example.com/page",
"fixDescription": "Convert to lowercase.",
"message": "Links should not contain uppercase characters.",
"name": "no-uppercase-chars",
"scope": "warning",
},
],
}
`)

expect(runRule({ link: '/this/IS/a/TEEST' } as RuleTestContext, RuleNoUppercaseChars())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "/this/is/a/teest",
"link": "/this/IS/a/TEEST",
"passes": false,
"textContent": undefined,
"warning": [
{
"fix": "/this/is/a/teest",
"fixDescription": "Convert to lowercase.",
"message": "Links should not contain uppercase characters.",
"name": "no-uppercase-chars",
"scope": "warning",
},
],
}
`)
})
})

3 comments on commit 7e92daf

@dargmuesli
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harlan-zw what do you think about running this check on internal links only by default? External links and how external services treat casing are not really under the developer's control, so warning about this might add much noise, I think.

@harlan-zw
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm we there's many rules like this we shouldn't run on external links so this is a bug that should be fixed

@harlan-zw
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed in 4.0.2

Please sign in to comment.