Prechádzať zdrojové kódy

Fixed rare case with positive check of `shouldAddValueToStringArray` method when `stringArrayThreshold` is 0

sanex3339 5 rokov pred
rodič
commit
19d0769163

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.browser.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.cli.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.js


+ 4 - 0
src/analyzers/string-array-storage-analyzer/StringArrayStorageAnalyzer.ts

@@ -62,6 +62,10 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
      * @param {Program} astTree
      */
     public analyze (astTree: ESTree.Program): void {
+        if (!this.options.stringArray) {
+            return;
+        }
+
         estraverse.traverse(astTree, {
             enter: (node: ESTree.Node): estraverse.VisitorOption | void => {
                 if (NodeMetadata.isIgnoredNode(node)) {

+ 43 - 1
test/unit-tests/analyzers/string-array-storage-analyzer/StringArrayStorageAnalyzer.spec.ts

@@ -197,7 +197,49 @@ describe('StringArrayStorageAnalyzer', () => {
             });
         });
 
-        describe('Analyzes of the AST tree string array threshold', () => {
+        /**
+         * This test covers rare case when with random value inside `shouldAddValueToStringArray` was `0`
+         * that trigger positive check for method.
+         *
+         * As fix i added check of `this.options.stringArray` option value
+         */
+        describe('Analyzes of the AST tree with disabled string array', () => {
+            const literalNode1: ESTree.Literal = NodeFactory.literalNode('foo');
+            const literalNode2: ESTree.Literal = NodeFactory.literalNode('bar');
+
+            const expectedStringArrayStorageItemData1: undefined = undefined;
+            const expectedStringArrayStorageItemData2: undefined = undefined;
+
+            let stringArrayStorageItemData1: IStringArrayStorageItemData | undefined;
+            let stringArrayStorageItemData2: IStringArrayStorageItemData | undefined;
+
+            before(() => {
+                stringArrayStorageAnalyzer = getStringArrayStorageAnalyzer({
+                    stringArray: false,
+                    stringArrayThreshold: 1
+                });
+                (<any>stringArrayStorageAnalyzer).options.stringArrayThreshold = 1;
+
+                const astTree: ESTree.Program = NodeFactory.programNode([
+                    NodeFactory.expressionStatementNode(literalNode1),
+                    NodeFactory.expressionStatementNode(literalNode2)
+                ]);
+
+                stringArrayStorageAnalyzer.analyze(astTree);
+                stringArrayStorageItemData1 = stringArrayStorageAnalyzer.getItemDataForLiteralNode(literalNode1);
+                stringArrayStorageItemData2 = stringArrayStorageAnalyzer.getItemDataForLiteralNode(literalNode2);
+            });
+
+            it('Variant #1: should return correct string array storage item data for literal node #1', () => {
+                assert.deepEqual(stringArrayStorageItemData1, expectedStringArrayStorageItemData1);
+            });
+
+            it('Variant #2: should return correct string array storage item data for literal node #1', () => {
+                assert.deepEqual(stringArrayStorageItemData2, expectedStringArrayStorageItemData2);
+            });
+        });
+
+        describe('Analyzes of the AST tree with string array threshold', () => {
             describe('Threshold value: 0', () => {
                 const literalNode1: ESTree.Literal = NodeFactory.literalNode('foo');
                 const literalNode2: ESTree.Literal = NodeFactory.literalNode('bar');

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov