Skip to content

Commit 3d420f1

Browse files
author
mmews-n4
authored
GH-2595: The d.ts export should append .js file extensions in import statements (#2598)
* d.ts exports emit type imports including file extension * respect existing file extension * add test * fix fileExtension() * fix xt framework * respect bare imports * add test
1 parent fa8bd23 commit 3d420f1

File tree

19 files changed

+155
-25
lines changed

19 files changed

+155
-25
lines changed

plugins/org.eclipse.n4js.transpiler.dts/src/org/eclipse/n4js/transpiler/dts/print/PrettyPrinterDts.java

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.eclipse.n4js.n4JS.ImportDeclaration;
4444
import org.eclipse.n4js.n4JS.ImportSpecifier;
4545
import org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName;
46+
import org.eclipse.n4js.n4JS.ModuleSpecifierForm;
4647
import org.eclipse.n4js.n4JS.N4ClassDeclaration;
4748
import org.eclipse.n4js.n4JS.N4ClassifierDeclaration;
4849
import org.eclipse.n4js.n4JS.N4EnumDeclaration;
@@ -89,6 +90,7 @@
8990
import org.eclipse.n4js.typesystem.utils.RuleEnvironmentExtensions;
9091
import org.eclipse.n4js.utils.N4JSLanguageUtils;
9192
import org.eclipse.n4js.utils.N4JSLanguageUtils.EnumKind;
93+
import org.eclipse.n4js.utils.URIUtils;
9294
import org.eclipse.n4js.utils.parser.conversion.ValueConverterUtils;
9395
import org.eclipse.xtext.EcoreUtil2;
9496

@@ -214,8 +216,19 @@ public Boolean caseImportDeclaration(ImportDeclaration original) {
214216
? original.getModuleSpecifierAsText().replace("%3A", ":") // see ModuleSpecifierValueConverter
215217
: original.getModule().getQualifiedName();
216218

219+
if (original.getModuleSpecifierForm() != ModuleSpecifierForm.PROJECT
220+
&& Strings.isNullOrEmpty(URIUtils.fileExtension(URIUtils.toFileUri(moduleSpecifier)))) {
221+
222+
String extension = original.isBare() ? N4JSGlobals.JS_FILE_EXTENSION : N4JSGlobals.DTS_FILE_EXTENSION;
223+
moduleSpecifier += "." + extension;
224+
}
225+
217226
processAnnotations(original.getAnnotations());
218227
write("import ");
228+
if (!original.isBare()) {
229+
write("type ");
230+
}
231+
219232
// 1) import specifiers
220233
List<ImportSpecifier> importSpecifiers = new ArrayList<>(original.getImportSpecifiers());
221234
if (!importSpecifiers.isEmpty() && importSpecifiers.get(0) instanceof DefaultImportSpecifier) {

plugins/org.eclipse.n4js.utils/src/org/eclipse/n4js/utils/URIUtils.java

+5
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,17 @@ static private int fileExtensionIndex(URI uri) {
9393
if (firstIdx < 0) {
9494
return -1;
9595
}
96+
boolean foundDot = false;
9697
for (int idx = firstIdx; idx > 0; idx--) {
9798
if (lastSegment.charAt(idx) == '.') {
99+
foundDot = true;
98100
firstIdx = idx + 1;
99101
break;
100102
}
101103
}
104+
if (!foundDot) {
105+
return -1;
106+
}
102107
String ext1 = lastSegment.substring(firstIdx);
103108
if (extensionPrefixes.containsKey(ext1)) {
104109
int scndIdx = firstIdx - 2;

testhelpers/org.eclipse.n4js.ide.tests.helper/src/org/eclipse/n4js/ide/tests/helper/server/xt/XtIdeTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,12 @@ public void generated_dts(XtMethodData data) throws IOException {
773773

774774
for (Project project : allProjectsWithGenerateDts) {
775775
File workingDir = getProjectRoot(project.getName());
776+
Path wdSrcDir = workingDir.toPath().resolve("src-gen");
777+
wdSrcDir.toFile().mkdirs(); // might not exist e.g. if is a definition project
776778

777779
// copy n4jsglobals.d.ts to output dir to make d.ts globals available
778780
Path n4jsGlobalsDTS = N4jsLibsAccess.getN4JSGlobalsDTS();
779-
Files.copy(n4jsGlobalsDTS, workingDir.toPath().resolve("src-gen/n4jsglobals.d.ts"));
781+
Files.copy(n4jsGlobalsDTS, wdSrcDir.resolve("n4jsglobals.d.ts"));
780782

781783
ProcessResult result;
782784
try {

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/cutOffReferences/ReferenceToNonDtsProject.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export public class Cls1 extends ClassWithDts {}
5151
export public class Cls2 extends ClassWithoutDts {}
5252

5353
/* XPECT generated_dts ---
54-
import {ClassWithDts} from 'ProjectOtherWithDts/src-gen/OtherWithDts'
54+
import type {ClassWithDts} from 'ProjectOtherWithDts/src-gen/OtherWithDts.d.ts'
5555

5656
export let v1: ClassWithDts;
5757
export let v2: any;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2021 NumberFour AG.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* NumberFour AG - Initial API and implementation
10+
*/
11+
12+
/* XPECT_SETUP org.eclipse.n4js.spec.tests.SpecXtTest
13+
14+
Workspace {
15+
Project "TestProject" {
16+
Folder "src" {
17+
ThisFile {}
18+
File "BareImportMe.js" { }
19+
}
20+
}
21+
}
22+
23+
File "BareImportMe.js" {
24+
export public class CP {}
25+
}
26+
27+
GENERATE_DTS
28+
29+
END_SETUP */
30+
31+
32+
33+
import "BareImportMe";
34+
35+
36+
37+
/* XPECT generated_dts ---
38+
import './BareImportMe.js'
39+
40+
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/DefaultAsImport.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export public class Dummy extends MyClass {} // ensures that the import is not r
3434

3535

3636
/* XPECT generated_dts ---
37-
import {default as MyClass} from './Other'
37+
import type {default as MyClass} from './Other.d.ts'
3838

3939
export class Dummy extends MyClass {}
4040
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ImportForLiteralType1.n4js.xt

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export public const redStringBased = ColorStringBased.RED;
3838

3939

4040
/* XPECT generated_dts ---
41-
import {Color} from './Other'
42-
import {ColorWithLiteralValues} from './Other'
43-
import {ColorNumberBased} from './Other'
44-
import {ColorStringBased} from './Other'
41+
import type {Color} from './Other.d.ts'
42+
import type {ColorWithLiteralValues} from './Other.d.ts'
43+
import type {ColorNumberBased} from './Other.d.ts'
44+
import type {ColorStringBased} from './Other.d.ts'
4545

4646
export const red: Color;
4747
export const redWithLiteralValues: ColorWithLiteralValues;

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ImportForLiteralType2.n4js.xt

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export public const redStringBased = getRedStringBased();
3838

3939

4040
/* XPECT generated_dts ---
41-
import {ColorStringBased} from './Other'
42-
import {ColorNumberBased} from './Other'
43-
import {ColorWithLiteralValues} from './Other'
44-
import {Color} from './Other'
41+
import type {ColorStringBased} from './Other.d.ts'
42+
import type {ColorNumberBased} from './Other.d.ts'
43+
import type {ColorWithLiteralValues} from './Other.d.ts'
44+
import type {Color} from './Other.d.ts'
4545

4646
export const red: Color;
4747
export const redWithLiteralValues: ColorWithLiteralValues;

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ImportMustBeAdded1.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const C = F();
3232

3333

3434
/* XPECT generated_dts ---
35-
import {Cls} from './Other'
35+
import type {Cls} from './Other.d.ts'
3636

3737
export const C: Cls;
3838
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ImportMustBeAdded2_withAlias.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Cls {
3737

3838

3939
/* XPECT generated_dts ---
40-
import {Cls as Cls2} from './Other'
40+
import type {Cls as Cls2} from './Other.d.ts'
4141

4242
export const C: Cls2;
4343
declare class Cls {}

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ImportMustBeAdded3_forIndirectlyExportedElem.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const C = F();
4343

4444

4545
/* XPECT generated_dts ---
46-
import {ClsExportedName} from './Other'
46+
import type {ClsExportedName} from './Other.d.ts'
4747

4848
export const C: ClsExportedName;
4949
--- */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021 NumberFour AG.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* NumberFour AG - Initial API and implementation
10+
*/
11+
12+
/* XPECT_SETUP org.eclipse.n4js.spec.tests.SpecXtTest
13+
14+
Workspace {
15+
Project "TestProject" {
16+
Folder "src" {
17+
ThisFile {}
18+
}
19+
DEPENDS_ON "ImportMe"
20+
}
21+
Project "ImportMe" {
22+
Folder "src" {
23+
File "index.n4js" { }
24+
}
25+
File "package.json" {}
26+
}
27+
}
28+
29+
File "index.n4js" {
30+
export public class CP {}
31+
}
32+
33+
File "package.json" {
34+
{
35+
"name": "ImportMe",
36+
"version": "1.0.0",
37+
"type": "module",
38+
"types": "src-gen/index.d.ts",
39+
"scripts": {},
40+
"n4js": {
41+
"vendorId": "VENDOR",
42+
"vendorName": "VENDOR_NAME",
43+
"projectType": "library",
44+
"output": "src-gen",
45+
"generator": { "d.ts": true },
46+
"sources": {
47+
"source": ["./src"]
48+
}
49+
},
50+
"dependencies": {
51+
"n4js-runtime": ""
52+
}
53+
}
54+
}
55+
56+
GENERATE_DTS
57+
58+
END_SETUP */
59+
60+
61+
import * as TS from "ImportMe";
62+
63+
export public class CPX extends TS.CP {}
64+
65+
66+
/* XPECT generated_dts ---
67+
import type * as TS from 'ImportMe'
68+
69+
export class CPX extends TS.CP {}
70+
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/ModuleSpecifiers.n4js.xt

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export let clsOtherLocal: ClsOtherLocal;
4343

4444

4545
/* XPECT generated_dts ---
46-
import {ClsOther as ClsOtherRemote} from 'ProjectOther/src-gen/RemoteOther'
47-
import {ClsOther as ClsOtherLocal} from './LocalOther'
46+
import type {ClsOther as ClsOtherRemote} from 'ProjectOther/src-gen/RemoteOther.d.ts'
47+
import type {ClsOther as ClsOtherLocal} from './LocalOther.d.ts'
4848

4949
export let clsOtherRemote: ClsOtherRemote;
5050
export let clsOtherLocal: ClsOtherLocal;

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/NamespaceAccess.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export public class Cls extends NS.SomeClass {}
3636

3737

3838
/* XPECT generated_dts ---
39-
import * as NS from './Other'
39+
import type * as NS from './Other.d.ts'
4040

4141
export let x: NS.SomeClass;
4242
export function foo(p: NS.SomeClass): NS.SomeClass;

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/imports/UseExistingNamespaceImport.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export public let x = NS.foo();
3030

3131

3232
/* XPECT generated_dts ---
33-
import * as NS from './Other'
33+
import type * as NS from './Other.d.ts'
3434

3535
export let x: NS.ClsOther;
3636
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/inferredTypes/InferredTypes2.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class ClassWithFields {
5858

5959

6060
/* XPECT generated_dts ---
61-
import {ClassDirectExported} from 'ProjectOtherDirect/src-gen/OtherDirect'
61+
import type {ClassDirectExported} from 'ProjectOtherDirect/src-gen/OtherDirect.d.ts'
6262

6363
export let v1: ClassDirectExported;
6464
export let v2: any;

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/inferredTypes/InferredTypesImportUsedInStatement.n4js.xt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class ClassWithFields {
3737

3838

3939
/* XPECT generated_dts ---
40-
import {ClassOther} from './Other'
40+
import type {ClassOther} from './Other.d.ts'
4141

4242
export let v: ClassOther;
4343
export class ClassWithFields {

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/inferredTypes/ReplaceWildcard.n4js.xt

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import {C} from "Other";
2929
const c: C<?> = null
3030

3131
/* XPECT generated_dts ---
32-
import {D} from './Other'
33-
import {C} from './Other'
32+
import type {D} from './Other.d.ts'
33+
import type {C} from './Other.d.ts'
3434

3535
declare const c: C<D>;
3636
--- */

tests/org.eclipse.n4js.spec.tests/xt-tests/dts-export/inferredTypes/UpperBounds.n4js.xt

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export class ClassWithFields {
4343

4444

4545
/* XPECT generated_dts ---
46-
import {ClassExported} from 'ProjectOther/src-gen/Other'
47-
import {GenClassOfClassExported} from 'ProjectOther/src-gen/Other'
48-
import {GenClassOfClassNotExported} from 'ProjectOther/src-gen/Other'
46+
import type {ClassExported} from 'ProjectOther/src-gen/Other.d.ts'
47+
import type {GenClassOfClassExported} from 'ProjectOther/src-gen/Other.d.ts'
48+
import type {GenClassOfClassNotExported} from 'ProjectOther/src-gen/Other.d.ts'
4949

5050
export let v1: GenClassOfClassExported<ClassExported>;
5151
export let v2: GenClassOfClassNotExported<any>;

0 commit comments

Comments
 (0)