Determine if an HTML tag name is valid We've developed an ESLint rule to determine if a tag name used inside the Protractor's by.tagName() locator is valid.


  A Tag Name is considered valid when:
  
  
  It can contain alphanumeric characters.
  It can contain the dash(-) symbol.
  It cannot start with dash or a number.
  It cannot end with dash.
  


The Code:

'use strict'

var isTagName = /^[A-Za-z][A-Za-z0-9-]*$/

module.exports = {
  meta: {
    schema: []
  },

  create: function (context) {
    return {
      'CallExpression': function (node) {
        var object = node.callee.object
        var property = node.callee.property

        var insideByTagName = object &amp;&amp; property &amp;&amp; object.name === 'by' &amp;&amp; property.name === 'tagName'
        var argumentExists = node.arguments &amp;&amp; node.arguments.length &amp;&amp; node.arguments[0].value

        if (insideByTagName &amp;&amp; argumentExists) {
          var tagName = node.arguments[0].value
          if (!tagName.match(isTagName) || tagName.endsWith('-')) {
            context.report({
              node: node,
              message: 'Invalid TagName value: "' + tagName + '"'
            })
          }
        }
      }
    }
  }
}


Is it possible to decrease nested-ness and improve on readability? 

FYI, using standard JS code style.
The original code is fine IMO, but technically it's possible to flatten using a complete regexp

var isTagName = /^[A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])?$/


plus dubious/stinky value fallbacks and ES2015 arrow function:

  create: context =&gt; ({
    CallExpression: node =&gt; {
      if ((node.callee.object || {}).name !== 'by' ||
          (node.callee.property || {}).name !== 'tagName') {
        return
      }
      var tagName = ((node.arguments || [])[0] || {}).value
      if (tagName &amp;&amp; !tagName.match(isTagName)) {
        context.report({
          node,
          message: `Invalid tag name: "${tagName}"`
        })
      }
    }
  })