diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 5a422598d..5c1b306a1 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -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); @@ -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); @@ -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); @@ -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); @@ -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, @@ -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 }"### @@ -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, @@ -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 }"### @@ -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(code: &str, path: &str) { + check_metrics::(code, path, |metric| { + assert_eq!(metric.loc.lloc(), 8.0); + }); + } + + check::(code, "foo.js"); + check::(code, "foo.js"); + check::(code, "foo.ts"); + check::(code, "foo.tsx"); + } #[test] fn cpp_namespace_loc() { check_metrics::( diff --git a/tests/pdf_js_test.rs b/tests/pdf_js_test.rs index 9b6404819..48a9b15c6 100644 --- a/tests/pdf_js_test.rs +++ b/tests/pdf_js_test.rs @@ -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"], &[]); } diff --git a/tests/repositories/rca-output b/tests/repositories/rca-output index f00d7c7bf..46ebefbd3 160000 --- a/tests/repositories/rca-output +++ b/tests/repositories/rca-output @@ -1 +1 @@ -Subproject commit f00d7c7bfb24184b930eb088b43ea03324c60341 +Subproject commit 46ebefbd392bed958af39e63ca793655e3bf8f67