Skip to content

Commit

Permalink
Added XmlLiteralChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfarwell committed Jan 8, 2015
1 parent 924d8af commit d1d0dcb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,7 @@ field.name.label = "Field name"
field.name.description = "Check that field names match a regular expression"
field.name.regex.label = "Regular expression"
field.name.regex.description = "The field names must match this regular expression"

xml.literal.message = "Avoid xml literals"
xml.literal.label = "XML literals"
xml.literal.description = "Check that XML literals are not used"
11 changes: 6 additions & 5 deletions src/main/resources/scalastyle_definition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@
<parameter name="tabSize" type="integer" default="2" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.FieldNamesChecker" id="field.name" defaultLevel="warning">
<parameters>
<parameter name="regex" type="string" default="^[a-z][A-Za-z0-9]*$" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.FieldNamesChecker" id="field.name" defaultLevel="warning">
<parameters>
<parameter name="regex" type="string" default="^[a-z][A-Za-z0-9]*$" />
</parameters>
</checker>
<checker class="org.scalastyle.scalariform.XmlLiteralChecker" id="xml.literal" defaultLevel="warning"/>
</scalastyle-definition>
33 changes: 33 additions & 0 deletions src/main/scala/org/scalastyle/scalariform/XmlLiteralChecker.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2011-2012 the original author or authors.
// See the LICENCE.txt file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.scalastyle.scalariform;

import org.scalastyle.PositionError
import org.scalastyle.ScalariformChecker
import org.scalastyle.ScalastyleError
import VisitorHelper.visit
import scalariform.parser.CompilationUnit
import scalariform.parser.Refinement
import scalariform.parser.XmlExpr

class XmlLiteralChecker extends ScalariformChecker {
val errorKey = "xml.literal"

final def verify(ast: CompilationUnit): List[ScalastyleError] = {
VisitorHelper.getAll[XmlExpr](ast.immediateChildren(0)).map(t => PositionError(t.firstToken.offset))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (C) 2011-2012 the original author or authors.
// See the LICENCE.txt file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.scalastyle.scalariform

import org.scalatest.junit.AssertionsForJUnit
import org.junit.Test
import org.scalastyle.file.CheckerTest

// scalastyle:off magic.number

class XmlLiteralCheckerTest extends AssertionsForJUnit with CheckerTest {

protected val classUnderTest = classOf[XmlLiteralChecker]

protected val key = "xml.literal"

@Test def testZeroErrors(): Unit = {
val source = """
class C1 {
def m1(n: Int) = n
}
""";
assertErrors(List(), source)
}

@Test def testOneError(): Unit = {
val source = """
class C1 {
val f = <foobar/>
}
"""
assertErrors(List(columnError(3, 10)), source)
}

@Test def testTwoErrors(): Unit = {
val source = """
class C1 {
val f1 = <foobar/>
val f2 = <foobar/>
}
"""
assertErrors(List(columnError(3, 11), columnError(4, 11)), source)
}

@Test def testMultiLine(): Unit = {
val source = """
class C1 {
val f = <foobar>
<foo>&gt;</foo>
</foobar>
}
"""
assertErrors(List(columnError(3, 10)), source)
}
}

0 comments on commit d1d0dcb

Please sign in to comment.