Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c003de7

Browse filesBrowse files
committed
Allow allowJs and declaration to be used together
This intorduces a new symbol-based declaration emitter - currently this is only used for JSON and JavaScript, as the output is likely worse than what the other declaration emitter is capable of. In addition, it is still incomplete - it does not yet support serializaing namespaces.
1 parent 6839973 commit c003de7
Copy full SHA for c003de7

102 files changed

+6,455-194Lines changed: 6455 additions & 194 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+590-1Lines changed: 590 additions & 1 deletion
Large diffs are not rendered by default.
Collapse file

‎src/compiler/emitter.ts‎

Copy file name to clipboardExpand all lines: src/compiler/emitter.ts
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ namespace ts {
9797
comparePaths(sourceFile.fileName, ownOutputFilePath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo;
9898
const jsFilePath = options.emitDeclarationOnly || isJsonEmittedToSameLocation ? undefined : ownOutputFilePath;
9999
const sourceMapFilePath = !jsFilePath || isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
100-
// For legacy reasons (ie, we have baselines capturing the behavior), js files don't report a .d.ts output path - this would only matter if `declaration` and `allowJs` were both on, which is currently an error
101-
const isJs = isSourceFileJS(sourceFile);
102-
const declarationFilePath = ((forceDtsPaths || getEmitDeclarations(options)) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
100+
const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
103101
const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
104102
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, buildInfoPath: undefined };
105103
}
@@ -351,17 +349,16 @@ namespace ts {
351349
declarationFilePath: string | undefined,
352350
declarationMapPath: string | undefined,
353351
relativeToBuildInfo: (path: string) => string) {
354-
if (!sourceFileOrBundle || !(declarationFilePath && !isInJSFile(sourceFileOrBundle))) {
352+
if (!sourceFileOrBundle || !declarationFilePath) {
355353
return;
356354
}
357355
const sourceFiles = isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : sourceFileOrBundle.sourceFiles;
358356
// Setup and perform the transformation to retrieve declarations from the input files
359-
const nonJsFiles = filter(sourceFiles, isSourceFileNotJS);
360-
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(nonJsFiles, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : nonJsFiles;
357+
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(sourceFiles, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : sourceFiles;
361358
if (emitOnlyDtsFiles && !getEmitDeclarations(compilerOptions)) {
362359
// Checker wont collect the linked aliases since thats only done when declaration is enabled.
363360
// Do that here when emitting only dts files
364-
nonJsFiles.forEach(collectLinkedAliases);
361+
sourceFiles.forEach(collectLinkedAliases);
365362
}
366363
const declarationTransform = transformNodes(resolver, host, compilerOptions, inputListOrBundle, declarationTransformers, /*allowDtsFiles*/ false);
367364
if (length(declarationTransform.diagnostics)) {
@@ -615,6 +612,7 @@ namespace ts {
615612
getAllAccessorDeclarations: notImplemented,
616613
getSymbolOfExternalModuleSpecifier: notImplemented,
617614
isBindingCapturedByNode: notImplemented,
615+
getDeclarationStatementsForSourceFile: notImplemented,
618616
};
619617

620618
/*@internal*/
Collapse file

‎src/compiler/factory.ts‎

Copy file name to clipboardExpand all lines: src/compiler/factory.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,11 @@ namespace ts {
21392139
: node;
21402140
}
21412141

2142+
/* @internal */
2143+
export function createEmptyExports() {
2144+
return createExportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createNamedExports([]), /*moduleSpecifier*/ undefined);
2145+
}
2146+
21422147
export function createNamedExports(elements: ReadonlyArray<ExportSpecifier>) {
21432148
const node = <NamedExports>createSynthesizedNode(SyntaxKind.NamedExports);
21442149
node.elements = createNodeArray(elements);
Collapse file

‎src/compiler/program.ts‎

Copy file name to clipboardExpand all lines: src/compiler/program.ts
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,10 +2906,6 @@ namespace ts {
29062906
}
29072907
}
29082908

2909-
if (!options.noEmit && options.allowJs && getEmitDeclarations(options)) {
2910-
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_with_option_1, "allowJs", getEmitDeclarationOptionName(options));
2911-
}
2912-
29132909
if (options.checkJs && !options.allowJs) {
29142910
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs"));
29152911
}
Collapse file

‎src/compiler/transformers/declarations.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/declarations.ts
+9-14Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/*@internal*/
22
namespace ts {
33
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] | undefined {
4-
if (file && isSourceFileJS(file)) {
5-
return []; // No declaration diagnostics for js for now
6-
}
74
const compilerOptions = host.getCompilerOptions();
8-
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJS), [transformDeclarations], /*allowDtsFiles*/ false);
5+
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : host.getSourceFiles(), [transformDeclarations], /*allowDtsFiles*/ false);
96
return result.diagnostics;
107
}
118

@@ -190,15 +187,11 @@ namespace ts {
190187
}
191188
}
192189

