Contenu connexe Similaire à Scala Code Analysis at Codacy (20) Scala Code Analysis at Codacy2. intro
‣ static code analysis
‣ supporting various languages via open source tools
‣ in-house scala engine
4. naming conventions / keywords
‣ AST: Abstract syntax tree - syntactic structure of program
‣ Pattern: AST => Set of code-positions containing issues
‣ quasiquote: templates that allow matching trees
7. universal ast
‣ unifying various languages
generic patterns
patterns only in theory useful
no language specific information
11. var utils = CodacyEngine.getInstance();
var astUtils = ScalaAstOps.getInstance();
var hasDeferredImpl = function(element){
return _.isArray(element.templ.body) &&
_.some(element.templ.body, function(bodyElem){
return astUtils.hasFlag(bodyElem,"deferred");
});
};
var isStructuralTypeDef = function(element){
return astUtils.isCompoundTypeTree(element) &&
hasDeferredImpl(element);
};
var getElements = function(element){
return astUtils.allInTreeWhere(element, isStructuralTypeDef);
};
utils.analyse = function(ast){
_.map(
ast.fileNodes,
function(fileNode){
_.map(
getElements(fileNode),
function (nestedIfElement) {
utils.addWarning(fileNode.filename,
nestedIfElement.line, nestedIfElement.line,
"Avoid using structural types");
}
);
}
);
return utils.finalResult();
};
ClassDef(
Modifiers(
ABSTRACT
),
TypeName(
"Foo"
),
List(),
Template(
List(
Select(
Ident(
scala
),
TypeName(
"AnyRef"
)
)
),
noSelfType,
List(
DefDef(
Modifiers(),
termNames.CONSTRUCTOR,
List(),
List(
List( )
),
TypeTree(),
Block(
List(
pendingSuperCall
),
Literal(
Constant(
( )
)
)
)
),
TypeDef(
Modifiers(),
TypeName(
…
abstract class Foo{
type BarType = AnyRef { def bar:String }
}
scala test code
javascript pattern code
scala.reflect AST
13. scala.meta + quasiquotes
‣ "make metaprogramming easy”
‣ scala.meta parses sources
‣ patterns written using quasiquotes
‣ > 80 patterns implemented
14. scala.meta + quasiquotes
easy to write patterns (scala patterns for scala code)
runs faster (no javascript indirection)
no client-side pattern execution
17. example: ===
‣ 1 == “1” should not compile
‣ find all occurrences of ==
‣ emit a warning and suggest using === instead
19. links
‣ codacy: https://codacy.com/
‣ scala.meta: http://scalameta.org/
‣ scala.meta presentation: https://www.youtube.com/watch?v=RBjZ7OxFxxg
‣ quasiquotes: https://github.com/scalameta/scalameta/blob/master/docs/quasiquotes.md