Skip to content

Commit 7943833

Browse files
Łukasz SobekjasonLaster
authored andcommitted
Set up logic to allow checking for frameworks (firefox-devtools#5186)
1 parent 9dabc88 commit 7943833

File tree

10 files changed

+33
-24
lines changed

10 files changed

+33
-24
lines changed

assets/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ welcomebox
116116
Quickopen
117117
CommandBarButton
118118
dbg
119+
getFramework
119120
Anshul
120121
sourcemap
121122
isPaused

src/actions/ast.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
getSymbols,
1717
getEmptyLines,
1818
findOutOfScopeLocations,
19-
isReactComponent
19+
getFramework
2020
} from "../workers/parser";
2121

2222
import type { SourceId } from "debugger-html";
@@ -34,12 +34,12 @@ export function setSourceMetaData(sourceId: SourceId) {
3434
return;
3535
}
3636

37-
const isReactComp = await isReactComponent(source.id);
37+
const framework = await getFramework(source.id);
3838
dispatch({
3939
type: "SET_SOURCE_METADATA",
4040
sourceId: source.id,
4141
sourceMetaData: {
42-
isReactComponent: isReactComp
42+
framework
4343
}
4444
});
4545
};

src/actions/tests/ast.spec.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,23 @@ describe("ast", () => {
8585

8686
await waitForState(store, state => {
8787
const metaData = getSourceMetaData(state, source.id);
88-
return metaData && metaData.isReactComponent;
88+
return metaData && metaData.framework;
8989
});
9090

9191
const sourceMetaData = getSourceMetaData(getState(), source.id);
92-
expect(sourceMetaData).toEqual({ isReactComponent: true });
92+
expect(sourceMetaData.framework).toBe("React");
9393
});
9494

9595
it("should not give false positive on non react components", async () => {
9696
const store = createStore(threadClient);
9797
const { dispatch, getState } = store;
98-
const source = makeSource("base.js");
99-
await dispatch(actions.newSource(source));
98+
const base = makeSource("base.js");
99+
await dispatch(actions.newSource(base));
100100
await dispatch(actions.loadSourceText(I.Map({ id: "base.js" })));
101101
await dispatch(actions.setSourceMetaData("base.js"));
102-
await waitForState(store, state => {
103-
const metaData = getSourceMetaData(state, source.id);
104-
return metaData && metaData.isReactComponent === false;
105-
});
106102

107-
const sourceMetaData = getSourceMetaData(getState(), source.id);
108-
expect(sourceMetaData).toEqual({ isReactComponent: false });
103+
const sourceMetaData = getSourceMetaData(getState(), base.id);
104+
expect(sourceMetaData.framework).toBe(undefined);
109105
});
110106
});
111107

src/actions/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ type ASTAction =
324324
type: "SET_SOURCE_METADATA",
325325
sourceId: string,
326326
sourceMetaData: {
327-
isReactComponent: boolean
327+
framework: ?string
328328
}
329329
}
330330
| {

src/reducers/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type SymbolsMap = Map<string, SymbolDeclarations>;
2424
export type EmptyLinesMap = Map<string, EmptyLinesType>;
2525

2626
export type SourceMetaDataType = {
27-
isReactComponent: boolean
27+
getFramework: ?string
2828
};
2929

3030
export type SourceMetaDataMap = Map<string, SourceMetaDataType>;

src/utils/tabs.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ export function getSourceAnnotation(
5151
const sourceId = source.get("id");
5252
const sourceMetaData = getMetaData(sourceId);
5353

54-
if (sourceMetaData && sourceMetaData.isReactComponent) {
55-
return <img className="react" />;
54+
const framework =
55+
sourceMetaData && sourceMetaData.framework
56+
? sourceMetaData.framework
57+
: false;
58+
59+
if (framework) {
60+
return <img className={framework.toLowerCase()} />;
5661
}
62+
5763
if (isPretty(source)) {
5864
return <img className="prettyPrint" />;
5965
}

src/workers/parser/frameworks.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
import getSymbols from "./getSymbols";
66

7-
export function isReactComponent(sourceId) {
7+
export function getFramework(sourceId) {
8+
if (isReactComponent(sourceId)) {
9+
return "React";
10+
}
11+
}
12+
13+
function isReactComponent(sourceId) {
814
const { imports, classes, callExpressions } = getSymbols(sourceId);
915
return (
1016
(importsReact(imports) || requiresReact(callExpressions)) &&

src/workers/parser/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const hasSource = dispatcher.task("hasSource");
2727
export const setSource = dispatcher.task("setSource");
2828
export const clearSources = dispatcher.task("clearSources");
2929
export const hasSyntaxError = dispatcher.task("hasSyntaxError");
30-
export const isReactComponent = dispatcher.task("isReactComponent");
30+
export const getFramework = dispatcher.task("getFramework");
3131
export const replaceOriginalVariableName = dispatcher.task(
3232
"replaceOriginalVariableName"
3333
);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { isReactComponent } from "../frameworks";
1+
import { getFramework } from "../frameworks";
22
import { getSource, getOriginalSource } from "./helpers";
33
import { setSource } from "../sources";
44

55
describe("Parser.frameworks", () => {
66
it("should be a react component", () => {
77
const source = getOriginalSource("frameworks/component");
88
setSource(source);
9-
expect(isReactComponent(source.id)).toBe(true);
9+
expect(getFramework(source.id)).toBe("React");
1010
});
1111

1212
it("should handle es5 implementation of a component", () => {
1313
const source = getSource("frameworks/es5Component");
1414
setSource(source);
15-
expect(isReactComponent(source.id)).toBe(true);
15+
expect(getFramework(source.id)).toBe("React");
1616
});
1717
});

src/workers/parser/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import findOutOfScopeLocations from "./findOutOfScopeLocations";
1212
import { getNextStep } from "./steps";
1313
import getEmptyLines from "./getEmptyLines";
1414
import { hasSyntaxError } from "./validate";
15-
import { isReactComponent } from "./frameworks";
15+
import { getFramework } from "./frameworks";
1616

1717
import { workerUtils } from "devtools-utils";
1818
const { workerHandler } = workerUtils;
@@ -32,5 +32,5 @@ self.onmessage = workerHandler({
3232
getNextStep,
3333
getEmptyLines,
3434
hasSyntaxError,
35-
isReactComponent
35+
getFramework
3636
});

0 commit comments

Comments
 (0)