Skip to content

Commit

Permalink
0.85.0 - #27 - Setting variable declaration type.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Nov 5, 2017
1 parent d44e2b1 commit fd3a64e
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 13 deletions.
14 changes: 10 additions & 4 deletions docs/details/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const var1 = "5",
var2 = "6";
```

### Get variable declaration list

Get one from the variable statement:

```typescript
Expand All @@ -62,9 +60,9 @@ const firstConstDeclaration = sourceFile.getVariableDeclarationList(l =>
l.getDeclarationType() === VariableDeclarationType.Const);
```

### Get declaration type
### Declaration type

Use:
Get:

```typescript
const declarationType = variableDeclarationList.getDeclarationType();
Expand All @@ -73,11 +71,19 @@ const declarationType = variableDeclarationList.getDeclarationType();
It will return one of the following values:

```typescript
import {VariableDeclarationType} from "ts-simple-ast";

VariableDeclarationType.Let;
VariableDeclarationType.Const;
VariableDeclarationType.Var;
```

Set:

```typescript
variableDeclarationList.setDeclarationType(VariableDeclarationType.Const);
```

## Variable Declaration

These are the individual declarations within a variable declaration list.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-simple-ast",
"version": "0.84.0",
"version": "0.85.0",
"description": "TypeScript compiler wrapper for AST navigation and code generation.",
"main": "dist/main.js",
"typings": "dist/main.d.ts",
Expand Down
43 changes: 43 additions & 0 deletions src/compiler/statement/VariableDeclarationList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as ts from "typescript";
import * as errors from "./../../errors";
import {insertIntoParent} from "./../../manipulation";
import {Node} from "./../common";
import {VariableDeclaration} from "./VariableDeclaration";
import {VariableDeclarationType} from "./VariableDeclarationType";
Expand All @@ -24,4 +26,45 @@ export class VariableDeclarationList extends Node<ts.VariableDeclarationList> {
else
return VariableDeclarationType.Var;
}

/**
* Gets the variable declaration type keyword.
*/
getDeclarationTypeKeyword(): Node {
const declarationType = this.getDeclarationType();
switch (declarationType) {
case VariableDeclarationType.Const:
return this.getFirstChildByKindOrThrow(ts.SyntaxKind.ConstKeyword);
case VariableDeclarationType.Let:
return this.getFirstChildByKindOrThrow(ts.SyntaxKind.LetKeyword);
case VariableDeclarationType.Var:
return this.getFirstChildByKindOrThrow(ts.SyntaxKind.VarKeyword);
default:
throw errors.getNotImplementedForNeverValueError(declarationType);
}
}

/**
* Sets the variable declaration type.
* @param type - Type to set.
*/
setDeclarationType(type: VariableDeclarationType) {
if (this.getDeclarationType() === type)
return this;
const keyword = this.getDeclarationTypeKeyword();

insertIntoParent({
childIndex: keyword.getChildIndex(),
insertItemsCount: 1,
insertPos: keyword.getStart(),
newText: type,
parent: this,
replacing: {
nodes: [keyword],
textLength: keyword.getWidth()
}
});

return this;
}
}
2 changes: 1 addition & 1 deletion src/compiler/statement/VariableStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class VariableStatement extends VariableStatementBase<ts.VariableStatemen
callBaseFill(VariableStatementBase.prototype, this, structure);

if (structure.declarationType != null)
throw new errors.NotImplementedError("Filling variable declaration type not implemented. Please comment on issue #27 if you need this and I will increase the priority.");
this.getDeclarationList().setDeclarationType(structure.declarationType);
if (structure.declarations != null)
throw new errors.NotImplementedError("Filling variable declarations not implemented. Please open an issue if you need this and I will increase the prioirty.");

Expand Down
8 changes: 8 additions & 0 deletions src/errors/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ export function throwIfNullOrUndefined<T>(value: T | undefined, errorMessage: st
throw new InvalidOperationError(typeof errorMessage === "string" ? errorMessage : errorMessage());
return value;
}

