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:
parent
cc3f172ef2
commit
38044fb7f5
@ -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 );
|
||||
}
|
||||
}
|
11
pmd/src/net/sourceforge/pmd/cpd/JavascriptLanguage.java
Normal file
11
pmd/src/net/sourceforge/pmd/cpd/JavascriptLanguage.java
Normal 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");
|
||||
}
|
||||
}
|
22
pmd/src/net/sourceforge/pmd/cpd/JavascriptTokenizer.java
Normal file
22
pmd/src/net/sourceforge/pmd/cpd/JavascriptTokenizer.java
Normal 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>();
|
||||
}
|
||||
}
|
@ -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";
|
||||
|
Loading…
x
Reference in New Issue
Block a user