193-
function createEmptyExports() {
194-
return createExportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createNamedExports([]), /*moduleSpecifier*/ undefined);
195-
}
196-
197190
function transformRoot(node: Bundle): Bundle;
198191
function transformRoot(node: SourceFile): SourceFile;
199192
function transformRoot(node: SourceFile | Bundle): SourceFile | Bundle;
200193
function transformRoot(node: SourceFile | Bundle) {
201-
if (node.kind === SyntaxKind.SourceFile && (node.isDeclarationFile || isSourceFileJS(node))) {
194+
if (node.kind === SyntaxKind.SourceFile && node.isDeclarationFile) {
202195
return node;
203196
}
204197

@@ -209,7 +202,10 @@ namespace ts {
209202
let hasNoDefaultLib = false;
210203
const bundle = createBundle(map(node.sourceFiles,
211204
sourceFile => {
212-
if (sourceFile.isDeclarationFile || isSourceFileJS(sourceFile)) return undefined!; // Omit declaration files from bundle results, too // TODO: GH#18217
205+
if (sourceFile.isDeclarationFile) return undefined!; // Omit declaration files from bundle results, too // TODO: GH#18217
206+
if (isSourceFileJS(sourceFile)) {
207+
return updateSourceFileNode(sourceFile, resolver.getDeclarationStatementsForSourceFile(sourceFile, declarationEmitNodeBuilderFlags, symbolTracker) || emptyArray);
208+
}
213209
hasNoDefaultLib = hasNoDefaultLib || sourceFile.hasNoDefaultLib;
214210
currentSourceFile = sourceFile;
215211
enclosingDeclaration = sourceFile;
@@ -257,6 +253,9 @@ namespace ts {
257253
refs.forEach(referenceVisitor);
258254
return bundle;
259255
}
256+
else if (isSourceFileJS(node)) {
257+
return updateSourceFileNode(node, resolver.getDeclarationStatementsForSourceFile(node, declarationEmitNodeBuilderFlags, symbolTracker) || emptyArray);
258+
}
260259

261260
// Single source file
262261
needsDeclare = true;
@@ -710,10 +709,6 @@ namespace ts {
710709
return isAnyImportOrReExport(result) || isExportAssignment(result) || hasModifier(result, ModifierFlags.Export);
711710
}
712711

713-
function needsScopeMarker(result: LateVisibilityPaintedStatement | ExportAssignment) {
714-
return !isAnyImportOrReExport(result) && !isExportAssignment(result) && !hasModifier(result, ModifierFlags.Export) && !isAmbientModule(result);
715-
}
716-
717712
function visitDeclarationSubtree(input: Node): VisitResult<Node> {
718713
if (shouldStripInternal(input)) return;
719714
if (isDeclaration(input)) {
Collapse file

‎src/compiler/types.ts‎

Copy file name to clipboardExpand all lines: src/compiler/types.ts
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,7 @@ namespace ts {
36203620
getAllAccessorDeclarations(declaration: AccessorDeclaration): AllAccessorDeclarations;
36213621
getSymbolOfExternalModuleSpecifier(node: StringLiteralLike): Symbol | undefined;
36223622
isBindingCapturedByNode(node: Node, decl: VariableDeclaration | BindingElement): boolean;
3623+
getDeclarationStatementsForSourceFile(node: SourceFile, flags: NodeBuilderFlags, tracker: SymbolTracker): Statement[] | undefined;
36233624
}
36243625

36253626
export const enum SymbolFlags {
@@ -3700,6 +3701,12 @@ namespace ts {
37003701

37013702
ClassMember = Method | Accessor | Property,
37023703

3704+
/* @internal */
3705+
ExportSupportsDefaultModifier = Class | Function | Interface,
3706+
3707+
/* @internal */
3708+
ExportDoesNotSupportDefaultModifier = ~ExportSupportsDefaultModifier,
3709+
37033710
/* @internal */
37043711
// The set of things we consider semantically classifiable. Used to speed up the LS during
37053712
// classification.
Collapse file

‎src/compiler/utilities.ts‎

Copy file name to clipboardExpand all lines: src/compiler/utilities.ts
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2561,10 +2561,14 @@ namespace ts {
25612561
}
25622562

25632563
export function exportAssignmentIsAlias(node: ExportAssignment | BinaryExpression): boolean {
2564-
const e = isExportAssignment(node) ? node.expression : node.right;
2564+
const e = getExportAssignmentExpression(node);
25652565
return isEntityNameExpression(e) || isClassExpression(e);
25662566
}
25672567

2568+
export function getExportAssignmentExpression(node: ExportAssignment | BinaryExpression): Expression {
2569+
return isExportAssignment(node) ? node.expression : node.right;
2570+
}
2571+
25682572
export function getEffectiveBaseTypeNode(node: ClassLikeDeclaration | InterfaceDeclaration) {
25692573
const baseType = getClassExtendsHeritageElement(node);
25702574
if (baseType && isInJSFile(node)) {
@@ -6656,6 +6660,21 @@ namespace ts {
66566660
return false;
66576661
}
66586662

6663+
/* @internal */
6664+
export function isScopeMarker(node: Node) {
6665+
return isExportAssignment(node) || isExportDeclaration(node);
6666+
}
6667+
6668+
/* @internal */
6669+
export function hasScopeMarker(statements: ReadonlyArray<Statement>) {
6670+
return some(statements, isScopeMarker);
6671+
}
6672+
6673+
/* @internal */
6674+
export function needsScopeMarker(result: Statement) {
6675+
return !isAnyImportOrReExport(result) && !isExportAssignment(result) && !hasModifier(result, ModifierFlags.Export) && !isAmbientModule(result);
6676+
}
6677+
66596678
/* @internal */
66606679
export function isForInOrOfStatement(node: Node): node is ForInOrOfStatement {
66616680
return node.kind === SyntaxKind.ForInStatement || node.kind === SyntaxKind.ForOfStatement;
Collapse file

‎src/harness/compiler.ts‎

Copy file name to clipboardExpand all lines: src/harness/compiler.ts
+13-8Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,19 @@ namespace compiler {
219219
return vpath.changeExtension(path, ext);
220220
}
221221

222-
public getNumberOfJsFiles() {
223-
let count = this.js.size;
224-
this.js.forEach(document => {
225-
if (ts.fileExtensionIs(document.file, ts.Extension.Json)) {
226-
count--;
227-
}
228-
});
229-
return count;
222+
public getNumberOfJsFiles(includeJson: boolean) {
223+
if (includeJson) {
224+
return this.js.size;
225+
}
226+
else {
227+
let count = this.js.size;
228+
this.js.forEach(document => {
229+
if (ts.fileExtensionIs(document.file, ts.Extension.Json)) {
230+
count--;
231+
}
232+
});
233+
return count;
234+
}
230235
}
231236
}
232237

Collapse file

‎src/harness/harness.ts‎

Copy file name to clipboardExpand all lines: src/harness/harness.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ namespace Harness {
883883
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
884884
}
885885
}
886-
else if (result.dts.size !== result.getNumberOfJsFiles()) {
886+
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ true)) {
887887
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
888888
}
889889
}
@@ -902,7 +902,7 @@ namespace Harness {
902902
if (vpath.isDeclaration(file.unitName) || vpath.isJson(file.unitName)) {
903903
dtsFiles.push(file);
904904
}
905-
else if (vpath.isTypeScript(file.unitName)) {
905+
else if (vpath.isTypeScript(file.unitName) || (vpath.isJavaScript(file.unitName) && options.allowJs)) {
906906
const declFile = findResultCodeFile(file.unitName);
907907
if (declFile && !findUnit(declFile.file, declInputFiles) && !findUnit(declFile.file, declOtherFiles)) {
908908
dtsFiles.push({ unitName: declFile.file, content: utils.removeByteOrderMark(declFile.text) });
@@ -1267,7 +1267,7 @@ namespace Harness {
12671267
return;
12681268
}
12691269
else if (options.sourceMap || declMaps) {
1270-
if (result.maps.size !== (result.getNumberOfJsFiles() * (declMaps && options.sourceMap ? 2 : 1))) {
1270+
if (result.maps.size !== ((options.sourceMap ? result.getNumberOfJsFiles(/*includeJson*/ false) : 0) + (declMaps ? result.getNumberOfJsFiles(/*includeJson*/ true) : 0))) {
12711271
throw new Error("Number of sourcemap files should be same as js files.");
12721272
}
12731273

Collapse file

‎src/testRunner/unittests/tsbuild/resolveJsonModule.ts‎

Copy file name to clipboardExpand all lines: src/testRunner/unittests/tsbuild/resolveJsonModule.ts
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ namespace ts {
4747
4848
export default hello.hello`);
4949
const allExpectedOutputs = ["/src/dist/src/index.js", "/src/dist/src/index.d.ts", "/src/dist/src/index.json"];
50-
verifyProjectWithResolveJsonModuleWithFs(fs, "/src/tsconfig_withIncludeOfJson.json", allExpectedOutputs);
50+
verifyProjectWithResolveJsonModuleWithFs(
51+
fs,
52+
"/src/tsconfig_withIncludeOfJson.json",
53+
allExpectedOutputs,
54+
[Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, "/src/dist/src/index.d.ts"]
55+
);
5156
});
5257

5358
it("with resolveJsonModule and files containing json file", () => {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.