-
-
Notifications
You must be signed in to change notification settings - Fork 627
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
Ethan Cohen
committed
Mar 2, 2016
1 parent
a2ab4ef
commit 1f42630
Showing
9 changed files
with
124 additions
and
7 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,21 @@ | ||
# no-hash-href | ||
|
||
Enforce an anchor element's href prop value is not just #. You should use something more descriptive, or use a button instead. | ||
|
||
## Rule details | ||
|
||
This rule takes no arguments. | ||
|
||
### Succeed | ||
```jsx | ||
<a href="https://github.com" /> | ||
<a href="#section" /> | ||
<a href="foo" /> | ||
``` | ||
|
||
### Fail | ||
```jsx | ||
<a href="#" /> | ||
<a href={"#"} /> | ||
<a href={`#`} /> | ||
``` |
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
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,36 @@ | ||
/** | ||
* @fileoverview Enforce links may not point to just #. | ||
* @author Ethan Cohen | ||
*/ | ||
'use strict'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ---------------------------------------------------------------------------- | ||
|
||
import hasAttribute from '../util/hasAttribute'; | ||
|
||
const errorMessage = 'Links must not point to "#". Use a more descriptive href or use a button instead.'; | ||
|
||
module.exports = context => ({ | ||
JSXOpeningElement: node => { | ||
const type = node.name.name; | ||
// Only check img tags. | ||
if (type !== 'a') { | ||
return; | ||
} | ||
|
||
const href = hasAttribute(node.attributes, 'href'); | ||
|
||
if (href === '#') { | ||
context.report({ | ||
node, | ||
message: errorMessage | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports.schema = [ | ||
{ type: 'object' } | ||
]; |
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
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,53 @@ | ||
/** | ||
* @fileoverview Enforce links may not point to just #. | ||
* @author Ethan Cohen | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import rule from '../../../src/rules/no-hash-href'; | ||
import { RuleTester } from 'eslint'; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const expectedError = { | ||
message: 'Links must not point to "#". Use a more descriptive href or use a button instead.', | ||
type: 'JSXOpeningElement' | ||
}; | ||
|
||
ruleTester.run('no-hash-href', rule, { | ||
valid: [ | ||
{ code: '<a />;', parserOptions }, | ||
{ code: '<a {...props} />', parserOptions }, | ||
{ code: '<a href="foo" />', parserOptions }, | ||
{ code: '<a href={foo} />', parserOptions }, | ||
{ code: '<a href="/foo" />', parserOptions }, | ||
{ code: '<a href={`${undefined}`} />', parserOptions }, | ||
{ code: '<div href="foo" />', parserOptions }, | ||
{ code: '<a href={`${undefined}foo`}/>', parserOptions }, | ||
{ code: '<a href={`#${undefined}foo`}/>', parserOptions }, | ||
{ code: '<a href={`#foo`}/>', parserOptions }, | ||
{ code: '<a href={"foo"}/>', parserOptions }, | ||
{ code: '<a href="#foo" />', parserOptions } | ||
], | ||
invalid: [ | ||
{ code: '<a href="#" />', errors: [ expectedError ], parserOptions }, | ||
{ code: '<a href={"#"} />', errors: [ expectedError ], parserOptions }, | ||
{ code: '<a href={`#${undefined}`} />', errors: [ expectedError ], parserOptions } | ||
] | ||
}); |
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