How to add your own rule

How to build your own rule?

First you will need to get your environment up and running. To do this, please refer to the Flex PMD Developer Documentation .

To add a rule, all you will need to do is modify the flex-pmd-ruleset project.
If you need to add a new resource in order to test your rule, please add it to the flex-pmd-test-resources/src/test/resources folder. Your resource will be detected in the flex-pmd-ruleset project automatically.

How to add a new Rule in the AcRuleSet

You will have to ask yourself two questions before creating a new rule:

  • Will my rule be about ActionScript files, MXML files or both?
  • Can my rule be detected only by a regular expression?
  • Do you have to visit the Abstract Syntax Tree (AST) to detect the violation?
  • Do you need a different technique to detect the violation?

If you answered yes to the second question, you are lucky: your rule will be quick and simple to implement.
Depending on your answer to the first question, you should create your rule and test in the correct package and possibly sub-packages, from the following:

  • com.adobe.ac.pmd.rules.as3
  • com.adobe.ac.pmd.rules.mxml
  • com.adobe.ac.pmd.rules.common

A violation can be detected by a regular expression:

  1. Create a new test case for testing your rule. Make this test case extending AbstractTestRegExpBasedRule.
  2. Implements the required abstract methods.
  3. Create a new class with a name that ends in Rule, and which extends AbstractRegexpBasedRule: You will have to create a constructor and to implement four abstract methods.
  4. Specify the default violation priority.
  5. Specify whether the current rule is concerned by the given file (typically "file.isMxml()" or "file.getClassName().endsWith( 'Event.as' )" )
  6. Specify the regular expression to match the line which might contain a violation. The Java regexp is slightly different: your regexp has to match the entire line (by adding .* at the beginning and at the end). You can also use regexp groups if you need to (see TooLongBindingExpressionRule for an example).
  7. Specify when to add a violation if the regexp matches the current line (see TooLongBindingExpressionRule for an example).
  8. Run the unit-tests for the flex-pmd-ruleset project

A violation cannot be detected by a regular expression but can be by visiting the AST:


| | Warning

Only AS script block can be concerned by an AST rule. The inline AS code in a mxml tag like (onChange="codeWithViolationsHere()") is not parsed by FlexPMD. However, it is good practice to minimize the code inside Script blocks, and inside XML tags within MXML components. |
||

  1. Create a new test case for testing your rule. Make this test case extending AbstractTestASTRule.
  2. Implements the required abstract methods.
  3. Create a new class whose name is ending by Rule, and which extends AbstractASTRule: You will have to create a constructor and to implement four abstract methods
  4. Specify the default violation priority.
  5. Specify whether the current rule is concerned by the given file (typically "file.isMxml()" or "file.getClassName().endsWith( 'Event.as' )" )
  6. if the violation can be computed from the package node, you can override the findViolationsFromPackageNode method.
  7. Otherwise override any visitX methods and detect your violation according to the current node content (see AvoidInstantiationInLoop, or EmptyIfStmt rules)
  8. Run the unit-tests for the flex-pmd-ruleset project

A violation cannot be detected by either a regexp or a AST visit:

  1. Create a new test case for testing your rule. Make this test case extending AbstractTestFlexRule.
  2. Implements the required abstract methods.
  3. Specify the default violation priority.
  4. Create a new class whose name is ending by Rule, and which extends AbstractFlexRule: You will have to create a constructor and to implement two abstract methods
  5. Implements the processFile method on your own.
  6. Run the unit-tests for the flex-pmd-ruleset project

Last step

Only when all your tests pass, add a reference of your rule to the RulesSet description which is in
/flex-pmd-ruleset/src/main/resources/com/adobe/ac/pmd/rulesets

  1. Update the relevant ruleset to include you newly created rule.
  2. Run the unit-tests for the flex-pmd-ruleset-core project.

Check the code in if you are entitled to do so, otherwise submit a patch for review and later inclusion.

Before committing

Run Java PMD in FlexPMD project in order to make sure that its code is free from violations using the eclipse plugin.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks