Skip to content

Commit 9bc4167

Browse files
Remove asserts (#2814)
1 parent a789d74 commit 9bc4167

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

packages/firestore/rollup.config.es2017.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2018 Google Inc.
3+
* Copyright 2018 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ import pkg from './package.json';
2828
import memoryPkg from './memory/package.json';
2929

3030
import {
31-
appendPrivatePrefixTransformers,
31+
firestoreTransformers,
3232
manglePrivatePropertiesOptions,
3333
resolveNodeExterns,
3434
resolveBrowserExterns
@@ -69,7 +69,7 @@ const browserBuildPlugins = [
6969
}
7070
},
7171
clean: true,
72-
transformers: appendPrivatePrefixTransformers
72+
transformers: firestoreTransformers
7373
}),
7474
json({ preferConst: true }),
7575
terser(manglePrivatePropertiesOptions)

packages/firestore/rollup.shared.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as path from 'path';
2020
import { externs } from './externs.json';
2121
import { renameInternals } from './scripts/rename-internals';
2222
import { extractPublicIdentifiers } from './scripts/extract-api';
23+
import { removeAsserts } from './scripts/remove-asserts';
2324

2425
import pkg from './package.json';
2526

@@ -43,11 +44,13 @@ const externsPaths = externs.map(p => path.resolve(__dirname, '../../', p));
4344
const publicIdentifiers = extractPublicIdentifiers(externsPaths);
4445

4546
/**
46-
* A transformer that appends a __PRIVATE_ prefix to all internal symbols.
47+
* Transformers that remove all debugAsserts and appends a __PRIVATE_ prefix to
48+
* all internal symbols.
4749
*/
48-
export const appendPrivatePrefixTransformers = [
50+
export const firestoreTransformers = [
4951
service => ({
5052
before: [
53+
removeAsserts(service.getProgram()),
5154
renameInternals(service.getProgram(), {
5255
publicIdentifiers,
5356
prefix: '__PRIVATE_'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// eslint-disable-next-line import/no-extraneous-dependencies
19+
import * as ts from 'typescript';
20+
21+
// Location of file that includes the asserts
22+
const ASSERT_LOCATION = 'packages/firestore/src/util/assert.ts';
23+
24+
export function removeAsserts(
25+
program: ts.Program
26+
): ts.TransformerFactory<ts.SourceFile> {
27+
const removeAsserts = new RemoveAsserts(program.getTypeChecker());
28+
return (context: ts.TransformationContext) => (file: ts.SourceFile) => {
29+
return removeAsserts.visitNodeAndChildren(file, context);
30+
};
31+
}
32+
33+
/** Transformer that removes all "debugAssert" statements from the SDK. */
34+
class RemoveAsserts {
35+
constructor(private readonly typeChecker: ts.TypeChecker) {}
36+
37+
visitNodeAndChildren<T extends ts.Node>(
38+
node: T,
39+
context: ts.TransformationContext
40+
): T {
41+
return ts.visitEachChild(
42+
this.visitNode(node),
43+
(childNode: ts.Node) => this.visitNodeAndChildren(childNode, context),
44+
context
45+
) as T;
46+
}
47+
48+
visitNode(node: ts.Node): ts.Node {
49+
if (ts.isCallExpression(node)) {
50+
const signature = this.typeChecker.getResolvedSignature(node);
51+
if (
52+
signature &&
53+
signature.declaration &&
54+
signature.declaration.kind === ts.SyntaxKind.FunctionDeclaration
55+
) {
56+
const declaration = signature.declaration as ts.FunctionDeclaration;
57+
if (
58+
declaration &&
59+
declaration.getSourceFile().fileName.indexOf(ASSERT_LOCATION) >= 0
60+
) {
61+
const method = declaration.name!.text;
62+
if (method === 'debugAssert') {
63+
return ts.createEmptyStatement();
64+
}
65+
}
66+
}
67+
}
68+
return node;
69+
}
70+
}

packages/firestore/src/core/view.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ export class View {
392392
targetChange.addedDocuments.forEach(
393393
key => (this._syncedDocuments = this._syncedDocuments.add(key))
394394
);
395-
targetChange.modifiedDocuments.forEach(key =>
395+
targetChange.modifiedDocuments.forEach(key => {
396396
debugAssert(
397397
this._syncedDocuments.has(key),
398398
`Modified document ${key} not found in view.`
399-
)
400-
);
399+
);
400+
});
401401
targetChange.removedDocuments.forEach(
402402
key => (this._syncedDocuments = this._syncedDocuments.delete(key))
403403
);

0 commit comments

Comments
 (0)