|
9 | 9 | import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; |
10 | 10 | import ts from 'typescript'; |
11 | 11 |
|
12 | | -import {collectMemberMethods, findTightestNode} from '../src/ts_utils'; |
| 12 | +import {addElementToArrayLiteral, collectMemberMethods, findTightestNode, objectPropertyAssignmentForKey, updateObjectValueForKey} from '../src/ts_utils'; |
13 | 13 | import {LanguageServiceTestEnv, OpenBuffer, Project} from '../testing'; |
14 | 14 |
|
15 | | -describe('ts utils', () => { |
| 15 | +describe('TS util', () => { |
16 | 16 | describe('collectMemberMethods', () => { |
17 | 17 | beforeEach(() => { |
18 | 18 | initMockFileSystem('Native'); |
@@ -84,4 +84,82 @@ describe('ts utils', () => { |
84 | 84 | .sort(); |
85 | 85 | } |
86 | 86 | }); |
| 87 | + |
| 88 | + describe('AST method', () => { |
| 89 | + let printer: ts.Printer; |
| 90 | + let sourceFile: ts.SourceFile; |
| 91 | + |
| 92 | + function print(node: ts.Node): string { |
| 93 | + return printer.printNode(ts.EmitHint.Unspecified, node, sourceFile); |
| 94 | + } |
| 95 | + |
| 96 | + beforeAll(() => { |
| 97 | + printer = ts.createPrinter(); |
| 98 | + sourceFile = |
| 99 | + ts.createSourceFile('placeholder.ts', '', ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('addElementToArrayLiteral', () => { |
| 103 | + it('transforms an empty array literal expression', () => { |
| 104 | + const oldArr = ts.factory.createArrayLiteralExpression([], false); |
| 105 | + const newArr = addElementToArrayLiteral(oldArr, ts.factory.createStringLiteral('a')); |
| 106 | + expect(print(newArr)).toEqual('["a"]'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('transforms an existing array literal expression', () => { |
| 110 | + const oldArr = |
| 111 | + ts.factory.createArrayLiteralExpression([ts.factory.createStringLiteral('a')], false); |
| 112 | + const newArr = addElementToArrayLiteral(oldArr, ts.factory.createStringLiteral('b')); |
| 113 | + expect(print(newArr)).toEqual('["a", "b"]'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('objectPropertyAssignmentForKey', () => { |
| 118 | + let oldObj: ts.ObjectLiteralExpression; |
| 119 | + |
| 120 | + beforeEach(() => { |
| 121 | + oldObj = ts.factory.createObjectLiteralExpression( |
| 122 | + [ts.factory.createPropertyAssignment( |
| 123 | + ts.factory.createIdentifier('foo'), ts.factory.createStringLiteral('bar'))], |
| 124 | + false); |
| 125 | + }); |
| 126 | + |
| 127 | + it('returns null when no property exists', () => { |
| 128 | + const prop = objectPropertyAssignmentForKey(oldObj, 'oops'); |
| 129 | + expect(prop).toBeNull(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns the requested property assignment', () => { |
| 133 | + const prop = objectPropertyAssignmentForKey(oldObj, 'foo'); |
| 134 | + expect(print(prop!)).toEqual('foo: "bar"'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('updateObjectValueForKey', () => { |
| 139 | + let oldObj: ts.ObjectLiteralExpression; |
| 140 | + |
| 141 | + const valueAppenderFn = (oldValue?: ts.Expression) => { |
| 142 | + if (!oldValue) return ts.factory.createStringLiteral('baz'); |
| 143 | + if (!ts.isStringLiteral(oldValue)) return oldValue; |
| 144 | + return ts.factory.createStringLiteral(oldValue.text + 'baz'); |
| 145 | + }; |
| 146 | + |
| 147 | + beforeEach(() => { |
| 148 | + oldObj = ts.factory.createObjectLiteralExpression( |
| 149 | + [ts.factory.createPropertyAssignment( |
| 150 | + ts.factory.createIdentifier('foo'), ts.factory.createStringLiteral('bar'))], |
| 151 | + false); |
| 152 | + }); |
| 153 | + |
| 154 | + it('creates a non-existant property', () => { |
| 155 | + const obj = updateObjectValueForKey(oldObj, 'newKey', valueAppenderFn); |
| 156 | + expect(print(obj)).toBe('{ foo: "bar", newKey: "baz" }'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('updates an existing property', () => { |
| 160 | + const obj = updateObjectValueForKey(oldObj, 'foo', valueAppenderFn); |
| 161 | + expect(print(obj)).toBe('{ foo: "barbaz" }'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
87 | 165 | }); |
0 commit comments