diff --git a/README.md b/README.md index fd3fc59..8ddcdad 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/rules/valid-by-id.md b/docs/rules/valid-by-id.md index 03136eb..f6640a0 100644 --- a/docs/rules/valid-by-id.md +++ b/docs/rules/valid-by-id.md @@ -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, @@ -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 diff --git a/lib/rules/valid-by-id.js b/lib/rules/valid-by-id.js index 6ce8924..4b96b9a 100644 --- a/lib/rules/valid-by-id.js +++ b/lib/rules/valid-by-id.js @@ -1,7 +1,7 @@ 'use strict' /** - * @fileoverview asdads + * @fileoverview Ensure ID is valid when using `by.id()` locator. * @author Raul Gallegos */ @@ -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)) {