forked from phoedos/pmd
Move cpd code for ecmascript into javascript sub-module
This commit is contained in:
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Zev Blut zb@ubit.com
|
||||
*/
|
||||
public class EcmascriptLanguage extends AbstractLanguage {
|
||||
public EcmascriptLanguage() {
|
||||
super(new EcmascriptTokenizer(), ".js");
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class EcmascriptTokenizer extends AbstractTokenizer {
|
||||
public EcmascriptTokenizer() {
|
||||
// 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>();
|
||||
|
||||
// strings do not span multiple lines in javascript - the lines would need to end with backslashes
|
||||
// - which is not supported by this tokenizer
|
||||
this.spanMultipleLinesString = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EcmascriptTokenizerTest {
|
||||
|
||||
@Test
|
||||
public void test1() throws Throwable {
|
||||
Tokenizer tokenizer = new EcmascriptTokenizer();
|
||||
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 EcmascriptTokenizer();
|
||||
SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader( getCode2() ) );
|
||||
Tokens tokens = new Tokens();
|
||||
t.tokenize( sourceCode, tokens );
|
||||
assertEquals( 22, tokens.size() );
|
||||
}
|
||||
|
||||
/**
|
||||
* See: https://sourceforge.net/p/pmd/bugs/1239/
|
||||
* @throws IOException IO Exception
|
||||
*/
|
||||
@Test
|
||||
public void parseStringNotAsMultiline() throws IOException {
|
||||
Tokenizer t = new EcmascriptTokenizer();
|
||||
SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader(
|
||||
"var s = \"a string\\\n"
|
||||
+ "continues\";\n"
|
||||
+ "var s = \"a string\\\n"
|
||||
+ "continues2\";\n") );
|
||||
Tokens tokens = new Tokens();
|
||||
t.tokenize(sourceCode, tokens);
|
||||
assertEquals(13, tokens.size());
|
||||
List<TokenEntry> list = tokens.getTokens();
|
||||
assertEquals("\"a string", list.get(3).getIdentifier(), list.get(9).getIdentifier());
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user