Sha256: 0a0bedb0c95574c266e06065fbfbe37c3b7c4b0da75c138d49e08c00818b4322

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = removeTypeDuplicates;

var _generated = require("../../validators/generated");

function getQualifiedName(node) {
  return (0, _generated.isIdentifier)(node) ? node.name : `${node.id.name}.${getQualifiedName(node.qualification)}`;
}

function removeTypeDuplicates(nodes) {
  const generics = {};
  const bases = {};
  const typeGroups = new Set();
  const types = [];

  for (let i = 0; i < nodes.length; i++) {
    const node = nodes[i];
    if (!node) continue;

    if (types.indexOf(node) >= 0) {
      continue;
    }

    if ((0, _generated.isAnyTypeAnnotation)(node)) {
      return [node];
    }

    if ((0, _generated.isFlowBaseAnnotation)(node)) {
      bases[node.type] = node;
      continue;
    }

    if ((0, _generated.isUnionTypeAnnotation)(node)) {
      if (!typeGroups.has(node.types)) {
        nodes = nodes.concat(node.types);
        typeGroups.add(node.types);
      }

      continue;
    }

    if ((0, _generated.isGenericTypeAnnotation)(node)) {
      const name = getQualifiedName(node.id);

      if (generics[name]) {
        let existing = generics[name];

        if (existing.typeParameters) {
          if (node.typeParameters) {
            existing.typeParameters.params = removeTypeDuplicates(existing.typeParameters.params.concat(node.typeParameters.params));
          }
        } else {
          existing = node.typeParameters;
        }
      } else {
        generics[name] = node;
      }

      continue;
    }

    types.push(node);
  }

  for (const type of Object.keys(bases)) {
    types.push(bases[type]);
  }

  for (const name of Object.keys(generics)) {
    types.push(generics[name]);
  }

  return types;
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.16.1 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.15.2 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.18.4 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.18.1 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.14.0 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@babel/types/lib/modifications/flow/removeTypeDuplicates.js