Added javascript as a supported language for CPD.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@7019 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Dale Anson 2009-12-09 02:22:56 +00:00
parent cc3f172ef2
commit 38044fb7f5
4 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,64 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package test.net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.cpd.JavascriptTokenizer;
import net.sourceforge.pmd.cpd.SourceCode;
import net.sourceforge.pmd.cpd.Tokenizer;
import net.sourceforge.pmd.cpd.Tokens;
import org.junit.Test;
public class JavascriptTokenizerTest {
@Test
public void test1() throws Throwable {
Tokenizer tokenizer = new JavascriptTokenizer();
SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader( getCode1() ) );
Tokens tokens = new Tokens();
tokenizer.tokenize( sourceCode, tokens );
assertEquals( 22, tokens.size() );
}
@Test
public void test2() throws Throwable {
Tokenizer t = new JavascriptTokenizer();
SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader( getCode2() ) );
Tokens tokens = new Tokens();
t.tokenize( sourceCode, tokens );
assertEquals( 22, tokens.size() );
}
// no semi-colons
private String getCode1() {
StringBuilder sb = new StringBuilder();
sb.append( "function switchToRealPassword() {" ).append(PMD.EOL);
sb.append( " var real = $('realPass')" ).append(PMD.EOL);
sb.append( " var prompt = $('promptPass')" ).append(PMD.EOL);
sb.append( " real.style.display = 'inline'" ).append(PMD.EOL);
sb.append( " prompt.style.display = 'none'" ).append(PMD.EOL);
sb.append( " real.focus()" ).append(PMD.EOL);
sb.append( "}" ).append(PMD.EOL);
return sb.toString();
}
// same as getCode1, but lines are ended with semi-colons
private String getCode2() {
StringBuilder sb = new StringBuilder();
sb.append( "function switchToRealPassword() {" ).append(PMD.EOL);
sb.append( " var real = $('realPass');" ).append(PMD.EOL);
sb.append( " var prompt = $('promptPass');" ).append(PMD.EOL);
sb.append( " real.style.display = 'inline';" ).append(PMD.EOL);
sb.append( " prompt.style.display = 'none';" ).append(PMD.EOL);
sb.append( " real.focus();" ).append(PMD.EOL);
sb.append( "}" ).append(PMD.EOL);
return sb.toString();
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter( JavascriptTokenizerTest.class );
}
}

View File

@ -0,0 +1,11 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
* @author Zev Blut zb@ubit.com
*/
package net.sourceforge.pmd.cpd;
public class JavascriptLanguage extends AbstractLanguage {
public JavascriptLanguage() {
super(new JavascriptTokenizer(), ".js");
}
}

View File

@ -0,0 +1,22 @@
package net.sourceforge.pmd.cpd;
import java.util.ArrayList;
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
public class JavascriptTokenizer extends AbstractTokenizer {
public JavascriptTokenizer() {
// setting markers for "string" in javascript
this.stringToken = new ArrayList<String>();
this.stringToken.add( "\'" );
this.stringToken.add( "\"" );
// setting markers for 'ignorable character' in javascript
this.ignorableCharacter = new ArrayList<String>();
this.ignorableCharacter.add( ";" );
// setting markers for 'ignorable string' in javascript
this.ignorableStmt = new ArrayList<String>();
}
}

View File

@ -7,7 +7,7 @@ import java.util.Properties;
public class LanguageFactory {
public static String[] supportedLanguages = new String[]{"java", "jsp", "cpp", "c", "php", "ruby","fortran" };
public static String[] supportedLanguages = new String[]{"java", "jsp", "cpp", "c", "php", "ruby","fortran", "javascript" };
private static final String SUFFIX = "Language";
public static final String EXTENSION = "extension";
public static final String BY_EXTENSION = "by_extension";