Skip to content

Commit

Permalink
feat: catch infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Sep 16, 2019
1 parent bc3a48f commit 5b1b99f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Awesome tree",
"description": "Stop creating folders, start creating structures! (Automated file creation)",
"publisher": "bajdzis",
"version": "0.0.4",
"version": "0.0.5",
"engines": {
"vscode": "^1.37.0"
},
Expand Down
30 changes: 30 additions & 0 deletions src/errors/AwesomeTreeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export class AwesomeTreeError extends Error {
private debugInfo: { [key: string]: any };

constructor(message: string, debugInfo: { [key: string]: any }) {
super(message);
this.debugInfo = debugInfo;
}

getDebugInfo = () => {
return { ...this.debugInfo };
}

getTitle = () => {
return this.message;
}

getBody = () => [
`${this.getTitle()}`,
'',
'Debug Info:',
'```json',
JSON.stringify(this.debugInfo, null, 2),
'```',
'Stack Info:',
'```',
(this.stack || ''),
'```'
].join('\n');

}
15 changes: 13 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path';
import { getInfoAboutPath, PathInfo } from './fileInfo/getInfoAboutPath';
import { createVariableTemplate } from './fileInfo/createVariableTemplate';
import { renderVariableTemplate } from './fileInfo/renderVariableTemplate';
import { AwesomeTreeError } from './errors/AwesomeTreeError';

type Directories = {
[key:string]: {
Expand Down Expand Up @@ -106,8 +107,18 @@ export function activate(context: vscode.ExtensionContext) {
'Create issue od GitHub'
);

if (result === 'Create issue od GitHub') {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/Bajdzis/vscode-awesome-tree/issues/new'));
if (error instanceof AwesomeTreeError) {
if (result === 'Create issue od GitHub') {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(
`https://github.com/Bajdzis/vscode-awesome-tree/issues/new?title=${encodeURI(error.getTitle())}&body=${encodeURIComponent(error.getBody())}`
));
}
} else {
if (result === 'Create issue od GitHub') {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(
`https://github.com/Bajdzis/vscode-awesome-tree/issues/new?title=${encodeURI(error.getTitle())}`
));
}
}

}
Expand Down
10 changes: 9 additions & 1 deletion src/fileInfo/createVariableTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PathInfo } from "./getInfoAboutPath";
import { getTextCase } from "./getInfoWords";
import { addSlashes } from "../helpers/addSlashes";
import { AwesomeTreeError } from "../errors/AwesomeTreeError";


export function createVariableTemplate(search:string, information: PathInfo[]) {
export function createVariableTemplate(search:string, information: PathInfo[], maxIterate: number = 500) {
const variables: {
[key: string] : [number,number,number];
} = {};
Expand Down Expand Up @@ -31,6 +32,13 @@ export function createVariableTemplate(search:string, information: PathInfo[]) {
if(textCase !== 'other'){
result = result.replace( new RegExp(`(?<=^([^\\$\{]|\\$\\{[^"]*\\})*)(?<varName>${word})`, 'g'), `\${${textCase}(variable[${index0}][${index1}][${index2}])}` );
}
maxIterate--;
if(maxIterate === 0){
throw new AwesomeTreeError(`Too many iterate!`, {
callFunction: 'createVariableTemplate',
arguments: [search, information]
});
}
}
});

Expand Down
10 changes: 9 additions & 1 deletion src/fileInfo/renderVariableTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PathInfo } from "./getInfoAboutPath";
import { TextCase, getFormatedText } from "./getInfoWords";
import { AwesomeTreeError } from "../errors/AwesomeTreeError";

export function renderVariableTemplate(template:string, information: PathInfo[]) {
export function renderVariableTemplate(template:string, information: PathInfo[], maxIterate: number = 500) {

let result = template;

Expand All @@ -15,6 +16,13 @@ export function renderVariableTemplate(template:string, information: PathInfo[])
const templateVariable = regExpResult[0];
const formatedText = getFormatedText(word, textCase);
result = result.replace(templateVariable, formatedText);
maxIterate--;
if(maxIterate === 0){
throw new AwesomeTreeError(`Too many iterate!`, {
callFunction: 'renderVariableTemplate',
arguments: [template, information]
});
}
}
});
});
Expand Down

0 comments on commit 5b1b99f

Please sign in to comment.