From 2b15909e4580fb1ef8d7f5aad64ade417b4ac0f6 Mon Sep 17 00:00:00 2001 From: jbhoosreddy Date: Thu, 29 Jun 2017 10:23:38 -0700 Subject: [PATCH 1/2] kills SourceText type --- flow-typed/debugger-html.js | 13 ----------- src/actions/sources.js | 9 +++++--- src/actions/types.js | 7 +++--- src/types.js | 1 - src/utils/parser/getOutOfScopeLocations.js | 4 ++-- src/utils/parser/getSymbols.js | 6 ++--- src/utils/parser/utils/ast.js | 27 +++++++++++----------- src/utils/parser/utils/closest.js | 8 +++---- 8 files changed, 32 insertions(+), 43 deletions(-) diff --git a/flow-typed/debugger-html.js b/flow-typed/debugger-html.js index 48edaea7dc..ce424270b6 100644 --- a/flow-typed/debugger-html.js +++ b/flow-typed/debugger-html.js @@ -216,19 +216,6 @@ declare module "debugger-html" { loading?: boolean }; - /** - * SourceText - * @memberof types - * @static - */ - declare type SourceText = { - id: string, - text: string, - contentType: string, - loading?: boolean, - error?: boolean - }; - /** * Script * This describes scripts which are sent to the debug server to be eval'd diff --git a/src/actions/sources.js b/src/actions/sources.js index 0076c4a3c0..ba6e07c82f 100644 --- a/src/actions/sources.js +++ b/src/actions/sources.js @@ -34,7 +34,7 @@ import { removeSourceFromTabList } from "../selectors"; -import type { Source, SourceText } from "../types"; +import type { Source } from "../types"; import type { ThunkArgs } from "./types"; import type { State } from "../reducers/types"; @@ -358,10 +358,13 @@ export function loadSourceText(source: Source) { const response = await client.sourceContents(source.id); - const sourceText: SourceText = { + const sourceText: Source = { id: source.id, text: response.source, - contentType: response.contentType || "text/javascript" + contentType: response.contentType || "text/javascript", + isBlackBoxed: false, + isPrettyPrinted: false, + url: "" }; return sourceText; diff --git a/src/actions/types.js b/src/actions/types.js index 3531a69e46..930f2b766c 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -7,7 +7,6 @@ import type { LoadedObject, Location, GeneratedLocation, - SourceText, Frame, Scope, Why @@ -107,7 +106,7 @@ type SourceAction = source: Source, status: AsyncStatus, error: string, - value: SourceText + value: Source } | { type: "BLACKBOX", @@ -123,7 +122,7 @@ type SourceAction = error: string, value: { isPrettyPrinted: boolean, - sourceText: SourceText, + sourceText: Source, frames: Frame[] } } @@ -216,7 +215,7 @@ type NavigateAction = { type: "NAVIGATE", url: string }; type ASTAction = | { type: "SET_SYMBOLS", - source: SourceText, + source: Source, symbols: SymbolDeclaration[] } | { diff --git a/src/types.js b/src/types.js index a7add8fca2..d3c1be22ed 100644 --- a/src/types.js +++ b/src/types.js @@ -31,7 +31,6 @@ export type { LoadedObject, Location, Source, - SourceText, Pause, Why } from "debugger-html"; diff --git a/src/utils/parser/getOutOfScopeLocations.js b/src/utils/parser/getOutOfScopeLocations.js index 91fd1b24c5..6b66dca01e 100644 --- a/src/utils/parser/getOutOfScopeLocations.js +++ b/src/utils/parser/getOutOfScopeLocations.js @@ -1,6 +1,6 @@ // @flow -import type { SourceText } from "debugger-html"; +import type { Source } from "debugger-html"; import type { AstLocation, AstPosition } from "./types"; import get from "lodash/fp/get"; @@ -74,7 +74,7 @@ function sortByStart(a: AstLocation, b: AstLocation) { * location. */ function getOutOfScopeLocations( - source: SourceText, + source: Source, position: AstPosition ): AstLocation[] { return findFunctions(source) diff --git a/src/utils/parser/getSymbols.js b/src/utils/parser/getSymbols.js index e719229aff..f30cdb15ec 100644 --- a/src/utils/parser/getSymbols.js +++ b/src/utils/parser/getSymbols.js @@ -6,7 +6,7 @@ import * as t from "babel-types"; import getFunctionName from "./utils/getFunctionName"; -import type { SourceText } from "debugger-html"; +import type { Source } from "debugger-html"; import type { NodePath, Node, Location as BabelLocation } from "babel-traverse"; const symbolDeclarations = new Map(); @@ -58,7 +58,7 @@ function getVariableNames(path: NodePath): SymbolDeclaration[] { })); } -export default function getSymbols(source: SourceText): SymbolDeclarations { +export default function getSymbols(source: Source): SymbolDeclarations { if (symbolDeclarations.has(source.id)) { const symbols = symbolDeclarations.get(source.id); if (symbols) { @@ -288,7 +288,7 @@ function getSnippet(path, prevPath, expression = "") { } } -export function formatSymbols(source: SourceText) { +export function formatSymbols(source: Source) { const { objectProperties, memberExpressions, diff --git a/src/utils/parser/utils/ast.js b/src/utils/parser/utils/ast.js index 60f9001b6d..8eb2f4e7cf 100644 --- a/src/utils/parser/utils/ast.js +++ b/src/utils/parser/utils/ast.js @@ -6,7 +6,7 @@ import traverse from "babel-traverse"; import isEmpty from "lodash/isEmpty"; import { isDevelopment } from "devtools-config"; -import type { SourceText } from "debugger-html"; +import type { Source } from "debugger-html"; const ASTs = new Map(); @@ -39,32 +39,33 @@ function parse(text: string, opts?: Object) { return ast; } -export function getAst(sourceText: SourceText) { - if (ASTs.has(sourceText.id)) { - return ASTs.get(sourceText.id); +export function getAst(source: Source) { + if (ASTs.has(source.id)) { + return ASTs.get(source.id); } let ast = {}; - if (sourceText.contentType == "text/html") { + if (source.contentType == "text/html") { // Custom parser for parse-script-tags that adapts its input structure to // our parser's signature - const parser = ({ source, line }) => { - return parse(source, { + const parser = ({ sourceText, line }) => { + return parse(sourceText, { startLine: line }); }; - ast = parseScriptTags(sourceText.text, parser) || {}; - } else if (sourceText.contentType == "text/javascript") { - ast = parse(sourceText.text); + ast = parseScriptTags(source.text, parser) || {}; + } else if (source.contentType == "text/javascript") { + const text = source.text || ""; + ast = parse(text); } - ASTs.set(sourceText.id, ast); + ASTs.set(source.id, ast); return ast; } type Visitor = { enter: Function }; -export function traverseAst(sourceText: SourceText, visitor: Visitor) { - const ast = getAst(sourceText); +export function traverseAst(source: Source, visitor: Visitor) { + const ast = getAst(source); if (isEmpty(ast)) { return null; } diff --git a/src/utils/parser/utils/closest.js b/src/utils/parser/utils/closest.js index 8c50e848de..f1fd39a03d 100644 --- a/src/utils/parser/utils/closest.js +++ b/src/utils/parser/utils/closest.js @@ -9,7 +9,7 @@ import { nodeContainsPosition } from "./helpers"; -import type { SourceText, Location } from "debugger-html"; +import type { Source, Location } from "debugger-html"; import type { NodePath, Node } from "babel-traverse"; function getNodeValue(node: Node) { @@ -43,7 +43,7 @@ function getClosestMemberExpression(source, token, location: Location) { } export function getClosestExpression( - source: SourceText, + source: Source, token: string, location: Location ) { @@ -61,7 +61,7 @@ export function getClosestExpression( return { expression: getNodeValue(node), location: node.loc }; } -export function getClosestScope(source: SourceText, location: Location) { +export function getClosestScope(source: Source, location: Location) { let closestPath = null; traverseAst(source, { @@ -83,7 +83,7 @@ export function getClosestScope(source: SourceText, location: Location) { return closestPath.scope; } -export function getClosestPath(source: SourceText, location: Location) { +export function getClosestPath(source: Source, location: Location) { let closestPath = null; traverseAst(source, { From fb27c02403ca7ae95a31f2d70549b45086dded66 Mon Sep 17 00:00:00 2001 From: Jason Laster Date: Thu, 29 Jun 2017 14:16:16 -0400 Subject: [PATCH 2/2] lj --- src/actions/sources.js | 9 ++------- src/actions/types.js | 2 +- src/reducers/sources.js | 6 +++--- src/utils/parser/utils/ast.js | 26 +++++++++++++++----------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/actions/sources.js b/src/actions/sources.js index ba6e07c82f..fe80bceb5c 100644 --- a/src/actions/sources.js +++ b/src/actions/sources.js @@ -358,16 +358,11 @@ export function loadSourceText(source: Source) { const response = await client.sourceContents(source.id); - const sourceText: Source = { + return { id: source.id, text: response.source, - contentType: response.contentType || "text/javascript", - isBlackBoxed: false, - isPrettyPrinted: false, - url: "" + contentType: response.contentType || "text/javascript" }; - - return sourceText; })() }); diff --git a/src/actions/types.js b/src/actions/types.js index 930f2b766c..56775588a4 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -122,7 +122,7 @@ type SourceAction = error: string, value: { isPrettyPrinted: boolean, - sourceText: Source, + source: Source, frames: Frame[] } } diff --git a/src/reducers/sources.js b/src/reducers/sources.js index 4fb9f9a3a7..a60913f00b 100644 --- a/src/reducers/sources.js +++ b/src/reducers/sources.js @@ -154,7 +154,7 @@ function update( function getTextPropsFromAction(action: any) { const source = action.source; - const sourceText = action.value; + const { value } = action; if (action.status === "start") { return { id: source.id, loading: true }; @@ -162,9 +162,9 @@ function getTextPropsFromAction(action: any) { return { id: source.id, error: action.error, loading: false }; } return { - text: sourceText.text, + text: value.text, id: source.id, - contentType: sourceText.contentType, + contentType: value.contentType, loading: false }; } diff --git a/src/utils/parser/utils/ast.js b/src/utils/parser/utils/ast.js index 8eb2f4e7cf..0e09707e7e 100644 --- a/src/utils/parser/utils/ast.js +++ b/src/utils/parser/utils/ast.js @@ -20,7 +20,7 @@ function _parse(code, opts) { ); } -function parse(text: string, opts?: Object) { +function parse(text: ?string, opts?: Object) { let ast; if (!text) { return; @@ -39,24 +39,28 @@ function parse(text: string, opts?: Object) { return ast; } +// Custom parser for parse-script-tags that adapts its input structure to +// our parser's signature +function htmlParser({ source, line }) { + return parse(source, { + startLine: line + }); +} + export function getAst(source: Source) { + if (!source || !source.text) { + return {}; + } + if (ASTs.has(source.id)) { return ASTs.get(source.id); } let ast = {}; if (source.contentType == "text/html") { - // Custom parser for parse-script-tags that adapts its input structure to - // our parser's signature - const parser = ({ sourceText, line }) => { - return parse(sourceText, { - startLine: line - }); - }; - ast = parseScriptTags(source.text, parser) || {}; + ast = parseScriptTags(source.text, htmlParser) || {}; } else if (source.contentType == "text/javascript") { - const text = source.text || ""; - ast = parse(text); + ast = parse(source.text); } ASTs.set(source.id, ast);