_getSourceType()

in src/scanners/javascript.js [210:261]


  _getSourceType(node, topLevel) {
    const possibleImportExportTypes = [
      'ExportAllDeclaration',
      'ExportDefaultDeclaration',
      'ExportNamedDeclaration',
      'ExportSpecifier',
      'ImportDeclaration',
      'ImportDefaultSpecifier',
      'ImportNamespaceSpecifier',
      'ImportSpecifier',
    ];
    const functionTypes = [
      'ArrowFunctionExpression',
      'FunctionDeclaration',
      'FunctionExpression',
    ];

    if (
      possibleImportExportTypes.includes(node.type) ||
      (topLevel && node.type === 'AwaitExpression')
    ) {
      return 'module';
    }

    const stayTopLevel = topLevel && !functionTypes.includes(node.type);

    const keys = vk.KEYS[node.type];

    if (keys.length >= 1) {
      for (let i = 0; i < keys.length; ++i) {
        const child = node[keys[i]];

        if (Array.isArray(child)) {
          for (let j = 0; j < child.length; ++j) {
            if (
              child[j] &&
              this._getSourceType(child[j], stayTopLevel) === 'module'
            ) {
              return 'module';
            }
          }
        } else if (
          child &&
          this._getSourceType(child, stayTopLevel) === 'module'
        ) {
          return 'module';
        }
      }
    }

    return 'script';
  }