-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
}) | ||
} | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
], | ||
} | ||
`) | ||
}) | ||
}) |