Skip to content

Commit 94ee69a

Browse files
Add tooling to verify dependency chain (#3033)
1 parent ab2e73d commit 94ee69a

14 files changed

+465
-9
lines changed

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@
5858
"integration/*"
5959
],
6060
"devDependencies": {
61+
"@microsoft/api-documenter": "7.7.20",
62+
"@microsoft/api-extractor": "7.7.13",
6163
"@types/chai": "4.2.11",
6264
"@types/chai-as-promised": "7.1.2",
6365
"@types/long": "4.0.1",
6466
"@types/mocha": "7.0.2",
6567
"@types/node": "12.12.37",
6668
"@types/sinon": "9.0.0",
6769
"@types/sinon-chai": "3.2.4",
70+
"@types/tmp": "0.2.0",
71+
"@types/yargs": "15.0.4",
6872
"@typescript-eslint/eslint-plugin": "2.30.0",
6973
"@typescript-eslint/eslint-plugin-tslint": "2.30.0",
7074
"@typescript-eslint/parser": "2.30.0",
@@ -132,9 +136,7 @@
132136
"typescript": "3.8.3",
133137
"watch": "1.0.2",
134138
"webpack": "4.43.0",
135-
"yargs": "15.3.1",
136-
"@microsoft/api-extractor": "7.7.13",
137-
"@microsoft/api-documenter": "7.7.20"
139+
"yargs": "15.3.1"
138140
},
139141
"husky": {
140142
"hooks": {

packages/firestore/exp/index.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
export function foo(): string;
19+
export function bar(): string;

packages/firestore/exp/index.node.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
export { foo, bar } from './src/api/foobar';
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
export function foo(): string {
19+
return bar();
20+
}
21+
22+
export function bar(): string {
23+
return 'bar';
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"bar": {
3+
"dependencies": {
4+
"functions": [
5+
"bar"
6+
],
7+
"classes": [],
8+
"variables": []
9+
},
10+
"sizeInBytes": 39
11+
},
12+
"foo": {
13+
"dependencies": {
14+
"functions": [
15+
"bar",
16+
"foo"
17+
],
18+
"classes": [],
19+
"variables": []
20+
},
21+
"sizeInBytes": 67
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import { expect } from 'chai';
19+
20+
import { extractDependencies } from '../../../../../scripts/exp/extract-deps.helpers';
21+
22+
import * as dependencies from './dependencies.json';
23+
import * as pkg from '../../../package.json';
24+
import { forEach } from '../../../src/util/obj';
25+
26+
describe('Dependencies', () => {
27+
forEach(dependencies, (api, { dependencies }) => {
28+
it(api, () => {
29+
return extractDependencies(api, pkg.exp).then(extractedDependencies => {
30+
expect(extractedDependencies.classes).to.have.members(
31+
dependencies.classes,
32+
'for classes'
33+
);
34+
expect(extractedDependencies.functions).to.have.members(
35+
dependencies.functions,
36+
'for functions'
37+
);
38+
expect(extractedDependencies.variables).to.have.members(
39+
dependencies.variables,
40+
'for variables'
41+
);
42+
});
43+
});
44+
});
45+
});

packages/firestore/externs.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"packages/webchannel-wrapper/src/index.d.ts",
2424
"packages/util/dist/src/environment.d.ts",
2525
"packages/firestore/src/protos/firestore_proto_api.d.ts",
26-
"packages/firestore/dist/lib/src/util/error.d.ts",
27-
"packages/firestore/dist/lib/src/local/indexeddb_schema.d.ts",
28-
"packages/firestore/dist/lib/src/local/shared_client_state_schema.d.ts"
26+
"packages/firestore/src/util/error.ts",
27+
"packages/firestore/src/local/indexeddb_schema.ts",
28+
"packages/firestore/src/local/shared_client_state_schema.ts"
2929
]
3030
}

packages/firestore/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
"description": "The Cloud Firestore component of the Firebase JS SDK.",
88
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
99
"scripts": {
10-
"prebuild": "tsc -d --downlevelIteration --declarationDir dist/lib --emitDeclarationOnly && tsc -m es2015 --moduleResolution node scripts/*.ts ",
10+
"prebuild": "tsc -m es2015 --moduleResolution node scripts/*.ts ",
1111
"build": "rollup -c rollup.config.es2017.js && rollup -c rollup.config.es5.js",
1212
"build:deps": "lerna run --scope @firebase/'{app,firestore}' --include-dependencies build",
1313
"build:console": "node tools/console.build.js",
14+
"build:exp": "rollup -c rollup.config.exp.js",
1415
"predev": "yarn prebuild",
1516
"dev": "rollup -c -w",
1617
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1718
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
18-
"prettier": "prettier --write '*.ts' '*.js' 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
19+
"prettier": "prettier --write '*.ts' '*.js' 'exp/**/*.ts' 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
20+
"pregendeps:exp": "yarn build:exp",
21+
"gendeps:exp": "../../scripts/exp/extract-deps.sh --types ./exp/index.d.ts --bundle ./dist/exp/index.js --output ./exp/test/deps/dependencies.json",
22+
"pretest:exp": "yarn build:exp",
23+
"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",
1924
"test": "run-s lint test:all",
2025
"test:ci": "node ../../scripts/run_tests_in_ci.js",
2126
"test:all": "run-p test:browser test:travis test:minified",
@@ -34,6 +39,7 @@
3439
"browser": "dist/index.cjs.js",
3540
"module": "dist/index.esm.js",
3641
"esm2017": "dist/index.esm2017.js",
42+
"exp": "dist/exp/index.js",
3743
"license": "Apache-2.0",
3844
"files": [
3945
"dist",
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
import typescriptPlugin from 'rollup-plugin-typescript2';
19+
import typescript from 'typescript';
20+
21+
import { resolveNodeExterns } from './rollup.shared';
22+
23+
import pkg from './package.json';
24+
25+
const defaultPlugins = [
26+
typescriptPlugin({
27+
typescript,
28+
tsconfigOverride: {
29+
compilerOptions: {
30+
target: 'es2017'
31+
}
32+
},
33+
clean: true
34+
})
35+
];
36+
37+
const nodeBuilds = [
38+
{
39+
input: './exp/index.node.ts',
40+
output: {
41+
file: pkg.exp,
42+
format: 'es'
43+
},
44+
plugins: defaultPlugins,
45+
external: resolveNodeExterns,
46+
treeshake: {
47+
tryCatchDeoptimization: false
48+
}
49+
}
50+
];
51+
52+
export default [...nodeBuilds];

packages/firestore/scripts/extract-api.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ function extractIdentifiersFromNodeAndChildren(
3232
);
3333
}
3434

35+
/** Generates the "d.ts" content for `fileName`. */
36+
function extractTypeDeclaration(fileName: string): string {
37+
let result: string;
38+
const compilerOptions = { declaration: true, emitDeclarationOnly: true };
39+
const host = ts.createCompilerHost(compilerOptions);
40+
host.writeFile = (_: string, contents: string) => (result = contents);
41+
const program = ts.createProgram([fileName], compilerOptions, host);
42+
program.emit();
43+
return result!;
44+
}
45+
3546
/**
3647
* Traverses TypeScript type definition files and returns the list of referenced
3748
* identifiers.
@@ -41,12 +52,21 @@ export function extractPublicIdentifiers(filePaths: string[]): Set<string> {
4152

4253
for (const filePath of filePaths) {
4354
const contents = fs.readFileSync(filePath, { encoding: 'UTF-8' });
44-
const sourceFile = ts.createSourceFile(
55+
let sourceFile = ts.createSourceFile(
4556
filePath,
4657
contents,
4758
ts.ScriptTarget.ES2015
4859
);
4960

61+
if (!sourceFile.isDeclarationFile) {
62+
const dtsSource = extractTypeDeclaration(filePath);
63+
sourceFile = ts.createSourceFile(
64+
filePath.replace('.ts', '.d.ts'),
65+
dtsSource,
66+
ts.ScriptTarget.ES2015
67+
);
68+
}
69+
5070
const identifiers = new Set<string>();
5171
ts.forEachChild(sourceFile, (childNode: ts.Node) =>
5272
extractIdentifiersFromNodeAndChildren(childNode, identifiers)

0 commit comments

Comments
 (0)