diff --git a/packages/firestore/rollup.config.es2017.js b/packages/firestore/rollup.config.es2017.js index 0577c4ff137..fcabdabe0b1 100644 --- a/packages/firestore/rollup.config.es2017.js +++ b/packages/firestore/rollup.config.es2017.js @@ -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. @@ -28,7 +28,7 @@ import pkg from './package.json'; import memoryPkg from './memory/package.json'; import { - appendPrivatePrefixTransformers, + firestoreTransformers, manglePrivatePropertiesOptions, resolveNodeExterns, resolveBrowserExterns @@ -69,7 +69,7 @@ const browserBuildPlugins = [ } }, clean: true, - transformers: appendPrivatePrefixTransformers + transformers: firestoreTransformers }), json({ preferConst: true }), terser(manglePrivatePropertiesOptions) diff --git a/packages/firestore/rollup.shared.js b/packages/firestore/rollup.shared.js index 77ccf83a3ee..5ce613b1777 100644 --- a/packages/firestore/rollup.shared.js +++ b/packages/firestore/rollup.shared.js @@ -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'; @@ -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_' diff --git a/packages/firestore/scripts/remove-asserts.ts b/packages/firestore/scripts/remove-asserts.ts new file mode 100644 index 00000000000..1345852b6cf --- /dev/null +++ b/packages/firestore/scripts/remove-asserts.ts @@ -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 { + 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( + 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; + } +} diff --git a/packages/firestore/src/core/view.ts b/packages/firestore/src/core/view.ts index 68382e967c3..c9f34f08103 100644 --- a/packages/firestore/src/core/view.ts +++ b/packages/firestore/src/core/view.ts @@ -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)) );