Skip to content

Commit ffc1d60

Browse files
Merge pull request #54 from SAP/circular-error
fix: circular structure error
2 parents 90b7e42 + 748d1cd commit ffc1d60

File tree

12 files changed

+230
-122
lines changed

12 files changed

+230
-122
lines changed

backend/.nycrc.json

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
{
2-
"require": ["ts-node/register/transpile-only"],
3-
"include": ["src/**/*.ts"],
4-
"exclude": ["src/logger/*.ts", "src/panels/*.ts", "src/webSocketServer/*.ts", "src/output-channel-log.ts", "src/youi-adapter.ts"],
5-
"reporter": ["lcov", "text"],
6-
"extension": [".ts"],
7-
"all": true,
8-
"temp-dir": "./reports/.nyc_output",
9-
"report-dir": "./reports/coverage",
10-
"check-coverage": true,
11-
"branches": 98,
12-
"lines": 99,
13-
"functions": 97,
14-
"statements": 99
15-
}
2+
"require": [
3+
"ts-node/register/transpile-only"
4+
],
5+
"include": [
6+
"src/**/*.ts"
7+
],
8+
"exclude": [
9+
"src/logger/*.ts",
10+
"src/panels/*.ts",
11+
"src/webSocketServer/*.ts",
12+
"src/output-channel-log.ts",
13+
"src/youi-adapter.ts"
14+
],
15+
"reporter": [
16+
"lcov",
17+
"text"
18+
],
19+
"extension": [
20+
".ts"
21+
],
22+
"all": true,
23+
"temp-dir": "./reports/.nyc_output",
24+
"report-dir": "./reports/coverage",
25+
"check-coverage": true,
26+
"branches": 98,
27+
"lines": 99,
28+
"functions": 97,
29+
"statements": 99
30+
}

backend/mocha.opts

Lines changed: 0 additions & 4 deletions
This file was deleted.

