Skip to content

Commit ffeb6ff

Browse files
authored
Merge pull request #39 from oslabs-beta/AL/client-conditionals
AL/client conditionals
2 parents fb4d211 + 2209927 commit ffeb6ff

35 files changed

+675
-267
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"rules": {
1717
"no-const-assign": "warn",
1818
"no-this-before-super": "warn",
19-
"no-undef": "warn",
19+
"no-undef": "off",
2020
"no-unreachable": "warn",
2121
"no-unused-vars": "off",
2222
"constructor-super": "warn",

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
node_modules
1+
/node_modules/
22
/package-lock.json
33
/.vscode-test/
4-
/build/
4+
/build/
5+
/.DS_Store

.vscodeignore

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# FOLDERS
12
.vscode/**
23
.vscode-test/**
34
test/**
4-
.gitignore
5-
.yarnrc
6-
vsc-extension-quickstart.md
5+
node_modules
76
**/jsconfig.json
87
**/*.map
98
**/.eslintrc.json
9+
10+
# FILES
11+
.gitignore
12+
.yarnrc
13+
vsc-extension-quickstart.md
1014
**/*.js.map
15+

package-lock.json

Lines changed: 149 additions & 155 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
{
22
"name": "react-labyrinth",
33
"displayName": "React Labyrinth",
4-
"description": "React Server Components visualization tool",
4+
"description": "React Component Type hierarchy tree visualization tool",
55
"version": "0.0.1",
6+
"icon": "./media/reactLbayrinthFinal.png",
67
"publisher": "ReactLabyrinthDev",
8+
"preview": false,
79
"engines": {
810
"vscode": "^1.85.1"
911
},
1012
"repository": {
1113
"type": "git",
12-
"url": "https://github.com/oslabs-beta/React-Labyrinth"
14+
"url": "https://github.com/oslabs-beta/React-Labyrinth/tree/main"
1315
},
1416
"categories": [
1517
"Other"
1618
],
1719
"keywords": [
1820
"react",
1921
"rsc",
22+
"react components",
2023
"hierarchy tree",
2124
"parent-child",
2225
"visualizer",
23-
"server component"
26+
"server component",
27+
"client component"
2428
],
2529
"license": "MIT",
2630
"pricing": "Free",
27-
"activationEvents": [],
31+
"activationEvents": [
32+
"onStartupFinished"
33+
],
2834
"main": "./build/extension.js",
2935
"contributes": {
3036
"commands": [
@@ -51,28 +57,25 @@
5157
"views": {
5258
"react-labyrinth": [
5359
{
54-
"id": "metrics-file",
55-
"name": "Metrics"
56-
},
57-
{
58-
"id": "test",
59-
"name": "Test"
60+
"id": "tree-render",
61+
"name": "Tree"
6062
}
6163
]
6264
},
6365
"viewsWelcome": [
6466
{
65-
"view": "metrics-file",
66-
"contents": "View tree to see where improvements can be made!\n[View Tree](command:myExtension.pickFile)\n"
67+
"view": "tree-render",
68+
"contents": "View tree to see component types!\n[View Tree](command:myExtension.pickFile)\n"
6769
}
6870
]
6971
},
7072
"scripts": {
73+
"vscode:prepublish": "prod",
7174
"lint": "eslint .",
7275
"pretest": "npm run lint",
7376
"test": "npx tsc ; node ./build/src/test/runTest.js",
74-
"dev": "webpack --watch",
75-
"webpack": "webpack"
77+
"dev": "webpack --mode development --config webpack.config.ts --watch",
78+
"prod": "webpack --mode production --config webpack.config.ts"
7679
},
7780
"devDependencies": {
7881
"@babel/preset-typescript": "^7.23.3",
@@ -97,7 +100,7 @@
97100
},
98101
"dependencies": {
99102
"@babel/core": "^7.23.3",
100-
"@babel/parser": "^7.23.4",
103+
"@babel/parser": "^7.23.9",
101104
"@babel/preset-env": "^7.23.3",
102105
"@babel/preset-react": "^7.23.3",
103106
"babel": "^6.23.0",

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22
import {createPanel} from './panel';
33
import { Parser } from './parser';
44
import { Tree } from './types/tree';
5+
import { showNotification } from './utils/modal';
56

67
let tree: Parser | undefined = undefined;
78
let panel: vscode.WebviewPanel | undefined = undefined;
@@ -27,7 +28,7 @@ function activate(context: vscode.ExtensionContext) {
2728

2829
// Throw error message if no file was selected
2930
if (!fileArray || fileArray.length === 0) {
30-
vscode.window.showErrorMessage('No file selected');
31+
showNotification({message: 'No file selected'});
3132
return;
3233
}
3334

src/parser.ts

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Parser {
4949
reactRouter: false,
5050
reduxConnect: false,
5151
children: [],
52-
parent: null,
52+
parent: '',
5353
parentList: [],
5454
props: {},
5555
error: '',
@@ -80,7 +80,7 @@ export class Parser {
8080
};
8181

8282
public getTree(): Tree {
83-
return this.tree;
83+
return this.tree!;
8484
}
8585

8686
// Set entryFile property with the result of Parser (from workspace state)
@@ -178,9 +178,6 @@ export class Parser {
178178
if (componentTree.parentList.includes(componentTree.filePath)) {
179179
return;
180180
}
181-
// if (typeof componentTree.parentList === 'string' && componentTree.parentList.includes(componentTree.filePath)) {
182-
// return;
183-
// }
184181

185182
// Create abstract syntax tree of current component tree file
186183
let ast: babel.ParseResult<File>;
@@ -289,15 +286,14 @@ export class Parser {
289286
}
290287

291288
// Determines server or client component type (looks for use of 'use client' and react/redux state hooks)
292-
private getComponentType(directive: { [key: string]: any }[], body: { [key: string]: any }[]): boolean {
289+
private getComponentType(directive: { [key: string]: any }[], body: { [key: string]: any }[]) {
293290
const defaultErr = (err) => {
294291
return {
295292
method: 'Error in getCallee method of Parser:',
296293
log: err,
297294
}
298295
};
299296

300-
// console.log('directive: ', directive);
301297
// Initial check for use of directives (ex: 'use client', 'use server', 'use strict')
302298
// Accounts for more than one directive
303299
for (let i = 0; i < directive.length; i++) {
@@ -310,8 +306,14 @@ export class Parser {
310306
}
311307

312308
// Second check for use of React/Redux hooks
309+
// Checks for components declared using 'const'
313310
const bodyCallee = body.filter((item) => item.type === 'VariableDeclaration');
314-
if (bodyCallee.length === 0) return false;
311+
312+
// Checks for components declared using 'export default function'
313+
const exportCallee = body.filter((item) => item.type === 'ExportDefaultDeclaration');
314+
315+
// Checks for components declared using 'function'
316+
const functionCallee = body.filter((item) => item.type === 'FunctionDeclaration');
315317

316318
// Helper function
317319
const calleeHelper = (item) => {
@@ -333,6 +335,8 @@ export class Parser {
333335
useDispatch: 0,
334336
useActions: 0,
335337
useSelector: 0,
338+
useShallowEqualSelector: 0,
339+
useStore: 0,
336340
bindActionCreators: 0,
337341
}
338342
if (item.type === 'VariableDeclaration') {
@@ -363,39 +367,43 @@ export class Parser {
363367
return false;
364368
}
365369

366-
if (bodyCallee.length === 1) {
367-
const calleeArr = bodyCallee[0].declarations[0]?.init?.body?.body;
368-
if (calleeArr === undefined) return false;
370+
// Process Function Declarations
371+
for (const func of functionCallee) {
372+
const calleeArr = func.body?.body;
373+
if (!calleeArr) continue; // Skip if no body
369374

370-
let checkTrue = false;
371-
for (let i = 0; i < calleeArr.length; i++) {
372-
if (checkTrue) return true;
373-
checkTrue = calleeHelper(calleeArr[i]);
375+
for (const callee of calleeArr) {
376+
if (calleeHelper(callee)) {
377+
return true;
378+
}
374379
}
375-
return checkTrue;
376380
}
377-
else if (bodyCallee.length > 1) {
378-
let calleeArr: [];
379-
for (let i = 0; i < bodyCallee.length; i++) {
380-
try {
381-
if (bodyCallee[i].declarations[0]?.init?.body?.body) {
382-
calleeArr = bodyCallee[i].declarations[0].init.body.body;
383-
}
384-
}
385-
catch (err) {
386-
const error = defaultErr(err);
387-
console.error(error.method, '\n', error.log);
381+
382+
// Process Export Declarations
383+
for (const exportDecl of exportCallee) {
384+
const calleeArr = exportDecl.declaration.body?.body;
385+
if (!calleeArr) continue; // Skip if no body
386+
387+
for (const callee of calleeArr) {
388+
if (calleeHelper(callee)) {
389+
return true;
388390
}
389391
}
390-
391-
if (calleeArr === undefined) return false;
392-
let checkTrue = false;
393-
for (let i = 0; i < calleeArr.length; i++) {
394-
if (checkTrue) return true;
395-
checkTrue = calleeHelper(calleeArr[i]);
392+
}
393+
394+
// Process Body Declarations
395+
for (const bodyDecl of bodyCallee) {
396+
const calleeArr = bodyDecl.declarations[0]?.init?.body?.body;
397+
if (!calleeArr) continue; // Skip if no body
398+
399+
for (const callee of calleeArr) {
400+
if (calleeHelper(callee)) {
401+
return true;
402+
}
396403
}
397-
return checkTrue;
398404
}
405+
406+
return false;
399407
}
400408

401409
// Finds JSX React Components in current file

src/test/runTest.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as path from 'path';
22
import { runTests } from '@vscode/test-electron';
33

44
async function main() {
5-
console.log('made it through the line before try block');
65
try {
76
// The folder containing the Extension Manifest package.json
87
// Passed to `--extensionDevelopmentPath`
@@ -12,8 +11,6 @@ async function main() {
1211
// Passed to --extensionTestsPath
1312
const extensionTestsPath = path.resolve(__dirname, './suite/index');
1413

15-
console.log('inside try block after var declarations');
16-
1714
// Download VS Code, unzip it and run the integration test
1815
await runTests({
1916
version: "1.85.1",

src/test/suite/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import * as jest from 'jest';
44

55
export async function run(): Promise<void> {
66
try {
7-
console.log('inside try block of index.ts');
8-
97
const testsRoot = path.resolve(__dirname, '..');
108
const files = await glob('**/**.test.js', { cwd: testsRoot });
119

@@ -14,15 +12,10 @@ export async function run(): Promise<void> {
1412
return;
1513
}
1614

17-
console.log('test files: ', files);
18-
1915
return new Promise(async (c, e) => {
2016
try {
21-
console.log('inside promise block of index.ts before await ')
2217
await jest.run([...files]);
23-
console.log('inside promise block of index.ts after await')
2418
c();
25-
console.log('inside promise block of index.ts after c()')
2619
} catch (err) {
2720
console.error(err);
2821
e(err);

0 commit comments

Comments
 (0)