001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028
029/**
030 * <p>
031 * Checks for {@code TODO:} comments. Actually it is a generic
032 * <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html">
033 * regular expression</a> matcher on Java comments. To check for other patterns
034 * in Java comments, set the {@code format} property.
035 * </p>
036 * <p>
037 * Using {@code TODO:} comments is a great way to keep track of tasks that need to be done.
038 * Having them reported by Checkstyle makes it very hard to forget about them.
039 * </p>
040 * <ul>
041 * <li>
042 * Property {@code format} - Specify pattern to match comments against.
043 * Type is {@code java.util.regex.Pattern}.
044 * Default value is {@code "TODO:"}.
045 * </li>
046 * </ul>
047 * <p>
048 * To configure the check:
049 * </p>
050 * <pre>
051 * &lt;module name="TodoComment"/&gt;
052 * </pre>
053 * <p>
054 * Example:
055 * </p>
056 * <pre>
057 * i++; // TODO: do differently in future   // violation
058 * i++; // todo: do differently in future   // OK
059 * </pre>
060 * <p>
061 * To configure the check for comments that contain {@code TODO} and {@code FIXME}:
062 * </p>
063 * <pre>
064 * &lt;module name="TodoComment"&gt;
065 *   &lt;property name="format" value="(TODO)|(FIXME)"/&gt;
066 * &lt;/module&gt;
067 * </pre>
068 * <p>
069 * Example:
070 * </p>
071 * <pre>
072 * i++;   // TODO: do differently in future   // violation
073 * i++;   // todo: do differently in future   // OK
074 * i=i/x; // FIXME: handle x = 0 case         // violation
075 * i=i/x; // FIX :  handle x = 0 case         // OK
076 * </pre>
077 * <p>
078 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
079 * </p>
080 * <p>
081 * Violation Message Keys:
082 * </p>
083 * <ul>
084 * <li>
085 * {@code todo.match}
086 * </li>
087 * </ul>
088 *
089 * @since 3.0
090 */
091@StatelessCheck
092public class TodoCommentCheck
093        extends AbstractCheck {
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY = "todo.match";
100
101    /**
102     * Specify pattern to match comments against.
103     */
104    private Pattern format = Pattern.compile("TODO:");
105
106    @Override
107    public boolean isCommentNodesRequired() {
108        return true;
109    }
110
111    /**
112     * Setter to specify pattern to match comments against.
113     *
114     * @param pattern
115     *        pattern of 'todo' comment.
116     */
117    public void setFormat(Pattern pattern) {
118        format = pattern;
119    }
120
121    @Override
122    public int[] getDefaultTokens() {
123        return getRequiredTokens();
124    }
125
126    @Override
127    public int[] getAcceptableTokens() {
128        return getRequiredTokens();
129    }
130
131    @Override
132    public int[] getRequiredTokens() {
133        return new int[] {TokenTypes.COMMENT_CONTENT };
134    }
135
136    @Override
137    public void visitToken(DetailAST ast) {
138        final String[] lines = ast.getText().split("\n");
139
140        for (String line : lines) {
141            if (format.matcher(line).find()) {
142                log(ast, MSG_KEY, format.pattern());
143            }
144        }
145    }
146
147}