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 1 commit
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.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import pkg from './package.json';
import memoryPkg from './memory/package.json';

import {
appendPrivatePrefixTransformers,
firestoreTransformers,
manglePrivatePropertiesOptions
} from './terser.config';

Expand Down Expand Up @@ -102,7 +102,7 @@ export function resolveMemoryExterns(deps, externsId, referencedBy) {
const es5BuildPlugins = [
typescriptPlugin({
typescript,
transformers: appendPrivatePrefixTransformers,
transformers: firestoreTransformers,
cacheRoot: `./.cache/es5.mangled/`
}),
json(),
Expand All @@ -118,7 +118,7 @@ const es2017BuildPlugins = [
}
},
cacheRoot: './.cache/es2017.mangled/',
transformers: appendPrivatePrefixTransformers
transformers: firestoreTransformers
}),
json({ preferConst: true }),
terser(manglePrivatePropertiesOptions)
Expand Down
56 changes: 56 additions & 0 deletions packages/firestore/scripts/remove-asserts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import * as ts from 'typescript';

// Location of file that includes the asserts
const DECLARING_FILE = "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 "asserts" and "fail" statement 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(
DECLARING_FILE) >= 0) {
const method = declaration.name!.text;
if (method === 'assert') {
return ts.createEmptyStatement();
} else if (method === 'fail') {
if (node.parent.kind === ts.SyntaxKind.ExpressionStatement) {
return ts.createEmptyStatement();
} else {
return ts.updateCall(node, node.expression, [], []);
}
}
}
}
}
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 @@ -389,12 +389,12 @@ export class View {
targetChange.addedDocuments.forEach(
key => (this._syncedDocuments = this._syncedDocuments.add(key))
);
targetChange.modifiedDocuments.forEach(key =>
targetChange.modifiedDocuments.forEach(key => {
assert(
this._syncedDocuments.has(key),
`Modified document ${key} not found in view.`
)
);
);
});
targetChange.removedDocuments.forEach(
key => (this._syncedDocuments = this._syncedDocuments.delete(key))
);
Expand Down
7 changes: 5 additions & 2 deletions packages/firestore/terser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ 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';

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 asserts 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