Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter/setter generation from private constructor parameters #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/getset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as vscode from 'vscode';

import { Variable } from './variable';
import { findCtorPrivateParams } from './regexutil';

export enum EType {
GETTER, SETTER, BOTH, CONSTRUCTOR
}
Expand Down Expand Up @@ -130,11 +133,12 @@ export function generateClassesList(type: EType): IClass[] {
let _class = getClass(classes, brackets.name);
const matches = {
privateDef: line.text.match(matchers.privateDef),
ctorParams: findCtorPrivateParams(line.text),
getMethod: line.text.match(matchers.getMethod),
setMethod: line.text.match(matchers.setMethod)
};
if (_class &&
(matches.getMethod || matches.privateDef || matches.setMethod)) {
(matches.getMethod || matches.privateDef || matches.setMethod || matches.ctorParams)) {
// push the found items into the approriate containers
if (matches.privateDef) {
_class.vars.push({
Expand All @@ -143,6 +147,16 @@ export function generateClassesList(type: EType): IClass[] {
typeName: matches.privateDef[2]
});
}
// add the private constructor parameters
if (matches.ctorParams.length !== 0) {
for (const param of matches.ctorParams) {
_class.vars.push({
name: param.name,
figure: publicName(param.name),
typeName: param.type
});
}
}
if (matches.getMethod) _class.getters.push(matches.getMethod[1]);
if (matches.setMethod) _class.setters.push(matches.setMethod[1]);
}
Expand Down Expand Up @@ -172,7 +186,8 @@ export function generateClassesList(type: EType): IClass[] {
break;
}
}
} else if (type == EType.SETTER || type == EType.BOTH) {
}
if (type == EType.SETTER || type == EType.BOTH) {
for (let j = 0; j < _class.setters.length; j++) {
if (_class.vars[i].figure.toLowerCase() === _class.setters[j].toLowerCase()) {
_class.vars.splice(i, 1);
Expand Down
26 changes: 26 additions & 0 deletions src/regexutil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Variable } from './variable';

const matchers = {
ctorDef: /\s*constructor\(\s*([^)]+?)\s*\)/,
ctorParam: /(?:public)?(private)?\s*([a-zA-Z_$][\w$]+)\s*\??:\s*([\.\w$]+<(?:[\.\[\]\w$\s]+,?)+>|[\.\[\]\w$]+)[^,]*,?\s*/y,
};

/**
* Finds private constructor parameters and returns them as {@link Variable[]}.
* @param line The line of text in which to try to find private constructor parameters.
*/
export function findCtorPrivateParams(line: string): Variable[] {
const params: Variable[] = [];
let ctor: RegExpMatchArray;
// First match the constructor, then match each param
if (ctor = line.match(matchers.ctorDef)) {
let param: RegExpMatchArray;
while (param = ctor[1].match(matchers.ctorParam)) {
// Check if the param is private
if (param[1]) {
params.push(new Variable(param[2], param[3]));
}
}
}
return params;
}
21 changes: 21 additions & 0 deletions src/variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class Variable {
constructor(private _name: string, private _type: string) {

}

public get name(): string {
return this._name;
}

public set name(value: string) {
this._name = value;
}

public get type(): string {
return this._type;
}

public set type(value: string) {
this._type = value;
}
}
65 changes: 65 additions & 0 deletions test/regexutil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as assert from 'assert';

import * as regexutil from '../src/regexutil';

suite("Regex Util Constructor Tests", () => {

test("Does match private params in constructor", () => {
const matches = regexutil.findCtorPrivateParams("constructor(private _name: string, private _age: number, _gender: string)");
assert.equal(2, matches.length);
assert.equal("_name", matches[0].name);
assert.equal("string", matches[0].type);
assert.equal("_age", matches[1].name);
assert.equal("number", matches[1].type);
});

test("Does ignore public and default params", () => {
const matches = regexutil.findCtorPrivateParams("constructor(public _name: string, private _age: number, _gender: string)");
assert.equal(1, matches.length);
assert.equal("_age", matches[0].name);
assert.equal("number", matches[0].type);
});

test("Does match private params after public and default params", () => {
const matches = regexutil.findCtorPrivateParams("constructor(public test: boolean, private _name: string, _gender: string, private _age: number)");
assert.equal(2, matches.length);
assert.equal("_name", matches[0].name);
assert.equal("string", matches[0].type);
assert.equal("_age", matches[1].name);
assert.equal("number", matches[1].type);
});

test("Can match generic types with more than one argument", () => {
const matches = regexutil.findCtorPrivateParams("constructor(private _test1: Type<string, number>, private _test2: Int<First, Second[], Third>)");
assert.equal(2, matches.length);
assert.equal("_test1", matches[0].name);
assert.equal("Type<string, number>", matches[0].type);
assert.equal("_test2", matches[1].name);
assert.equal("Int<First, Second[], Third>", matches[1].type);
});

test("Can match params with default value", () => {
const matches = regexutil.findCtorPrivateParams("constructor(private _test1: string = \"test1\", variable: string = 'abc', private _test2: number = 7)");
assert.equal(2, matches.length);
assert.equal("_test1", matches[0].name);
assert.equal("string", matches[0].type);
assert.equal("_test2", matches[1].name);
assert.equal("number", matches[1].type);
});

test("Does not match if not constructor", () => {
const matches = regexutil.findCtorPrivateParams("myMethod(private _name: string, private _age: number)");
assert.equal(0, matches.length);
});

test("Can match zero private params in constructor", () => {
const matches = regexutil.findCtorPrivateParams("constructor(name: string, age: number)");
assert.equal(0, matches.length);
});

test("Does not match if empty constructor", () => {
const matches = regexutil.findCtorPrivateParams("constructor()");
assert.equal(0, matches.length);
});

});