コード例 #1
0
ファイル: visitor.js プロジェクト: jld/gecko-dev
      node.specifiers.forEach(spec => {
        if (spec.importKind && spec.importKind !== "value") {
          return;
        }

        if (t.isImportNamespaceSpecifier(spec)) {
          state.declarationBindingIds.add(spec.local);

          state.scope.bindings[spec.local.name] = {
            // Imported namespaces aren't live import bindings, they are
            // just normal const bindings.
            type: "const",
            refs: [
              {
                type: "import-ns-decl",
                start: fromBabelLocation(spec.local.loc.start, state.sourceId),
                end: fromBabelLocation(spec.local.loc.end, state.sourceId),
                declaration: {
                  start: fromBabelLocation(node.loc.start, state.sourceId),
                  end: fromBabelLocation(node.loc.end, state.sourceId),
                },
              },
            ],
          };
        } else {
          state.declarationBindingIds.add(spec.local);

          state.scope.bindings[spec.local.name] = {
            type: "import",
            refs: [
              {
                type: "import-decl",
                start: fromBabelLocation(spec.local.loc.start, state.sourceId),
                end: fromBabelLocation(spec.local.loc.end, state.sourceId),
                importName: t.isImportDefaultSpecifier(spec)
                  ? "default"
                  : spec.imported.name,
                declaration: {
                  start: fromBabelLocation(node.loc.start, state.sourceId),
                  end: fromBabelLocation(node.loc.end, state.sourceId),
                },
              },
            ],
          };
        }
      });
コード例 #2
0
function resolveScopedAt(path, identifier, document, kind, sourceRange) {
    // Handle all kinds of imports except namespace imports (see below for them).
    if (isSomeKindOfImport(path)) {
        const exportedIdentifier = getExportedIdentifier(path.node, identifier);
        if (exportedIdentifier === undefined) {
            if (sourceRange === undefined) {
                return { successful: false, error: undefined };
            }
            else {
                return {
                    successful: false,
                    error: new warning_1.Warning({
                        code: 'could-not-resolve-reference',
                        message: `Could not resolve reference to '${identifier}' ` +
                            `with kind ${kind}`,
                        severity: warning_1.Severity.WARNING,
                        sourceRange,
                        parsedDocument: document.parsedDocument,
                    }),
                };
            }
        }
        return resolveThroughImport(path, exportedIdentifier, document, kind, sourceRange);
    }
    if (babel.isExportNamedDeclaration(path.node) && !path.node.source) {
        for (const specifier of path.node.specifiers) {
            if (specifier.exported.name !== specifier.local.name &&
                specifier.exported.name === identifier) {
                // In cases like `export {foo as bar}`, we need to look for a feature
                // called `foo` instead of `bar`.
                return resolveScopedAt(path, specifier.local.name, document, kind, sourceRange);
            }
        }
    }
    const statement = esutil.getCanonicalStatement(path);
    if (statement === undefined) {
        return { successful: false, error: undefined };
    }
    const features = document.getFeatures({ kind, id: identifier, statement });
    if (features.size > 1) {
        // TODO(rictic): narrow down by identifier? warn?
        return { successful: false, error: undefined };
    }
    const [feature] = features;
    if (feature !== undefined) {
        return { successful: true, value: feature };
    }
    // Handle namespace imports. e.g.
    //     import * as foo from 'foo-library'; class X extends foo.Bar {}
    const hasASingleDotInName = /^[^\.]+\.[^\.]+$/;
    if (hasASingleDotInName.test(identifier)) {
        const [namespace, name] = identifier.split('.');
        const namespaceBinding = path.scope.getBinding(namespace);
        if (namespaceBinding !== undefined) {
            const node = namespaceBinding.path.node;
            if (babel.isImportNamespaceSpecifier(node)) {
                return resolveThroughImport(namespaceBinding.path, name, document, kind, sourceRange);
            }
        }
    }
    const binding = path.scope.getBinding(identifier);
    if (binding === undefined || binding.path.node === path.node) {
        return { successful: false, error: undefined };
    }
    return resolveScopedAt(binding.path, identifier, document, kind, sourceRange);
}