Skip to content

Commit a8bae21

Browse files
committed
fix(in-memory-db,spectator): fix linting rules
1 parent f2811a3 commit a8bae21

File tree

2 files changed

+87
-87
lines changed

2 files changed

+87
-87
lines changed

packages/in-memory-db/src/schematics/utils/ast-utils.ts

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,95 @@
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://angular.io/license
88
*/
9-
import { ImportClause } from 'typescript';
109
import * as ts from 'typescript';
1110
import { Change, InsertChange, NoopChange } from './change';
1211

12+
13+
/**
14+
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
15+
* @param node
16+
* @param kind
17+
* @param max The maximum number of items to return.
18+
* @return all nodes of kind, or [] if none is found
19+
*/
20+
export function findNodes(
21+
node: ts.Node,
22+
kind: ts.SyntaxKind,
23+
max = Infinity,
24+
): ts.Node[] {
25+
if (!node || max == 0) {
26+
return [];
27+
}
28+
29+
const arr: ts.Node[] = [];
30+
if (node.kind === kind) {
31+
arr.push(node);
32+
max--;
33+
}
34+
if (max > 0) {
35+
for (const child of node.getChildren()) {
36+
findNodes(child, kind, max).forEach((node) => {
37+
if (max > 0) {
38+
arr.push(node);
39+
}
40+
max--;
41+
});
42+
43+
if (max <= 0) {
44+
break;
45+
}
46+
}
47+
}
48+
49+
return arr;
50+
}
51+
52+
/**
53+
* Helper for sorting nodes.
54+
* @return function to sort nodes in increasing order of position in sourceFile
55+
*/
56+
function nodesByPosition(first: ts.Node, second: ts.Node): number {
57+
return first.getStart() - second.getStart();
58+
}
59+
60+
/**
61+
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
62+
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
63+
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
64+
*
65+
* @param nodes insert after the last occurence of nodes
66+
* @param toInsert string to insert
67+
* @param file file to insert changes into
68+
* @param fallbackPos position to insert if toInsert happens to be the first occurence
69+
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
70+
* @return Change instance
71+
* @throw Error if toInsert is first occurence but fall back is not set
72+
*/
73+
export function insertAfterLastOccurrence(
74+
nodes: ts.Node[],
75+
toInsert: string,
76+
file: string,
77+
fallbackPos: number,
78+
syntaxKind?: ts.SyntaxKind,
79+
): Change {
80+
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
81+
let lastItem = [...nodes].sort(nodesByPosition).pop();
82+
if (!lastItem) {
83+
throw new Error();
84+
}
85+
if (syntaxKind) {
86+
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
87+
}
88+
if (!lastItem && fallbackPos == undefined) {
89+
throw new Error(
90+
`tried to insert ${toInsert} as first occurence with no fallback position`,
91+
);
92+
}
93+
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;
94+
95+
return new InsertChange(file, lastItemPosition, toInsert);
96+
}
97+
1398
/**
1499
* Add Import `import { symbolName } from fileName` if the import doesn't exit
15100
* already. Assumes fileToEdit can be resolved and accessed.
@@ -129,45 +214,6 @@ export function insertImport(
129214
);
130215
}
131216

132-
/**
133-
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
134-
* @param node
135-
* @param kind
136-
* @param max The maximum number of items to return.
137-
* @return all nodes of kind, or [] if none is found
138-
*/
139-
export function findNodes(
140-
node: ts.Node,
141-
kind: ts.SyntaxKind,
142-
max = Infinity,
143-
): ts.Node[] {
144-
if (!node || max == 0) {
145-
return [];
146-
}
147-
148-
const arr: ts.Node[] = [];
149-
if (node.kind === kind) {
150-
arr.push(node);
151-
max--;
152-
}
153-
if (max > 0) {
154-
for (const child of node.getChildren()) {
155-
findNodes(child, kind, max).forEach((node) => {
156-
if (max > 0) {
157-
arr.push(node);
158-
}
159-
max--;
160-
});
161-
162-
if (max <= 0) {
163-
break;
164-
}
165-
}
166-
}
167-
168-
return arr;
169-
}
170-
171217
/**
172218
* Get all the nodes from a source.
173219
* @param sourceFile The source file object.
@@ -209,52 +255,6 @@ export function findNode(
209255
return foundNode;
210256
}
211257

212-
/**
213-
* Helper for sorting nodes.
214-
* @return function to sort nodes in increasing order of position in sourceFile
215-
*/
216-
function nodesByPosition(first: ts.Node, second: ts.Node): number {
217-
return first.getStart() - second.getStart();
218-
}
219-
220-
/**
221-
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
222-
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
223-
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
224-
*
225-
* @param nodes insert after the last occurence of nodes
226-
* @param toInsert string to insert
227-
* @param file file to insert changes into
228-
* @param fallbackPos position to insert if toInsert happens to be the first occurence
229-
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
230-
* @return Change instance
231-
* @throw Error if toInsert is first occurence but fall back is not set
232-
*/
233-
export function insertAfterLastOccurrence(
234-
nodes: ts.Node[],
235-
toInsert: string,
236-
file: string,
237-
fallbackPos: number,
238-
syntaxKind?: ts.SyntaxKind,
239-
): Change {
240-
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
241-
let lastItem = [...nodes].sort(nodesByPosition).pop();
242-
if (!lastItem) {
243-
throw new Error();
244-
}
245-
if (syntaxKind) {
246-
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
247-
}
248-
if (!lastItem && fallbackPos == undefined) {
249-
throw new Error(
250-
`tried to insert ${toInsert} as first occurence with no fallback position`,
251-
);
252-
}
253-
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;
254-
255-
return new InsertChange(file, lastItemPosition, toInsert);
256-
}
257-
258258
export function getContentOfKeyLiteral(
259259
_source: ts.SourceFile,
260260
node: ts.Node,

packages/spectator/src/lib/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function installProtoMethods<T>(
7373
typeof mock[key] === 'undefined'
7474
) {
7575
mock[key] = createSpyFn(key);
76-
} else if (descriptor.get && !mock.hasOwnProperty(key)) {
76+
} else if (descriptor.get && !Object.prototype.hasOwnProperty.call(mock, key)) {
7777
Object.defineProperty(mock, key, {
7878
set: (value) => (mock[`_${key}`] = value),
7979
get: () => mock[`_${key}`],

0 commit comments

Comments
 (0)