or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

ast-parsing.mdast-traversal.mdcopy-paste-detection.mdindex.mdlanguage-module.mdrule-development.md

index.mddocs/

0

# PMD Scala 2.12

1

2

PMD Scala 2.12 provides Scala language support for PMD, the extensible multilanguage static code analyzer. It enables parsing of Scala 2.12 source code into abstract syntax trees (AST) for static analysis, code quality checks, and copy-paste detection. The module integrates Scalameta parsers with PMD's rule engine to provide comprehensive Scala code analysis capabilities.

3

4

## Package Information

5

6

- **Package Name**: pmd-scala_2.12

7

- **Package Type**: maven

8

- **Group ID**: net.sourceforge.pmd

9

- **Language**: Java/Kotlin (analyzing Scala code)

10

- **Installation**: Add to Maven dependencies:

11

12

```xml

13

<dependency>

14

<groupId>net.sourceforge.pmd</groupId>

15

<artifactId>pmd-scala_2.12</artifactId>

16

<version>7.13.0</version>

17

</dependency>

18

```

19

20

## Core Imports

21

22

```java

23

import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;

24

import net.sourceforge.pmd.lang.scala.ScalaLanguageHandler;

25

import net.sourceforge.pmd.lang.scala.ast.ScalaParser;

26

import net.sourceforge.pmd.lang.scala.ast.ASTSource;

27

import net.sourceforge.pmd.lang.scala.ast.AbstractScalaNode;

28

import net.sourceforge.pmd.lang.scala.ast.ScalaNode;

29

import net.sourceforge.pmd.lang.scala.ast.ScalaVisitor;

30

import net.sourceforge.pmd.lang.scala.ast.ScalaTreeBuilder;

31

import net.sourceforge.pmd.lang.scala.rule.ScalaRule;

32

import net.sourceforge.pmd.lang.scala.cpd.ScalaCpdLexer;

33

import net.sourceforge.pmd.lang.scala.cpd.ScalaTokenAdapter;

34

```

35

36

## Basic Usage

37

38

```java

39

// Get the Scala language module

40

ScalaLanguageModule module = ScalaLanguageModule.getInstance();

41

42

// Parse Scala source code

43

ScalaParser parser = new ScalaParser();

44

ASTSource ast = parser.parse(parserTask);

45

46

// Create a custom Scala rule

47

public class MyScalaRule extends ScalaRule {

48

@Override

49

public RuleContext visit(ASTDefnClass node, RuleContext data) {

50

// Rule logic for class definitions

51

return super.visit(node, data);

52

}

53

}

54

```

55

56

## Architecture

57

58

The PMD Scala module follows a layered architecture:

59

60

1. **Language Integration Layer**: Core PMD integration through `ScalaLanguageModule` and `ScalaLanguageHandler`

61

2. **Parsing Layer**: Scalameta-based parsing via `ScalaParser` and `ScalaTreeBuilder`

62

3. **AST Layer**: 140+ AST node types representing all Scala language constructs

63

4. **Analysis Layer**: Visitor pattern support and rule development framework

64

5. **Copy-Paste Detection Layer**: Tokenization for duplicate code detection

65

66

## Capabilities

67

68

### Language Module Registration

69

70

Core PMD integration and language module registration for Scala versions 2.10-2.13.

71

72

```java { .api }

73

public class ScalaLanguageModule extends SimpleLanguageModuleBase {

74

public static ScalaLanguageModule getInstance();

75

public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);

76

}

77

```

78

79

[Language Module](./language-module.md)

80

81

### AST Parsing and Processing

82

83

Complete Scala source code parsing into PMD-compatible abstract syntax trees using Scalameta.

84

85

```java { .api }

86

public final class ScalaParser implements Parser {

87

public ASTSource parse(ParserTask task) throws ParseException;

88

}

89

90

public final class ASTSource extends AbstractScalaNode<Source> implements RootNode {

91

public AstInfo<ASTSource> getAstInfo();

92

}

93

```

94

95

[AST Parsing](./ast-parsing.md)

96

97

### AST Node Traversal

98

99

Comprehensive visitor pattern implementation for traversing and analyzing Scala ASTs.

100

101

```java { .api }

102

public interface ScalaVisitor<D, R> extends AstVisitor<D, R> {

103

default R visit(ScalaNode<?> node, D data);

104

default R visit(ASTSource node, D data);

105

// 140+ visit methods for all AST node types

106

}

107

108

public interface ScalaNode<T extends Tree> extends GenericNode<ScalaNode<?>> {

109

boolean isImplicit();

110

}

111

```

112

113

[AST Traversal](./ast-traversal.md)

114

115

### Rule Development

116

117

Framework for creating custom static analysis rules for Scala code.

118

119

```java { .api }

120

public class ScalaRule extends AbstractRule implements ScalaVisitor<RuleContext, RuleContext> {

121

public void apply(Node target, RuleContext ctx);

122

public RuleContext visitNode(Node node, RuleContext param);

123

}

124

```

125

126

[Rule Development](./rule-development.md)

127

128

### Copy-Paste Detection

129

130

Scalameta-based tokenization for detecting code duplication in Scala projects.

131

132

```java { .api }

133

public class ScalaCpdLexer implements CpdLexer {

134

public ScalaCpdLexer(LanguagePropertyBundle bundle);

135

public void tokenize(TextDocument document, TokenFactory tokenEntries);

136

}

137

```

138

139

[Copy-Paste Detection](./copy-paste-detection.md)

140

141

## Types

142

143

### Core Types

144

145

```java { .api }

146

// Language integration

147

class ScalaLanguageModule extends SimpleLanguageModuleBase

148

class ScalaLanguageHandler extends AbstractPmdLanguageVersionHandler

149

150

// Parsing

151

class ScalaParser implements Parser

152

class ScalaTreeBuilder // Internal tree building

153

class ASTSource extends AbstractScalaNode<Source> implements RootNode

154

155

// AST base types

156

abstract class AbstractScalaNode<T extends Tree> extends AbstractNode<AbstractScalaNode<?>, ScalaNode<?>> implements ScalaNode<T>

157

interface ScalaNode<T extends Tree> extends GenericNode<ScalaNode<?>>

158

interface ScalaVisitor<D, R> extends AstVisitor<D, R>

159

class ScalaVisitorBase implements ScalaVisitor<Object, Object>

160

161

// Rule development

162

class ScalaRule extends AbstractRule implements ScalaVisitor<RuleContext, RuleContext>

163

164

// Copy-paste detection

165

class ScalaCpdLexer implements CpdLexer

166

class ScalaTokenAdapter // Token adapter for PMD integration

167

168

// Internal utilities

169

class ScalaDialect // Maps language versions to Scalameta dialects

170

```

171

172

### Supported Scala Versions

173

174

- Scala 2.10

175

- Scala 2.11

176

- Scala 2.12 (this module's target version)

177

- Scala 2.13

178

179

### Integration Dependencies

180

181

- **PMD Core**: `net.sourceforge.pmd:pmd-core` - Core PMD framework

182

- **Scalameta**: `org.scalameta:parsers_2.12` and `org.scalameta:trees_2.12` - Scala parsing

183

- **Scala Runtime**: `org.scala-lang:scala-library:2.12.20` - Scala standard library