/* eslint-env node */
'use strict';

const Rule = require('./base');

/*
  Don't allow dot(s) at the end of a Path expression.

  Bad

  {{contact.contact_name.}}

  {{#if contact.contact_name.}}
    {{displayName}}
  {{/if}}

  {{if contact.contact_name..}}
    {{displayName}}
  {{/if}}


  Good

  {{contact.contact_name}}

  {{#if contact.contact_name}}
    {{displayName}}
  {{/if}}

  {{if contact.contact_name}}
    {{displayName}}
  {{/if}}
*/

module.exports = class NoTrailingDotOnPathExpression extends Rule {

  findPathExpressionWithTrailingDot(node) {
    return node.params.find((param) => param.type === 'PathExpression' && param.original.endsWith('.')) ||
    node.hash.pairs.find((param) => param.type === 'PathExpression' && param.original.endsWith('.'));
  }

  visitor() {
    return {
      MustacheStatement(node) {
        let pathExpression = this.findPathExpressionWithTrailingDot(node);
        if (pathExpression) {
          let source = this.sourceForNode(node);
          this.log({
            message: 'A MustacheStatement should not have a Path Expression with a trialing dot.',
            line: pathExpression.loc && pathExpression.loc.start.line,
            column: pathExpression.loc && pathExpression.loc.start.column,
            source
          });
        }
      },

      SubExpression(node) {
        let pathExpression = this.findPathExpressionWithTrailingDot(node);
        if (pathExpression) {
          let source = this.sourceForNode(node);
          this.log({
            message: 'A SubExpression should not have a Path Expression with a trialing dot.',
            line: pathExpression.loc && pathExpression.loc.start.line,
            column: pathExpression.loc && pathExpression.loc.start.column,
            source
          });
        }
      },

      BlockStatement(node) {
        let pathExpression = this.findPathExpressionWithTrailingDot(node);
        if (pathExpression) {
          // Since we are processing only the hash pairs and params, its enough to display the open-invocation.
          let sourceLoc = {
            start: node.loc.start,
            end: node.program.loc.start
          };
          let source = this.sourceForNode({ loc: sourceLoc });
          this.log({
            message: 'A BlockStatement should not have a Path Expression with a trialing dot.',
            line: pathExpression.loc && pathExpression.loc.start.line,
            column: pathExpression.loc && pathExpression.loc.start.column,
            source
          });
        }
      }
    };
  }
};

@@ -518,2038 +518,150 @@
 e %7B%0A
-%0A
   
-findPathExpressionWithTrailingDot(node) %7B%0A    return node.params.find((param) =%3E param.type === 'PathExpression' && param.original.endsWith('.')) %7C%7C%0A    node.hash.pairs.find((param) =%3E param.type === 'PathExpression' && param.original.endsWith('.'));%0A  %7D%0A%0A  visitor() %7B%0A    return %7B%0A      MustacheStatement(node) %7B%0A        let pathExpression = this.findPathExpressionWithTrailingDot(node);%0A        if (pathExpression) %7B%0A          let source = this.sourceForNode(node);%0A          this.log(%7B%0A            message: 'A MustacheStatement should not have a Path Expression with a trialing dot.',%0A            line: pathExpression.loc && pathExpression.loc.start.line,%0A            column: pathExpression.loc && pathExpression.loc.start.column,%0A            source%0A          %7D);%0A        %7D%0A      %7D,%0A%0A      SubExpression(node) %7B%0A        let pathExpression = this.findPathExpressionWithTrailingDot(node);%0A        if (pathExpression) %7B%0A          let source = this.sourceForNode(node);%0A          this.log(%7B%0A            message: 'A SubExpression should not have a Path Expression with a trialing dot.',%0A            line: pathExpression.loc && pathExpression.loc.start.line,%0A            column: pathExpression.loc && pathExpression.loc.start.column,%0A            source%0A          %7D);%0A        %7D%0A      %7D,%0A%0A      BlockStatement(node) %7B%0A        let pathExpression = this.findPathExpressionWithTrailingDot(node);%0A        if (pathExpression) %7B%0A          // Since we are processing only the hash pairs and params, its enough to display the open-invocation.%0A          let sourceLoc = %7B%0A            start: node.loc.start,%0A            end: node.program.loc.start%0A          %7D;%0A          let source = this.sourceForNode(%7B loc: sourceLoc %7D);%0A          this.log(%7B%0A            message: 'A BlockStatement should not have a Path Expression with a trialing dot.',%0A            line: pathExpression.loc && pathExpression.loc.start.line,%0A            column: pathExpression.loc && pathExpression.loc.start.column,%0A            source%0A          %7D);%0A        %7D%0A      %7D%0A   
+// glimmer-vm now asserts on this condition, this rule will be removed during%0A  // the next major version bump...%0A  visitor() %7B%0A    return %7B
  %7D;%0A

Remove rule implementation (it is no longer needed).