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 * Default value is {@code "TODO:"}.
044 * </li>
045 * </ul>
046 * <p>
047 * To configure the check:
048 * </p>
049 * <pre>
050 * &lt;module name="TodoComment"/&gt;
051 * </pre>
052 * <p>
053 * To configure the check for comments that contain {@code TODO} and {@code FIXME}:
054 * </p>
055 * <pre>
056 * &lt;module name="TodoComment"&gt;
057 *   &lt;property name="format" value="(TODO)|(FIXME)"/&gt;
058 * &lt;/module&gt;
059 * </pre>
060 *
061 * @since 3.0
062 */
063@StatelessCheck
064public class TodoCommentCheck
065        extends AbstractCheck {
066
067    /**
068     * A key is pointing to the warning message text in "messages.properties"
069     * file.
070     */
071    public static final String MSG_KEY = "todo.match";
072
073    /**
074     * Specify pattern to match comments against.
075     */
076    private Pattern format = Pattern.compile("TODO:");
077
078    @Override
079    public boolean isCommentNodesRequired() {
080        return true;
081    }
082
083    /**
084     * Setter to specify pattern to match comments against.
085     * @param pattern
086     *        pattern of 'todo' comment.
087     */
088    public void setFormat(Pattern pattern) {
089        format = pattern;
090    }
091
092    @Override
093    public int[] getDefaultTokens() {
094        return getRequiredTokens();
095    }
096
097    @Override
098    public int[] getAcceptableTokens() {
099        return getRequiredTokens();
100    }
101
102    @Override
103    public int[] getRequiredTokens() {
104        return new int[] {TokenTypes.COMMENT_CONTENT };
105    }
106
107    @Override
108    public void visitToken(DetailAST ast) {
109        final String[] lines = ast.getText().split("\n");
110
111        for (int i = 0; i < lines.length; i++) {
112            if (format.matcher(lines[i]).find()) {
113                log(ast.getLineNo() + i, MSG_KEY, format.pattern());
114            }
115        }
116    }
117
118}