/*
 * Sonar Puppet Plugin
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Iain Adams
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.iadams.sonarqube.puppet.checks;

import com.google.common.collect.Lists;
import com.iadams.sonarqube.puppet.api.PuppetGrammar;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;

import java.util.List;

import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
import org.sonar.squidbridge.checks.SquidCheck;

@Rule(
  key = "ResourceWithSelector",
  priority = Priority.MINOR,
  name = "Conditionals should not be intermingled with resource declarations",
  tags = {Tags.PITFALL, Tags.BRAIN_OVERLOAD})
@ActivatedByDefault
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.READABILITY)
@SqaleConstantRemediation("10min")
public class ResourceWithSelectorCheck extends SquidCheck<Grammar> {

  @Override
  public void init() {
    subscribeTo(PuppetGrammar.RESOURCE);
  }

  @Override
  public void visitNode(AstNode node) {

    List<AstNode> selectors = Lists.newArrayList();

    for (AstNode selector : node.getDescendants(PuppetGrammar.SELECTOR)) {
      selectors.add(selector);
    }
    for (AstNode violation : selectors) {
      getContext().createLineViolation(this, "Extract this conditional from the resource declaration.", violation);
    }
  }
}

@@ -1208,48 +1208,8 @@
 s;%0A%0A
-import com.google.common.collect.Lists;%0A
 impo
@@ -1332,33 +1332,8 @@
 ar;%0A
-%0Aimport java.util.List;%0A%0A
 impo
@@ -2209,62 +2209,8 @@
 ) %7B%0A
-%0A    List%3CAstNode%3E selectors = Lists.newArrayList();%0A%0A
     
@@ -2284,87 +2284,8 @@
 ) %7B%0A
-      selectors.add(selector);%0A    %7D%0A    for (AstNode violation : selectors) %7B%0A
     
@@ -2388,17 +2388,16 @@
 .%22, 
-violation
+selector
 );%0A 
@@ -2401,14 +2401,15 @@
 ;%0A    %7D%0A
-
   %7D%0A
+%0A
 %7D%0A

Simplify code