Skip to content

Add tooling to verify dependency chain #3033

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 12 commits into from
May 14, 2020
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@
"integration/*"
],
"devDependencies": {
"@microsoft/api-documenter": "7.7.20",
"@microsoft/api-extractor": "7.7.13",
"@types/chai": "4.2.11",
"@types/chai-as-promised": "7.1.2",
"@types/long": "4.0.1",
"@types/mocha": "7.0.2",
"@types/node": "12.12.37",
"@types/sinon": "9.0.0",
"@types/sinon-chai": "3.2.4",
"@types/tmp": "0.2.0",
"@types/yargs": "15.0.4",
"@typescript-eslint/eslint-plugin": "2.30.0",
"@typescript-eslint/eslint-plugin-tslint": "2.30.0",
"@typescript-eslint/parser": "2.30.0",
Expand Down Expand Up @@ -132,9 +136,7 @@
"typescript": "3.8.3",
"watch": "1.0.2",
"webpack": "4.43.0",
"yargs": "15.3.1",
"@microsoft/api-extractor": "7.7.13",
"@microsoft/api-documenter": "7.7.20"
"yargs": "15.3.1"
},
"husky": {
"hooks": {
Expand Down
19 changes: 19 additions & 0 deletions packages/firestore/exp/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @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.
*/

export function foo(): string;
export function bar(): string;
18 changes: 18 additions & 0 deletions packages/firestore/exp/index.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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.
*/

export { foo, bar } from './src/api/foobar';
24 changes: 24 additions & 0 deletions packages/firestore/exp/src/api/foobar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @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.
*/

export function foo(): string {
return bar();
}

export function bar(): string {
return 'bar';
}
23 changes: 23 additions & 0 deletions packages/firestore/exp/test/deps/dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This file is generated by the Node CLI tool and then used in the unit test as the source of truth. It is checked in and as such provides a nice diff as we make changes.

"sizeInBytes" is for reference only.

"bar": {
"dependencies": {
"functions": [
"bar"
],
"classes": [],
"variables": []
},
"sizeInBytes": 39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is per function? seems like a pain to always update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is auto-updated via the script, but the script is not run automatically. I'll send this to @Feiyang1 and we can ponder how we can automate this step.

},
"foo": {
"dependencies": {
"functions": [
"bar",
"foo"
],
"classes": [],
"variables": []
},
"sizeInBytes": 67
}
}
45 changes: 45 additions & 0 deletions packages/firestore/exp/test/deps/verify_dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @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.
*/

import { expect } from 'chai';

import { extractDependencies } from '../../../../../scripts/exp/extract-deps.helpers';

import * as dependencies from './dependencies.json';
import * as pkg from '../../../package.json';
import { forEach } from '../../../src/util/obj';

describe('Dependencies', () => {
forEach(dependencies, (api, { dependencies }) => {
it(api, () => {
return extractDependencies(api, pkg.exp).then(extractedDependencies => {
expect(extractedDependencies.classes).to.have.members(
dependencies.classes,
'for classes'
);
expect(extractedDependencies.functions).to.have.members(
dependencies.functions,
'for functions'
);
expect(extractedDependencies.variables).to.have.members(
dependencies.variables,
'for variables'
);
});
});
});
});
6 changes: 3 additions & 3 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"packages/webchannel-wrapper/src/index.d.ts",
"packages/util/dist/src/environment.d.ts",
"packages/firestore/src/protos/firestore_proto_api.d.ts",
"packages/firestore/dist/lib/src/util/error.d.ts",
"packages/firestore/dist/lib/src/local/indexeddb_schema.d.ts",
"packages/firestore/dist/lib/src/local/shared_client_state_schema.d.ts"
"packages/firestore/src/util/error.ts",
"packages/firestore/src/local/indexeddb_schema.ts",
"packages/firestore/src/local/shared_client_state_schema.ts"
]
}
10 changes: 8 additions & 2 deletions packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
"description": "The Cloud Firestore component of the Firebase JS SDK.",
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
"scripts": {
"prebuild": "tsc -d --downlevelIteration --declarationDir dist/lib --emitDeclarationOnly && tsc -m es2015 --moduleResolution node scripts/*.ts ",
"prebuild": "tsc -m es2015 --moduleResolution node scripts/*.ts ",
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js",
"build:deps": "lerna run --scope @firebase/'{app,firestore}' --include-dependencies build",
"build:console": "node tools/console.build.js",
"build:exp": "rollup -c rollup.config.exp.js",
"predev": "yarn prebuild",
"dev": "rollup -c -w",
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
"prettier": "prettier --write '*.ts' '*.js' 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
"prettier": "prettier --write '*.ts' '*.js' 'exp/**/*.ts' 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
"pregendeps:exp": "yarn build:exp",
"gendeps:exp": "../../scripts/exp/extract-deps.sh --types ./exp/index.d.ts --bundle ./dist/exp/index.js --output ./exp/test/deps/dependencies.json",
"pretest:exp": "yarn build:exp",
"test:exp": "TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'exp/test/**/*.test.ts' --file exp/index.node.ts --config ../../config/mocharc.node.js",
"test": "run-s lint test:all",
"test:ci": "node ../../scripts/run_tests_in_ci.js",
"test:all": "run-p test:browser test:travis test:minified",
Expand All @@ -34,6 +39,7 @@
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"esm2017": "dist/index.esm2017.js",
"exp": "dist/exp/index.js",
"license": "Apache-2.0",
"files": [
"dist",
Expand Down
52 changes: 52 additions & 0 deletions packages/firestore/rollup.config.exp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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.
*/

import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';

import { resolveNodeExterns } from './rollup.shared';

import pkg from './package.json';

const defaultPlugins = [
typescriptPlugin({
typescript,
tsconfigOverride: {
compilerOptions: {
target: 'es2017'
}
},
clean: true
})
];

const nodeBuilds = [
{
input: './exp/index.node.ts',
output: {
file: pkg.exp,
format: 'es'
},
plugins: defaultPlugins,
external: resolveNodeExterns,
treeshake: {
tryCatchDeoptimization: false
}
}
];

export default [...nodeBuilds];
22 changes: 21 additions & 1 deletion packages/firestore/scripts/extract-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ function extractIdentifiersFromNodeAndChildren(
);
}

/** Generates the "d.ts" content for `fileName`. */
function extractTypeDeclaration(fileName: string): string {
let result: string;
const compilerOptions = { declaration: true, emitDeclarationOnly: true };
const host = ts.createCompilerHost(compilerOptions);
host.writeFile = (_: string, contents: string) => (result = contents);
const program = ts.createProgram([fileName], compilerOptions, host);
program.emit();
return result!;
}

/**
* Traverses TypeScript type definition files and returns the list of referenced
* identifiers.
Expand All @@ -41,12 +52,21 @@ export function extractPublicIdentifiers(filePaths: string[]): Set<string> {

for (const filePath of filePaths) {
const contents = fs.readFileSync(filePath, { encoding: 'UTF-8' });
const sourceFile = ts.createSourceFile(
let sourceFile = ts.createSourceFile(
filePath,
contents,
ts.ScriptTarget.ES2015
);

if (!sourceFile.isDeclarationFile) {
const dtsSource = extractTypeDeclaration(filePath);
sourceFile = ts.createSourceFile(
filePath.replace('.ts', '.d.ts'),
dtsSource,
ts.ScriptTarget.ES2015
);
}

const identifiers = new Set<string>();
ts.forEachChild(sourceFile, (childNode: ts.Node) =>
extractIdentifiersFromNodeAndChildren(childNode, identifiers)
Expand Down
Loading