From ace502f3c00427fd1a356aa3363ef2bcc1c7ac11 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Thu, 5 Aug 2021 18:09:12 -0700 Subject: [PATCH] Fix: Code path analysis for class fields (fixes #14343) --- .../class-field--conditional.js | 20 +++++++++++++++++++ .../object-literal--conditional.js | 20 +++++++++++++++++++ .../code-path-analysis/code-path-analyzer.js | 4 ++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/code-path-analysis/class-field--conditional.js create mode 100644 tests/fixtures/code-path-analysis/object-literal--conditional.js diff --git a/tests/fixtures/code-path-analysis/class-field--conditional.js b/tests/fixtures/code-path-analysis/class-field--conditional.js new file mode 100644 index 00000000000..e39be7ee065 --- /dev/null +++ b/tests/fixtures/code-path-analysis/class-field--conditional.js @@ -0,0 +1,20 @@ +/*expected +initial->s1_1->s1_2->s1_4; +s1_1->s1_3->s1_4->final; +*/ + +class x { a = b ? c : d } + +/*DOT +digraph { + node[shape=box,style="rounded,filled",fillcolor=white]; + initial[label="",shape=circle,style=filled,fillcolor=black,width=0.25,height=0.25]; + final[label="",shape=doublecircle,style=filled,fillcolor=black,width=0.25,height=0.25]; + s1_1[label="Program:enter\nClassDeclaration:enter\nIdentifier (x)\nClassBody:enter\nPropertyDefinition:enter\nIdentifier (a)\nConditionalExpression:enter\nIdentifier (b)"]; + s1_2[label="Identifier (c)"]; + s1_4[label="ConditionalExpression:exit\nPropertyDefinition:exit\nClassBody:exit\nClassDeclaration:exit\nProgram:exit"]; + s1_3[label="Identifier (d)"]; + initial->s1_1->s1_2->s1_4; + s1_1->s1_3->s1_4->final; +} +*/ diff --git a/tests/fixtures/code-path-analysis/object-literal--conditional.js b/tests/fixtures/code-path-analysis/object-literal--conditional.js new file mode 100644 index 00000000000..c5a4145a8be --- /dev/null +++ b/tests/fixtures/code-path-analysis/object-literal--conditional.js @@ -0,0 +1,20 @@ +/*expected +initial->s1_1->s1_2->s1_4; +s1_1->s1_3->s1_4->final; +*/ + +x = { a: b ? c : d } + +/*DOT +digraph { + node[shape=box,style="rounded,filled",fillcolor=white]; + initial[label="",shape=circle,style=filled,fillcolor=black,width=0.25,height=0.25]; + final[label="",shape=doublecircle,style=filled,fillcolor=black,width=0.25,height=0.25]; + s1_1[label="Program:enter\nExpressionStatement:enter\nAssignmentExpression:enter\nIdentifier (x)\nObjectExpression:enter\nProperty:enter\nIdentifier (a)\nConditionalExpression:enter\nIdentifier + (b)"]; + s1_2[label="Identifier (c)"]; + s1_4[label="ConditionalExpression:exit\nProperty:exit\nObjectExpression:exit\nAssignmentExpression:exit\nExpressionStatement:exit\nProgram:exit"]; + s1_3[label="Identifier (d)"]; + initial->s1_1->s1_2->s1_4; + s1_1->s1_3->s1_4->final; +}*/ diff --git a/tests/lib/linter/code-path-analysis/code-path-analyzer.js b/tests/lib/linter/code-path-analysis/code-path-analyzer.js index b2356f24691..0b5dd33aab8 100644 --- a/tests/lib/linter/code-path-analysis/code-path-analyzer.js +++ b/tests/lib/linter/code-path-analysis/code-path-analyzer.js @@ -561,11 +561,11 @@ describe("CodePathAnalyzer", () => { } })); const messages = linter.verify(source, { - parserOptions: { ecmaVersion: 2021 }, + parserOptions: { ecmaVersion: 2022 }, rules: { test: 2 } }); - assert.strictEqual(messages.length, 0); + assert.strictEqual(messages.length, 0, "Unexpected linting error in code."); assert.strictEqual(actual.length, expected.length, "a count of code paths is wrong."); for (let i = 0; i < actual.length; ++i) {