Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 64 additions & 16 deletions src/metrics/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,17 @@ impl Loc for MozjsCode {
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
| DebuggerStatement | ReturnStatement | ThrowStatement | StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
EmptyStatement => {
// An empty initializer or condition in a `for` header is an
// `empty_statement`, while a non-empty one is a bare expression:
// count neither, the `for` statement is already counted.
if !node.parent().is_some_and(|p| p.kind_id() == ForStatement) {
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
Expand All @@ -655,10 +662,17 @@ impl Loc for JavascriptCode {
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
| DebuggerStatement | ReturnStatement | ThrowStatement | StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
EmptyStatement => {
// An empty initializer or condition in a `for` header is an
// `empty_statement`, while a non-empty one is a bare expression:
// count neither, the `for` statement is already counted.
if !node.parent().is_some_and(|p| p.kind_id() == ForStatement) {
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
Expand All @@ -681,10 +695,17 @@ impl Loc for TypescriptCode {
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
| DebuggerStatement | ReturnStatement | ThrowStatement | StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
EmptyStatement => {
// An empty initializer or condition in a `for` header is an
// `empty_statement`, while a non-empty one is a bare expression:
// count neither, the `for` statement is already counted.
if !node.parent().is_some_and(|p| p.kind_id() == ForStatement) {
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
Expand All @@ -707,10 +728,17 @@ impl Loc for TsxCode {
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
| DebuggerStatement | ReturnStatement | ThrowStatement | StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
EmptyStatement => {
// An empty initializer or condition in a `for` header is an
// `empty_statement`, while a non-empty one is a bare expression:
// count neither, the `for` statement is already counted.
if !node.parent().is_some_and(|p| p.kind_id() == ForStatement) {
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
Expand Down Expand Up @@ -2549,12 +2577,12 @@ mod tests {
{
"sloc": 5.0,
"ploc": 5.0,
"lloc": 6.0,
"lloc": 5.0,
"cloc": 0.0,
"blank": 0.0,
"sloc_average": 2.5,
"ploc_average": 2.5,
"lloc_average": 3.0,
"lloc_average": 2.5,
"cloc_average": 0.0,
"blank_average": 0.0,
"sloc_min": 5.0,
Expand All @@ -2563,8 +2591,8 @@ mod tests {
"cloc_max": 0.0,
"ploc_min": 5.0,
"ploc_max": 5.0,
"lloc_min": 5.0,
"lloc_max": 5.0,
"lloc_min": 4.0,
"lloc_max": 4.0,
"blank_min": 0.0,
"blank_max": 0.0
}"###
Expand All @@ -2590,12 +2618,12 @@ mod tests {
{
"sloc": 5.0,
"ploc": 5.0,
"lloc": 6.0,
"lloc": 5.0,
"cloc": 0.0,
"blank": 0.0,
"sloc_average": 2.5,
"ploc_average": 2.5,
"lloc_average": 3.0,
"lloc_average": 2.5,
"cloc_average": 0.0,
"blank_average": 0.0,
"sloc_min": 5.0,
Expand All @@ -2604,8 +2632,8 @@ mod tests {
"cloc_max": 0.0,
"ploc_min": 5.0,
"ploc_max": 5.0,
"lloc_min": 5.0,
"lloc_max": 5.0,
"lloc_min": 4.0,
"lloc_max": 4.0,
"blank_min": 0.0,
"blank_max": 0.0
}"###
Expand All @@ -2614,6 +2642,26 @@ mod tests {
);
}

#[test]
fn js_for_header_loc() {
// The initializer and the condition of a `for` header are not logical
// lines on their own, whether they are empty or not.
let code = "for (i = 0; i < 4; ++i) {} // +2 (for, block)
for (; i < 4; ++i) {} // +2
for (i = 0; ; ++i) {} // +2
for (;;) {} // +2";

fn check<T: ParserTrait>(code: &str, path: &str) {
check_metrics::<T>(code, path, |metric| {
assert_eq!(metric.loc.lloc(), 8.0);
});
}

check::<JavascriptParser>(code, "foo.js");
check::<MozjsParser>(code, "foo.js");
check::<TypescriptParser>(code, "foo.ts");
check::<TsxParser>(code, "foo.tsx");
}
#[test]
fn cpp_namespace_loc() {
check_metrics::<CppParser>(
Expand Down
125 changes: 1 addition & 124 deletions tests/pdf_js_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,5 @@ use common::compare_rca_output_with_files;

#[test]
fn test_pdfjs() {
// FIXME: Ignoring these files temporarily due to a parse error (see issue: https://github.com/mozilla/rust-code-analysis/issues/1143),
// in order to allow CI to pass until the issue is resolved.
let exclude = &[
"**/pdf.js/extensions/firefox/tools/l10n.js",
"**/pdf.js/src/display/optional_content_config.js",
"**/pdf.js/docs/contents/js/bootstrap.min.js",
"**/pdf.js/src/display/font_loader.js",
"**/pdf.js/src/display/content_disposition.js",
"**/pdf.js/src/display/pattern_helper.js",
"**/pdf.js/src/display/canvas.js",
"**/pdf.js/src/display/annotation_layer.js",
"**/pdf.js/src/display/svg.js",
"**/pdf.js/docs/contents/js/jquery-2.1.0.min.js",
"**/pdf.js/src/display/display_utils.js",
"**/pdf.js/src/display/editor/ink.js",
"**/pdf.js/src/display/text_layer.js",
"**/pdf.js/src/display/api.js",
"**/pdf.js/src/core/fonts_utils.js",
"**/pdf.js/src/core/primitives.js",
"**/pdf.js/src/core/jpg.js",
"**/pdf.js/src/core/jbig2_stream.js",
"**/pdf.js/src/core/colorspace.js",
"**/pdf.js/src/core/worker.js",
"**/pdf.js/src/core/parser.js",
"**/pdf.js/src/core/annotation.js",
"**/pdf.js/src/core/evaluator.js",
"**/pdf.js/src/core/function.js",
"**/pdf.js/src/core/jpx_stream.js",
"**/pdf.js/src/core/predictor_stream.js",
"**/pdf.js/src/core/run_length_stream.js",
"**/pdf.js/src/core/image.js",
"**/pdf.js/src/core/writer.js",
"**/pdf.js/src/core/operator_list.js",
"**/pdf.js/src/core/jbig2.js",
"**/pdf.js/src/core/jpx.js",
"**/pdf.js/src/core/document.js",
"**/pdf.js/src/core/name_number_tree.js",
"**/pdf.js/src/core/cff_parser.js",
"**/pdf.js/src/core/core_utils.js",
"**/pdf.js/src/core/metadata_parser.js",
"**/pdf.js/src/core/pattern.js",
"**/pdf.js/src/core/fonts.js",
"**/pdf.js/src/core/ccitt.js",
"**/pdf.js/src/core/cff_font.js",
"**/pdf.js/src/core/jpeg_stream.js",
"**/pdf.js/src/core/struct_tree.js",
"**/pdf.js/src/core/font_renderer.js",
"**/pdf.js/src/core/glyf.js",
"**/pdf.js/src/core/to_unicode_map.js",
"**/pdf.js/src/core/cmap.js",
"**/pdf.js/src/core/crypto.js",
"**/pdf.js/src/core/type1_parser.js",
"**/pdf.js/src/core/bidi.js",
"**/pdf.js/src/core/chunked_stream.js",
"**/pdf.js/src/core/type1_font.js",
"**/pdf.js/src/core/opentype_file_builder.js",
"**/pdf.js/src/core/catalog.js",
"**/pdf.js/src/core/xref.js",
"**/pdf.js/src/core/xml_parser.js",
"**/pdf.js/src/core/flate_stream.js",
"**/pdf.js/src/core/xfa/bind.js",
"**/pdf.js/src/core/unicode.js",
"**/pdf.js/src/core/xfa/text.js",
"**/pdf.js/src/core/xfa/config.js",
"**/pdf.js/src/core/xfa/som.js",
"**/pdf.js/src/core/image_resizer.js",
"**/pdf.js/src/core/xfa/xfa_object.js",
"**/pdf.js/src/core/ascii_85_stream.js",
"**/pdf.js/src/core/lzw_stream.js",
"**/pdf.js/src/core/xfa/template.js",
"**/pdf.js/src/scripting_api/field.js",
"**/pdf.js/src/shared/fonts_utils.js",
"**/pdf.js/src/scripting_api/aform.js",
"**/pdf.js/src/shared/image_utils.js",
"**/pdf.js/src/shared/murmurhash3.js",
"**/pdf.js/src/shared/util.js",
"**/pdf.js/test/stats/statcmp.js",
"**/pdf.js/test/chromium/test-telemetry.js",
"**/pdf.js/test/test.js",
"**/pdf.js/test/integration/annotation_spec.js",
"**/pdf.js/test/integration/freetext_editor_spec.js",
"**/pdf.js/test/integration/ink_editor_spec.js",
"**/pdf.js/test/resources/reftest-analyzer.js",
"**/pdf.js/test/driver.js",
"**/pdf.js/test/unit/crypto_spec.js",
"**/pdf.js/test/integration/scripting_spec.js",
"**/pdf.js/test/unit/ui_utils_spec.js",
"**/pdf.js/test/unit/util_spec.js",
"**/pdf.js/test/unit/struct_tree_spec.js",
"**/pdf.js/test/unit/parser_spec.js",
"**/pdf.js/test/unit/cff_parser_spec.js",
"**/pdf.js/test/unit/annotation_spec.js",
"**/pdf.js/test/unit/pdf_viewer_spec.js",
"**/pdf.js/test/unit/pdf_find_controller_spec.js",
"**/pdf.js/test/unit/scripting_spec.js",
"**/pdf.js/examples/image_decoders/jpeg_viewer.js",
"**/pdf.js/examples/node/pdf2svg.js",
"**/pdf.js/examples/node/getinfo.js",
"**/pdf.js/external/dist/webpack.js",
"**/pdf.js/external/webL10n/l10n.js",
"**/pdf.js/gulpfile.js",
"**/pdf.js/test/unit/api_spec.js",
"**/pdf.js/external/cmapscompress/optimize.js",
"**/pdf.js/external/cmapscompress/compress.js",
"**/pdf.js/web/firefox_print_service.js",
"**/pdf.js/external/quickjs/quickjs-eval.js",
"**/pdf.js/web/pdf_rendering_queue.js",
"**/pdf.js/web/ui_utils.js",
"**/pdf.js/web/event_utils.js",
"**/pdf.js/web/debugger.js",
"**/pdf.js/web/app.js",
"**/pdf.js/web/pdf_page_view.js",
"**/pdf.js/web/pdf_find_controller.js",
"**/pdf.js/web/pdf_outline_viewer.js",
"**/pdf.js/web/pdf_link_service.js",
"**/pdf.js/web/pdf_thumbnail_viewer.js",
"**/pdf.js/web/pdf_viewer.js",
"**/pdf.js/web/text_highlighter.js",
"**/pdf.js/web/view_history.js",
"**/pdf.js/web/pdf_history.js",
"**/pdf.js/web/pdf_sidebar.js",
];

compare_rca_output_with_files("pdf.js", &["*.js"], exclude);
compare_rca_output_with_files("pdf.js", &["*.js"], &[]);
}
2 changes: 1 addition & 1 deletion tests/repositories/rca-output
Submodule rca-output updated 118 files