Skip to content

Commit

Permalink
docs(rules): improve 'valid-by-id' docs and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jan 20, 2017
1 parent c4c7b04 commit 34368a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ There are various types of rules implemented in the plugin. Here is a rough cate
* [use-simple-repeaters][]: Discourage using extended `ng-repeat` syntax in `by.repeater()` locators
* [no-repetitive-locators][]: Discourage repeating locators
* [no-repetitive-selectors][]: Discourage repeating parts of CSS selectors
* [valid-by-id][]: Prohibit use of invalid ID values when using `by.id`
* [valid-by-id][]: Prohibit use of invalid ID value when using `by.id()` locator
#### Style Guide Recommendations and Best Practices
Expand Down
7 changes: 4 additions & 3 deletions docs/rules/valid-by-id.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Warn if using invalid ID values
# Prohibit use of invalid ID value when using `by.id()` locator

Ensure ID is valid when using `by.id`. We take [HTML4 standard](https://www.w3.org/TR/html4/types.html#type-id)
Ensure ID is valid when using `by.id()` locator. We take [HTML4 standard](https://www.w3.org/TR/html4/types.html#type-id)
to determine the validness of the ID. Basically we are checking if the ID matches the following rule:

> ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters,
Expand All @@ -9,7 +9,8 @@ digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."
Let's take into account that [HTML5](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute)
is even more permissive which could result in conflicts with this rule.

This rule is very useful for notifying when an invalid html `id` is being used.
This rule is very useful for notifying when an invalid html `id` is being used.
It will also prevent unintentionally putting `id` attribute CSS selector instead of the actual `id` value - for example: `#my-id` instead of just `my-id`.

## Rule details

Expand Down
4 changes: 3 additions & 1 deletion lib/rules/valid-by-id.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

/**
* @fileoverview asdads
* @fileoverview Ensure ID is valid when using `by.id()` locator.
* @author Raul Gallegos
*/

Expand All @@ -17,8 +17,10 @@ module.exports = {
'CallExpression': function (node) {
var object = node.callee.object
var property = node.callee.property

var insideById = object && property && object.name === 'by' && property.name === 'id'
var argumentExists = node.arguments && node.arguments.length && node.arguments[0].value

if (insideById && argumentExists) {
var idValue = node.arguments[0].value
if (!idValue.match(isHtmlID)) {
Expand Down

0 comments on commit 34368a7

Please sign in to comment.