forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-public-api-formats.js
More file actions
102 lines (91 loc) · 3.87 KB
/
Copy pathcheck-public-api-formats.js
File metadata and controls
102 lines (91 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs');
const path = require('path');
const ts = require('typescript');
const chalk = require('chalk');
/** -------------------------------------------------------------------------------------------- */
/** -------------------------------------- ERROR HANDLING -------------------------------------- */
/** -------------------------------------------------------------------------------------------- */
/** Stores all errors that are caught during execution. */
const errors = [];
/** Throws an error explaining that the given export node must be explicit. */
function throwExplicitExportError(path, content, node) {
const invalidExport = content
.substring(node.jsDoc ? node.jsDoc[0].end : node.pos, node.end)
.trim();
errors.push(
chalk.red(
`ERROR ${errors.length + 1}:` +
`\n File: ${path}` +
`\n Line: "${invalidExport}"` +
`\n Re-exports must be explicit. For example:` +
`\n [x] export * from './foo';` +
`\n [✓] export {MatFoo} from './foo';`,
),
);
}
/** Throws an error explaining that the name(s) of given export node must contain "legacy". */
function throwLegacyNamingError(path, node) {
const invalidSymbol = node.exportClause.elements
.map(n => n.name.escapedText)
.filter(n => !n.includes('legacy'))
.map(n => ` * ${n}`)
.join('\n');
errors.push(
chalk.red(
`ERROR ${errors.length + 1}:` +
`\n File: ${path}` +
`\n The following exported symbols do not contain 'legacy':` +
`\n${invalidSymbol}`,
),
);
}
/** -------------------------------------------------------------------------------------------- */
/** --------------------------------------- BEGIN SCRIPT --------------------------------------- */
/** -------------------------------------------------------------------------------------------- */
/** An array of the file paths of all public-api.ts files for legacy components. */
const paths = fs
.readdirSync(path.join(path.dirname(__dirname), 'src/material'))
.filter(dir => dir.startsWith('legacy-'))
.filter(dir => dir !== 'legacy-prebuilt-themes' && dir !== 'legacy-core')
.flatMap(dir => {
const base = path.join(path.dirname(__dirname), 'src/material', dir);
return [path.join(base, 'public-api.ts'), path.join(base, 'testing/public-api.ts')];
});
for (let i = 0; i < paths.length; i++) {
const content = fs.readFileSync(paths[i], 'utf8');
ts.createSourceFile(paths[i], content, ts.ScriptTarget.Latest).forEachChild(child => {
// todo: consider enforcing this for all public-api.ts files.
if (!ts.isExportDeclaration(child)) {
return;
}
// Do not allow wildcard forwarding of exports.
// E.g. `export * from './foo';` vs `export {Foo} from './foo';`.
if (!child.exportClause) {
throwExplicitExportError(paths[i], content, child);
return;
}
// Ensure all exports from public-api.ts files nested
// under src/legacy-* directory contain the word "legacy".
for (let j = 0; j < child.exportClause.elements.length; j++) {
if (!child.exportClause.elements[j].name.escapedText.toLowerCase().includes('legacy')) {
throwLegacyNamingError(paths[i], child);
return;
}
}
});
}
/** -------------------------------------------------------------------------------------------- */
/** ------------------------------------- ERROR REPORTING -------------------------------------- */
/** -------------------------------------------------------------------------------------------- */
if (errors.length) {
const separator = chalk.red('\n-----------------------------------------------------\n');
console.log(
chalk.red(`\nPublic APIs check failed with ${errors.length} error(s)`),
separator,
errors.join(`${separator}`),
separator,
);
process.exitCode = 1;
} else {
console.log(chalk.green('✓ All public-api format checks passed!'));
}