/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at . */
// @flow
import isEmpty from "lodash/isEmpty";
import type { SourceId, SourceLocation } from "../../../types";
import * as t from "@babel/types";
import type {
Node,
TraversalAncestors,
Location as BabelLocation
} from "@babel/types";
import getFunctionName from "../utils/getFunctionName";
import { getAst } from "../utils/ast";
/**
* "implicit"
* Variables added automaticly like "this" and "arguments"
*
* "var"
* Variables declared with "var" or non-block function declarations
*
* "let"
* Variables declared with "let".
*
* "const"
* Variables declared with "const", or added as const
* bindings like inner function expressions and inner class names.
*
* "import"
* Imported binding names exposed from other modules.
*
* "global"
* Variables that reference undeclared global values.
*/
export type BindingType =
| "implicit"
| "var"
| "const"
| "let"
| "import"
| "global";
export type BindingLocationType = "ref" | BindingDeclarationType;
export type BindingDeclarationType =
| "fn-expr"
| "fn-decl"
| "fn-param"
| "class-decl"
| "class-inner"
| "import-decl"
| "import-ns-decl"
| "ts-enum-decl"
| "ts-namespace-decl"
| "var"
| "let"
| "const"
| "catch";
export type BindingLocation = BindingDeclarationLocation | BindingRefLocation;
export type BindingRefLocation = {
type: "ref",
start: SourceLocation,
end: SourceLocation,
meta?: BindingMetaValue | null
};
export type BindingDeclarationLocation = {
type: BindingDeclarationType,
start: SourceLocation,
end: SourceLocation,
// The overall location of the declaration that this binding is part of.
declaration: {
start: SourceLocation,
end: SourceLocation
},
// If this declaration was an import, include the name of the imported
// binding that this binding references.
importName?: string
};
export type BindingData = {
type: BindingType,
refs: Array
};
// Location information about the expression immediartely surrounding a
// given binding reference.
export type BindingMetaValue =
| {
type: "inherit",
start: SourceLocation,
end: SourceLocation,
parent: BindingMetaValue | null
}
| {
type: "call",
start: SourceLocation,
end: SourceLocation,
parent: BindingMetaValue | null
}
| {
type: "member",
start: SourceLocation,
end: SourceLocation,
property: string,
parent: BindingMetaValue | null
};
export type ScopeBindingList = {
[name: string]: BindingData
};
export type SourceScope = {
type: "object" | "function" | "block",
displayName: string,
start: SourceLocation,
end: SourceLocation,
bindings: ScopeBindingList
};
export type ParsedScope = SourceScope & {
children: ?(ParsedScope[])
};
export type ParseJSScopeVisitor = {
traverseVisitor: any,
toParsedScopes: () => ParsedScope[]
};
type TempScope = {
type: "object" | "function" | "function-body" | "block" | "module",
displayName: string,
parent: TempScope | null,
children: Array,
loc: BabelLocation,
bindings: ScopeBindingList
};
type ScopeCollectionVisitorState = {
sourceId: SourceId,
inType: Node | null,
freeVariables: Map>,
freeVariableStack: Array