Skip to content

Remove asserts #2814

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

Merged
merged 5 commits into from
Apr 8, 2020
Merged
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
6 changes: 3 additions & 3 deletions packages/firestore/rollup.config.es2017.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2018 Google Inc.
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@ import pkg from './package.json';
import memoryPkg from './memory/package.json';

import {
appendPrivatePrefixTransformers,
firestoreTransformers,
manglePrivatePropertiesOptions,
resolveNodeExterns,
resolveBrowserExterns
Expand Down Expand Up @@ -69,7 +69,7 @@ const browserBuildPlugins = [
}
},
clean: true,
transformers: appendPrivatePrefixTransformers
transformers: firestoreTransformers
}),
json({ preferConst: true }),
terser(manglePrivatePropertiesOptions)
Expand Down
7 changes: 5 additions & 2 deletions packages/firestore/rollup.shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as path from 'path';
import { externs } from './externs.json';
import { renameInternals } from './scripts/rename-internals';
import { extractPublicIdentifiers } from './scripts/extract-api';
import { removeAsserts } from './scripts/remove-asserts';

import pkg from './package.json';

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

/**
* A transformer that appends a __PRIVATE_ prefix to all internal symbols.
* Transformers that remove all debugAsserts and appends a __PRIVATE_ prefix to
* all internal symbols.
*/
export const appendPrivatePrefixTransformers = [
export const firestoreTransformers = [
service => ({
before: [
removeAsserts(service.getProgram()),
renameInternals(service.getProgram(), {
publicIdentifiers,
prefix: '__PRIVATE_'
Expand Down
70 changes: 70 additions & 0 deletions packages/firestore/scripts/remove-asserts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
import * as ts from 'typescript';

// Location of file that includes the asserts
const ASSERT_LOCATION = 'packages/firestore/src/util/assert.ts';

export function removeAsserts(
program: ts.Program
): ts.TransformerFactory<ts.SourceFile> {
const removeAsserts = new RemoveAsserts(program.getTypeChecker());
return (context: ts.TransformationContext) => (file: ts.SourceFile) => {
return removeAsserts.visitNodeAndChildren(file, context);
};
}

/** Transformer that removes all "debugAssert" statements from the SDK. */
class RemoveAsserts {
constructor(private readonly typeChecker: ts.TypeChecker) {}

visitNodeAndChildren<T extends ts.Node>(
node: T,
context: ts.TransformationContext
): T {
return ts.visitEachChild(
this.visitNode(node),
(childNode: ts.Node) => this.visitNodeAndChildren(childNode, context),
context
) as T;
}

visitNode(node: ts.Node): ts.Node {
if (ts.isCallExpression(node)) {
const signature = this.typeChecker.getResolvedSignature(node);
if (
signature &&
signature.declaration &&
signature.declaration.kind === ts.SyntaxKind.FunctionDeclaration
) {
const declaration = signature.declaration as ts.FunctionDeclaration;
if (
declaration &&
declaration.getSourceFile().fileName.indexOf(ASSERT_LOCATION) >= 0
) {
const method = declaration.name!.text;
if (method === 'debugAssert') {
return ts.createEmptyStatement();
}
}
}
}
return node;
}
}
6 changes: 3 additions & 3 deletions packages/firestore/src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ export class View {
targetChange.addedDocuments.forEach(
key => (this._syncedDocuments = this._syncedDocuments.add(key))
);
targetChange.modifiedDocuments.forEach(key =>
targetChange.modifiedDocuments.forEach(key => {
debugAssert(
this._syncedDocuments.has(key),
`Modified document ${key} not found in view.`
)
);
);
});
targetChange.removedDocuments.forEach(
key => (this._syncedDocuments = this._syncedDocuments.delete(key))
);
Expand Down