backend/package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippet",
3-
"version": "0.0.22",
4-
"displayName": "Code Snippet",
3+
"version": "0.0.23",
4+
"displayName": "Code Snippet",
55
"publisher": "SAPOSS",
66
"author": {
77
"name": "SAP SE"
@@ -110,29 +110,29 @@
110110
"compile": "tsc -p ./",
111111
"watch": "tsc -watch -p ./",
112112
"package": "npm run webpack && vsce package .",
113-
"test": "nyc mocha -p tsconfig.json --opts ./mocha.opts",
113+
"test": "nyc mocha -p tsconfig.json --recursive tests/**/*.spec.ts",
114114
"ws:run": "node ./out/src/webSocketServer/index.js",
115115
"lint": "eslint . --ext .ts,.tsx --cache",
116116
"lint:fix": "eslint . --ext .ts,.tsx --cache --fix"
117117
},
118118
"dependencies": {
119-
"@sap-devx/webview-rpc": "^0.2.2",
120-
"@vscode-logging/logger": "1.1.0",
121-
"datauri": "^2.0.0",
122-
"fs-extra": "^8.1.0",
123-
"lodash": "^4.17.15",
124-
"strip-ansi": "^6.0.0",
125-
"ws": "^7.2.5",
119+
"@sap-devx/webview-rpc": "0.2.3",
120+
"@vscode-logging/logger": "1.2.1",
121+
"datauri": "3.0.0",
122+
"fs-extra": "9.0.1",
123+
"lodash": "4.17.20",
124+
"strip-ansi": "6.0.0",
125+
"ws": "7.4.0",
126126
"yeoman-environment": "2.10.3"
127127
},
128128
"devDependencies": {
129-
"@types/chai": "^4.2.9",
130-
"@types/fs-extra": "^8.1.0",
129+
"@types/chai": "^4.2.14",
130+
"@types/fs-extra": "^9.0.4",
131131
"@types/inquirer": "^6.5.0",
132-
"@types/lodash": "^4.14.150",
133-
"@types/mocha": "^5.2.7",
132+
"@types/lodash": "^4.14.165",
133+
"@types/mocha": "^7.0.0",
134134
"@types/node": "^10.17.21",
135-
"@types/sinon": "^7.5.0",
135+
"@types/sinon": "^9.0.9",
136136
"@types/ws": "^6.0.3",
137137
"@types/yeoman-environment": "2.10.2",
138138
"@typescript-eslint/parser": "^2.30.0",
@@ -143,9 +143,9 @@
143143
"typescript": "^3.9.7",
144144
"chai": "^4.2.0",
145145
"copy-webpack-plugin": "^5.0.5",
146-
"mocha": "^6.2.2",
147-
"nyc": "^14.1.1",
148-
"sinon": "^7.5.0",
146+
"mocha": "^7.0.0",
147+
"nyc": "^15.1.0",
148+
"sinon": "^9.2.1",
149149
"ts-loader": "^6.2.1",
150150
"ts-node": "^8.9.1",
151151
"string-replace-loader": "^2.1.1",

backend/src/code-snippet.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as _ from "lodash";
2-
import * as Environment from "yeoman-environment";
32
import * as inquirer from "inquirer";
43
import { AppLog } from "./app-log";
54
import { AppEvents } from "./app-events";
@@ -8,6 +7,7 @@ import Generator = require("yeoman-generator");
87
import { IChildLogger } from "@vscode-logging/logger";
98
import TerminalAdapter = require("yeoman-environment/lib/adapter");
109

10+
1111
export class CodeSnippet {
1212

1313
private static funcReplacer(key: any, value: any) {
@@ -24,7 +24,6 @@ export class CodeSnippet {
2424
private currentQuestions: TerminalAdapter.Questions<any>;
2525
private snippetName: string;
2626
private readonly customQuestionEventHandlers: Map<string, Map<string, Function>>;
27-
private errorThrown = false;
2827

2928
constructor(rpc: IRpc, appEvents: AppEvents, outputChannel: AppLog, logger: IChildLogger, uiOptions: any) {
3029
this.rpc = rpc;
@@ -50,7 +49,17 @@ export class CodeSnippet {
5049
}
5150

5251
private async getState() {
53-
return this.uiOptions;
52+
let state = _.omit(this.uiOptions, ["snippet"]);
53+
try {
54+
// valiation for rpc
55+
JSON.stringify(state);
56+
} catch (error) {
57+
// save stateError and remove contributorInfo.context
58+
state = _.omit(state, ["contributorInfo.context"]);
59+
_.set(state, "stateError", true);
60+
}
61+
62+
return state;
5463
}
5564

5665
public registerCustomQuestionEventHandler(questionType: string, methodName: string, handler: Function): void {
@@ -165,7 +174,6 @@ export class CodeSnippet {
165174
}
166175

167176
private async onFailure(snippetrName: string, error: any) {
168-
this.errorThrown = true;
169177
const messagePrefix = `${snippetrName} snippet failed.`;
170178
const errorMessage: string = await this.logError(error, messagePrefix);
171179
this.appEvents.doSnippeDone(false, errorMessage);

backend/src/contributors.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
import * as vscode from 'vscode';
22
import * as _ from 'lodash';
3+
import { IChildLogger } from '@vscode-logging/logger';
4+
import { getClassLogger } from './logger/logger-wrapper';
5+
36

47
export class Contributors {
5-
public static async getSnippet(contributerInfo: any) {
6-
const contributorId = _.get(contributerInfo, "contributorId");
7-
const extension = Contributors.getContributorExtension(contributorId);
8+
private readonly logger: IChildLogger;
9+
10+
constructor() {
11+
this.logger = getClassLogger("Contributors");
12+
}
13+
14+
public async getSnippet(contributorInfo: any) {
15+
const contributorId = _.get(contributorInfo, "contributorId");
16+
const extension = this.getContributorExtension(contributorId);
817
if (extension) {
918
try {
1019
const api = await this.getApiPromise(extension as vscode.Extension<any>);
11-
const snippetContext = _.get(contributerInfo, "context");
20+
const snippetContext = _.get(contributorInfo, "context");
1221
const snippets = api.getCodeSnippets(snippetContext);
13-
const snippetName = _.get(contributerInfo, "snippetName");
22+
const snippetName = _.get(contributorInfo, "snippetName");
1423
return snippets.get(snippetName);
1524
} catch (error) {
1625
const errorMessage = _.get(error, "stack", _.get(error, "message", error));
17-
console.error(errorMessage);
18-
// TODO: Add Logger.error
26+
this.logger.error(`Could not get '${contributorId}' snippet`, errorMessage);
1927
}
2028
}
2129
}
2230

23-
private static getApiPromise(extension: vscode.Extension<any>) {
24-
return (extension.isActive ? extension.exports : extension.activate());
31+
private getApiPromise(extension: vscode.Extension<any>): Thenable<any> {
32+
return (extension.isActive ? Promise.resolve(extension.exports) : extension.activate());
2533
}
2634

27-
private static getContributorExtension(contributorId: string) {
35+
private getContributorExtension(contributorId: string) {
2836
return _.find(vscode.extensions.all, (extension: vscode.Extension<any>) => {
2937
const extensionDependencies: string[] = _.get(extension, "packageJSON.extensionDependencies");
3038
if (_.includes(extensionDependencies, "saposs.code-snippet")) {
31-
if (contributorId === Contributors.getExtensionId(extension)) {
39+
if (contributorId === this.getExtensionId(extension)) {
3240
return extension;
3341
}
3442
}
43+
44+
this.logger.warn(`Extension '${contributorId}' could not be found.`);
3545
});
3646
}
3747

38-
private static getExtensionId(extension: vscode.Extension<any>) {
48+
private getExtensionId(extension: vscode.Extension<any>) {
3949
const extensionName: string = _.get(extension, "packageJSON.name");
4050
const extensionPublisher: string = _.get(extension, "packageJSON.publisher");
4151
return `${extensionPublisher}.${extensionName}`;

backend/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function registerAndSubscribeCommand(cId: string, cAction: any) {
2929

3030
function registerWebviewPanelSerializer(abstractPanel: AbstractWebviewPanel) {
3131
vscode.window.registerWebviewPanelSerializer(abstractPanel.viewType, {
32-
async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state?: any) {
33-
abstractPanel.setWebviewPanel(webviewPanel, state);
32+
async deserializeWebviewPanel(webViewPanel: vscode.WebviewPanel, state?: any) {
33+
abstractPanel.setWebviewPanel(webViewPanel, state);
3434
}
3535
});
3636
}

backend/src/panels/AbstractWebviewPanel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path';
33
import * as _ from 'lodash';
44
import * as fsextra from 'fs-extra';
55
import { IChildLogger } from '@vscode-logging/logger';
6-
import { getLogger } from '../logger/logger-wrapper';
6+
import { getClassLogger } from '../logger/logger-wrapper';
77

88

99
export abstract class AbstractWebviewPanel {
@@ -24,7 +24,7 @@ export abstract class AbstractWebviewPanel {
2424
this.extensionPath = context.extensionPath;
2525
this.mediaPath = path.join(context.extensionPath, "dist", "media");
2626
this.htmlFileName = "index.html";
27-
this.logger = getLogger();
27+
this.logger = getClassLogger("AbstractWebviewPanel");
2828
this.disposables = [];
2929
}
3030

backend/src/panels/CodeSnippetPanel.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@ import { Contributors } from "../contributors";
1515

1616
export class CodeSnippetPanel extends AbstractWebviewPanel {
1717
public static CODE_SNIPPET = "Code Snippet";
18-
1918
private static channel: vscode.OutputChannel;
2019

2120
public toggleOutput() {
2221
this.outputChannel.showOutput();
2322
}
2423

2524
public setWebviewPanel(webViewPanel: vscode.WebviewPanel, uiOptions?: any) {
26-
super.setWebviewPanel(webViewPanel, uiOptions);
25+
const contributorInfo = _.get(uiOptions, "contributorInfo", uiOptions);
26+
27+
if (_.get(uiOptions, "stateError")) {
28+
this.logger.error("test");
29+
this.logger.error(`'${contributorInfo.contributorId}' snippet state could not be saved. JSON.stringify issue.`);
30+
return webViewPanel.dispose();
31+
}
2732

28-
const contributerInfo = _.get(uiOptions, "contributerInfo", uiOptions);
29-
Contributors.getSnippet(contributerInfo).then(snippet => {
33+
this.contributors.getSnippet(contributorInfo).then(snippet => {
3034
if (_.isNil(snippet)) {
31-
this.webViewPanel.dispose();
32-
return vscode.window.showErrorMessage("Can not find snippet.");
35+
this.logger.error(`'${contributorInfo.contributorId}' snippet could not be found.`);
36+
return this.webViewPanel.dispose();
3337
}
3438

39+
super.setWebviewPanel(webViewPanel, uiOptions);
40+
3541
this.messages = _.assign({}, backendMessages, snippet.getMessages());
3642
const rpc = new RpcExtension(this.webViewPanel.webview);
3743
this.outputChannel = new OutputChannelLog(this.messages.channelName);
@@ -40,7 +46,7 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
4046
vscodeEvents,
4147
this.outputChannel,
4248
this.logger,
43-
{ messages: this.messages, snippet, contributerInfo});
49+
{ messages: this.messages, snippet, contributorInfo});
4450
this.codeSnippet.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
4551
this.codeSnippet.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));
4652

@@ -59,12 +65,14 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
5965
private codeSnippet: CodeSnippet;
6066
private messages: any;
6167
private outputChannel: AppLog;
68+
private readonly contributors: Contributors;
6269

6370
public constructor(context: vscode.ExtensionContext) {
6471
super(context);
6572
this.viewType = "codeSnippet";
6673
this.viewTitle = CodeSnippetPanel.CODE_SNIPPET;
6774
this.focusedKey = "codeSnippet.Focused";
75+
this.contributors = new Contributors();
6876
}
6977

7078
private async showOpenFileDialog(currentPath: string): Promise<string> {

0 commit comments

Comments
 (0)