Skip to content

Commit

Permalink
feat: no-whitespace rule
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 22, 2024
1 parent bfe9e0f commit ce79e8a
Show file tree
Hide file tree
Showing 3 changed files with 75 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 RuleNoWhitespace from './inspections/no-whitespace'
import RuleTrailingSlash from './inspections/trailing-slash'
import RuleRedirects from './redirects'

Expand All @@ -18,6 +19,7 @@ export const AllInspections = [
RuleNoDuplicateQueryParams(),
RuleNoNonAsciiChars(),
RuleMissingHash(),
RuleNoWhitespace(),
RuleNoDoubleSlashes(),
RuleNoErrorResponse(),
RuleNoDocumentRelative(),
Expand Down
28 changes: 28 additions & 0 deletions src/runtime/shared/inspections/no-whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineRule } from './util'

export default function RuleNoWhitespace() {
return defineRule({
id: 'no-whitespace',
test({ link, report }) {
// if it starts with or ends with whitespace we can safely fix it
if (link.trim() !== link) {
report({
name: 'no-whitespace',
scope: 'warning',
message: 'Links should not start or end with whitespace.',
fix: link.trim(),
fixDescription: 'Remove whitespace from start and end of link.',
})
}
// test for whitespace
if (link.trim().match(/\s/)) {
report({
name: 'no-whitespace',
scope: 'warning',
message: 'Links should not contain whitespace.',
tip: 'Use hyphens to separate words instead of spaces.',
})
}
},
})
}
45 changes: 45 additions & 0 deletions test/unit/rules/no-whitespace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { RuleTestContext } from '../../../src/runtime/types'
import { describe, expect, it } from 'vitest'
import RuleNoWhitespace from '../../../src/runtime/shared/inspections/no-whitespace'
import { runRule } from './util'

describe('rule no-whitespace', () => {
it('works', () => {
expect(runRule({ link: '/this/ is / a link' } as RuleTestContext, RuleNoWhitespace())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "/this/ is / a link",
"link": "/this/ is / a link",
"passes": false,
"textContent": undefined,
"warning": [
{
"message": "Links should not contain whitespace.",
"name": "no-whitespace",
"scope": "warning",
"tip": "Use hyphens to separate words instead of spaces.",
},
],
}
`)

expect(runRule({ link: ' /trimmable ' } as RuleTestContext, RuleNoWhitespace())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "/trimmable",
"link": " /trimmable ",
"passes": false,
"textContent": undefined,
"warning": [
{
"fix": "/trimmable",
"fixDescription": "Remove whitespace from start and end of link.",
"message": "Links should not start or end with whitespace.",
"name": "no-whitespace",
"scope": "warning",
},
],
}
`)
})
})

0 comments on commit ce79e8a

Please sign in to comment.