forked from facebook/lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-tsc-types.js
88 lines (83 loc) · 2.62 KB
/
validate-tsc-types.js
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
// @ts-check
'use strict';
const fs = require('fs-extra');
const glob = require('glob');
const ts = require('typescript');
const pretty = process.env.CI !== 'true';
/** @type {ts.FormatDiagnosticsHost} */
const diagnosticsHost = {
getCanonicalFileName: (fn) => fn,
getCurrentDirectory: () => './',
getNewLine: () => '\n',
};
/**
* Validate that the published .d.ts types do not have dependencies
* on any private module (currently shared/*).
*
* `process.exit(1)` on failure.
*/
function validateTscTypes() {
const dtsFilesPattern = './.ts-temp/packages/{lexical,lexical-*}/**/*.d.ts';
const dtsFiles = glob.sync(dtsFilesPattern);
if (dtsFiles.length === 0) {
console.error(
`Missing ${dtsFilesPattern}, \`npm run build-prod\` or \`npm run build-release\` first`,
);
process.exit(1);
}
/** @type {ts.Diagnostic[]} */
const diagnostics = [];
for (const fn of dtsFiles) {
// console.log(fn);
const ast = ts.createSourceFile(
fn,
fs.readFileSync(fn, 'utf-8'),
ts.ScriptTarget.Latest,
);
const checkSpecifier = (/** @type {ts.Node | undefined} */ node) => {
if (!node || node.kind !== ts.SyntaxKind.StringLiteral) {
return;
}
const specifier = /** @type {import('typescript').StringLiteral} */ (
node
);
if (/^(shared|scripts)(\/|$)/.test(specifier.text)) {
const start = specifier.getStart(ast);
diagnostics.push({
category: ts.DiagnosticCategory.Error,
code: Infinity,
file: ast,
length: specifier.getEnd() - start,
messageText: `Published .d.ts files must not import private module '${specifier.text}'.`,
start,
});
}
};
ast.forEachChild((node) => {
if (node.kind === ts.SyntaxKind.ExportDeclaration) {
const exportNode =
/** @type {import('typescript').ExportDeclaration} */ (node);
checkSpecifier(exportNode.moduleSpecifier);
} else if (node.kind === ts.SyntaxKind.ImportDeclaration) {
const importNode =
/** @type {import('typescript').ImportDeclaration} */ (node);
checkSpecifier(importNode.moduleSpecifier);
}
});
}
if (diagnostics.length > 0) {
const msg = (
pretty ? ts.formatDiagnosticsWithColorAndContext : ts.formatDiagnostics
)(diagnostics, diagnosticsHost);
console.error(msg.replace(/ TSInfinity:/g, ':'));
process.exit(1);
}
}
validateTscTypes();