/**
* Throw if the value should have been the never type.
* @param value - Value to check.
*/
export function getNotImplementedForNeverValueError(value: never) {
return new NotImplementedError(`Not implemented value: ${value}`);
}
57 changes: 51 additions & 6 deletions src/tests/compiler/statement/variableDeclarationListTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,64 @@ import {getInfoFromText} from "./../testHelpers";
// todo: need to add tests here for getMembers
describe(nameof(VariableDeclarationList), () => {
describe(nameof<VariableDeclarationList>(d => d.getDeclarationType), () => {
function doTest(code: string, expectedType: VariableDeclarationType) {
const {firstChild} = getInfoFromText<VariableStatement>(code);
expect(firstChild.getDeclarationList().getDeclarationType()).to.equal(expectedType);
}

it("should get var for a var variable", () => {
doTest("var myVar;", VariableDeclarationType.Var);
});

it("should get let for a let variable", () => {
doTest("let myVar;", VariableDeclarationType.Let);
});

it("should get const for a const variable", () => {
doTest("const myVar = 3;", VariableDeclarationType.Const);
});
});

describe(nameof<VariableDeclarationList>(d => d.getDeclarationTypeKeyword), () => {
function doTest(code: string, expectedType: VariableDeclarationType) {
const {firstChild} = getInfoFromText<VariableStatement>(code);
expect(firstChild.getDeclarationList().getDeclarationTypeKeyword().getText()).to.equal(expectedType);
}

it("should get var for a var variable", () => {
const {firstChild} = getInfoFromText<VariableStatement>("var myVar;");
expect(firstChild.getDeclarationList().getDeclarationType()).to.equal(VariableDeclarationType.Var);
doTest("var myVar;", VariableDeclarationType.Var);
});

it("should get let for a let variable", () => {
const {firstChild} = getInfoFromText<VariableStatement>("let myVar;");
expect(firstChild.getDeclarationList().getDeclarationType()).to.equal(VariableDeclarationType.Let);
doTest("let myVar;", VariableDeclarationType.Let);
});

it("should get const for a const variable", () => {
const {firstChild} = getInfoFromText<VariableStatement>("const myVar = 3;");
expect(firstChild.getDeclarationList().getDeclarationType()).to.equal(VariableDeclarationType.Const);
doTest("const myVar = 3;", VariableDeclarationType.Const);
});
});

describe(nameof<VariableDeclarationList>(d => d.setDeclarationType), () => {
function doTest(code: string, newType: VariableDeclarationType, expectedCode: string) {
const {firstChild, sourceFile} = getInfoFromText<VariableStatement>(code);
firstChild.getDeclarationList().setDeclarationType(newType);
expect(sourceFile.getFullText()).to.equal(expectedCode);
}

it("should not change the type when it is the same", () => {
doTest("var myVar;", VariableDeclarationType.Var, "var myVar;");
});

it("should change to let", () => {
doTest("var myVar;", VariableDeclarationType.Let, "let myVar;");
});

it("should change to const", () => {
doTest("var myVar;", VariableDeclarationType.Const, "const myVar;");
});

it("should change to var", () => {
doTest("let myVar;", VariableDeclarationType.Var, "var myVar;");
});
});
});
15 changes: 14 additions & 1 deletion src/tests/compiler/statement/variableStatementTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from "chai";
import {VariableStatement} from "./../../../compiler";
import {VariableStatement, VariableDeclarationType} from "./../../../compiler";
import {VariableStatementStructure} from "./../../../structures";
import {getInfoFromText} from "./../testHelpers";

describe(nameof(VariableStatement), () => {
Expand All @@ -14,4 +15,16 @@ describe(nameof(VariableStatement), () => {
doTest("const t = '';\nconst v = '';\nconst u = '';", 1, "const t = '';\nconst u = '';");
});
});

describe(nameof<VariableStatement>(d => d.fill), () => {
function doTest(text: string, fillStructure: Partial<VariableStatementStructure>, expectedText: string) {
const {sourceFile} = getInfoFromText(text);
sourceFile.getVariableStatements()[0].fill(fillStructure);
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should set the variable declaration type", () => {
doTest("const t = '';", { declarationType: VariableDeclarationType.Let }, "let t = '';");
});
});
});

0 comments on commit fd3a64e

Please sign in to comment.