2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-12-23 11:44:08 -05:00
|
|
|
import { test, assert, assertEqual } from "./test_util.ts";
|
2018-10-05 10:13:22 -04:00
|
|
|
import * as deno from "deno";
|
2018-08-22 17:17:26 -04:00
|
|
|
|
|
|
|
// We use a silly amount of `any` in these tests...
|
|
|
|
// tslint:disable:no-any
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
const { Compiler, jsonEsmTemplate } = (deno as any)._compiler;
|
2018-08-22 17:17:26 -04:00
|
|
|
|
|
|
|
interface ModuleInfo {
|
2018-10-28 19:41:10 -04:00
|
|
|
moduleName: string | undefined;
|
|
|
|
filename: string | undefined;
|
|
|
|
mediaType: MediaType | undefined;
|
|
|
|
sourceCode: string | undefined;
|
|
|
|
outputCode: string | undefined;
|
|
|
|
sourceMap: string | undefined;
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
// Since we can't/don't want to import all of TypeScript for this one enum, we
|
|
|
|
// we will replicate it from TypeScript. This does mean that if other script
|
|
|
|
// kinds are added in the future we would need to add them manually to the tests
|
|
|
|
enum ScriptKind {
|
|
|
|
Unknown = 0,
|
|
|
|
JS = 1,
|
|
|
|
JSX = 2,
|
|
|
|
TS = 3,
|
|
|
|
TSX = 4,
|
|
|
|
External = 5,
|
|
|
|
JSON = 6,
|
|
|
|
Deferred = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
const compilerInstance = Compiler.instance();
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2018-10-24 11:54:34 -04:00
|
|
|
// References to original items we are going to mock
|
2018-08-22 17:17:26 -04:00
|
|
|
const originals = {
|
|
|
|
_log: (compilerInstance as any)._log,
|
|
|
|
_os: (compilerInstance as any)._os,
|
|
|
|
_ts: (compilerInstance as any)._ts,
|
2019-01-13 23:44:11 -05:00
|
|
|
_service: (compilerInstance as any)._service
|
2018-08-22 17:17:26 -04:00
|
|
|
};
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
enum MediaType {
|
|
|
|
JavaScript = 0,
|
|
|
|
TypeScript = 1,
|
|
|
|
Json = 2,
|
|
|
|
Unknown = 3
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
function mockModuleInfo(
|
2018-10-28 19:41:10 -04:00
|
|
|
moduleName: string | undefined,
|
|
|
|
filename: string | undefined,
|
|
|
|
mediaType: MediaType | undefined,
|
|
|
|
sourceCode: string | undefined,
|
|
|
|
outputCode: string | undefined,
|
|
|
|
sourceMap: string | undefined
|
2018-08-22 17:17:26 -04:00
|
|
|
): ModuleInfo {
|
|
|
|
return {
|
|
|
|
moduleName,
|
|
|
|
filename,
|
2018-10-21 22:14:27 -04:00
|
|
|
mediaType,
|
2018-08-22 17:17:26 -04:00
|
|
|
sourceCode,
|
2018-10-28 19:41:10 -04:00
|
|
|
outputCode,
|
|
|
|
sourceMap
|
2018-08-22 17:17:26 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-03 22:09:07 -05:00
|
|
|
// Some fixtures we will use in testing
|
2018-10-05 10:13:22 -04:00
|
|
|
const fooBarTsSource = `import * as deno from "deno";
|
|
|
|
console.log(deno);
|
2018-08-22 17:17:26 -04:00
|
|
|
export const foo = "bar";
|
|
|
|
`;
|
|
|
|
|
|
|
|
const fooBazTsSource = `import { foo } from "./bar.ts";
|
|
|
|
console.log(foo);
|
|
|
|
`;
|
|
|
|
|
2018-09-02 10:33:08 -04:00
|
|
|
const modASource = `import { B } from "./modB.ts";
|
|
|
|
|
|
|
|
export class A {
|
|
|
|
b = new B();
|
|
|
|
};
|
|
|
|
`;
|
|
|
|
|
|
|
|
const modAModuleInfo = mockModuleInfo(
|
|
|
|
"modA",
|
|
|
|
"/root/project/modA.ts",
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-09-02 10:33:08 -04:00
|
|
|
modASource,
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-09-02 10:33:08 -04:00
|
|
|
undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
const modBSource = `import { A } from "./modA.ts";
|
|
|
|
|
|
|
|
export class B {
|
|
|
|
a = new A();
|
|
|
|
};
|
|
|
|
`;
|
|
|
|
|
|
|
|
const modBModuleInfo = mockModuleInfo(
|
|
|
|
"modB",
|
|
|
|
"/root/project/modB.ts",
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-09-02 10:33:08 -04:00
|
|
|
modBSource,
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-09-02 10:33:08 -04:00
|
|
|
undefined
|
|
|
|
);
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
// tslint:disable:max-line-length
|
2019-01-13 23:44:11 -05:00
|
|
|
const fooBarTsOutput = `import * as deno from "deno";
|
|
|
|
console.log(deno);
|
|
|
|
export const foo = "bar";
|
2018-10-28 19:41:10 -04:00
|
|
|
//# sourceMappingURL=bar.js.map
|
2018-08-22 17:17:26 -04:00
|
|
|
//# sourceURL=/root/project/foo/bar.ts`;
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
const fooBarTsSourcemap = `{"version":3,"file":"bar.js","sourceRoot":"","sources":["file:///root/project/foo/bar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAClB,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,CAAC"}`;
|
2018-10-30 23:50:40 -04:00
|
|
|
|
|
|
|
const loadConfigSource = `import * as config from "./config.json";
|
|
|
|
console.log(config.foo.baz);
|
|
|
|
`;
|
|
|
|
const configJsonSource = `{"foo":{"bar": true,"baz": ["qat", 1]}}`;
|
2018-08-22 17:17:26 -04:00
|
|
|
// tslint:enable:max-line-length
|
|
|
|
|
|
|
|
const moduleMap: {
|
|
|
|
[containFile: string]: { [moduleSpecifier: string]: ModuleInfo };
|
|
|
|
} = {
|
|
|
|
"/root/project": {
|
|
|
|
"foo/bar.ts": mockModuleInfo(
|
2018-09-02 18:50:46 -04:00
|
|
|
"/root/project/foo/bar.ts",
|
2018-08-22 17:17:26 -04:00
|
|
|
"/root/project/foo/bar.ts",
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-08-22 17:17:26 -04:00
|
|
|
fooBarTsSource,
|
2018-10-28 19:41:10 -04:00
|
|
|
null,
|
2018-08-22 17:17:26 -04:00
|
|
|
null
|
|
|
|
),
|
|
|
|
"foo/baz.ts": mockModuleInfo(
|
2018-09-02 18:50:46 -04:00
|
|
|
"/root/project/foo/baz.ts",
|
2018-08-22 17:17:26 -04:00
|
|
|
"/root/project/foo/baz.ts",
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-08-22 17:17:26 -04:00
|
|
|
fooBazTsSource,
|
2019-01-13 23:44:11 -05:00
|
|
|
null,
|
|
|
|
null
|
2018-09-02 10:33:08 -04:00
|
|
|
),
|
2018-10-21 22:14:27 -04:00
|
|
|
"modA.ts": modAModuleInfo,
|
|
|
|
"some.txt": mockModuleInfo(
|
|
|
|
"/root/project/some.txt",
|
|
|
|
"/root/project/some.text",
|
|
|
|
MediaType.Unknown,
|
|
|
|
"console.log();",
|
2018-10-28 19:41:10 -04:00
|
|
|
null,
|
2018-10-21 22:14:27 -04:00
|
|
|
null
|
2018-10-30 23:50:40 -04:00
|
|
|
),
|
|
|
|
"loadConfig.ts": mockModuleInfo(
|
|
|
|
"/root/project/loadConfig.ts",
|
|
|
|
"/root/project/loadConfig.ts",
|
|
|
|
MediaType.TypeScript,
|
|
|
|
loadConfigSource,
|
|
|
|
null,
|
|
|
|
null
|
2018-10-21 22:14:27 -04:00
|
|
|
)
|
2018-08-22 17:17:26 -04:00
|
|
|
},
|
|
|
|
"/root/project/foo/baz.ts": {
|
|
|
|
"./bar.ts": mockModuleInfo(
|
2018-09-02 18:50:46 -04:00
|
|
|
"/root/project/foo/bar.ts",
|
2018-08-22 17:17:26 -04:00
|
|
|
"/root/project/foo/bar.ts",
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-08-22 17:17:26 -04:00
|
|
|
fooBarTsSource,
|
2018-10-28 19:41:10 -04:00
|
|
|
fooBarTsOutput,
|
|
|
|
fooBarTsSourcemap
|
2018-08-22 17:17:26 -04:00
|
|
|
)
|
2018-09-02 10:33:08 -04:00
|
|
|
},
|
|
|
|
"/root/project/modA.ts": {
|
|
|
|
"./modB.ts": modBModuleInfo
|
|
|
|
},
|
|
|
|
"/root/project/modB.ts": {
|
|
|
|
"./modA.ts": modAModuleInfo
|
2018-10-21 22:14:27 -04:00
|
|
|
},
|
2018-10-30 23:50:40 -04:00
|
|
|
"/root/project/loadConfig.ts": {
|
|
|
|
"./config.json": mockModuleInfo(
|
|
|
|
"/root/project/config.json",
|
|
|
|
"/root/project/config.json",
|
|
|
|
MediaType.Json,
|
|
|
|
configJsonSource,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
},
|
2018-10-21 22:14:27 -04:00
|
|
|
"/moduleKinds": {
|
|
|
|
"foo.ts": mockModuleInfo(
|
|
|
|
"foo",
|
|
|
|
"/moduleKinds/foo.ts",
|
|
|
|
MediaType.TypeScript,
|
|
|
|
"console.log('foo');",
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-10-21 22:14:27 -04:00
|
|
|
undefined
|
|
|
|
),
|
|
|
|
"foo.d.ts": mockModuleInfo(
|
|
|
|
"foo",
|
|
|
|
"/moduleKinds/foo.d.ts",
|
|
|
|
MediaType.TypeScript,
|
|
|
|
"console.log('foo');",
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-10-21 22:14:27 -04:00
|
|
|
undefined
|
|
|
|
),
|
|
|
|
"foo.js": mockModuleInfo(
|
|
|
|
"foo",
|
|
|
|
"/moduleKinds/foo.js",
|
|
|
|
MediaType.JavaScript,
|
|
|
|
"console.log('foo');",
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-10-21 22:14:27 -04:00
|
|
|
undefined
|
|
|
|
),
|
|
|
|
"foo.json": mockModuleInfo(
|
|
|
|
"foo",
|
|
|
|
"/moduleKinds/foo.json",
|
|
|
|
MediaType.Json,
|
|
|
|
"console.log('foo');",
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-10-21 22:14:27 -04:00
|
|
|
undefined
|
|
|
|
),
|
|
|
|
"foo.txt": mockModuleInfo(
|
|
|
|
"foo",
|
|
|
|
"/moduleKinds/foo.txt",
|
|
|
|
MediaType.JavaScript,
|
|
|
|
"console.log('foo');",
|
2018-10-28 19:41:10 -04:00
|
|
|
undefined,
|
2018-10-21 22:14:27 -04:00
|
|
|
undefined
|
2019-01-03 22:09:07 -05:00
|
|
|
),
|
|
|
|
"empty_file.ts": mockModuleInfo(
|
|
|
|
"/moduleKinds/empty_file.ts",
|
|
|
|
"/moduleKinds/empty_file.ts",
|
|
|
|
MediaType.TypeScript,
|
|
|
|
"",
|
|
|
|
undefined,
|
|
|
|
undefined
|
2018-10-21 22:14:27 -04:00
|
|
|
)
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-02 10:33:08 -04:00
|
|
|
const moduleCache: {
|
|
|
|
[fileName: string]: ModuleInfo;
|
|
|
|
} = {
|
|
|
|
"/root/project/modA.ts": modAModuleInfo,
|
|
|
|
"/root/project/modB.ts": modBModuleInfo
|
|
|
|
};
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
const emittedFiles = {
|
|
|
|
"/root/project/foo/qat.ts": "console.log('foo');"
|
|
|
|
};
|
|
|
|
|
|
|
|
let getEmitOutputStack: string[] = [];
|
|
|
|
let logStack: any[][] = [];
|
|
|
|
let codeCacheStack: Array<{
|
|
|
|
fileName: string;
|
|
|
|
sourceCode: string;
|
|
|
|
outputCode: string;
|
2018-10-28 19:41:10 -04:00
|
|
|
sourceMap: string;
|
2018-08-22 17:17:26 -04:00
|
|
|
}> = [];
|
|
|
|
let codeFetchStack: Array<{
|
|
|
|
moduleSpecifier: string;
|
|
|
|
containingFile: string;
|
|
|
|
}> = [];
|
|
|
|
|
2018-08-28 00:22:57 -04:00
|
|
|
let mockDepsStack: string[][] = [];
|
2018-10-05 10:13:22 -04:00
|
|
|
let mockFactoryStack: any[] = [];
|
2018-08-22 17:17:26 -04:00
|
|
|
|
|
|
|
function logMock(...args: any[]): void {
|
|
|
|
logStack.push(args);
|
|
|
|
}
|
2018-09-04 15:23:38 -04:00
|
|
|
const osMock = {
|
2018-10-28 19:41:10 -04:00
|
|
|
codeCache(
|
|
|
|
fileName: string,
|
|
|
|
sourceCode: string,
|
|
|
|
outputCode: string,
|
|
|
|
sourceMap: string
|
|
|
|
): void {
|
|
|
|
codeCacheStack.push({ fileName, sourceCode, outputCode, sourceMap });
|
2018-09-02 10:33:08 -04:00
|
|
|
if (fileName in moduleCache) {
|
|
|
|
moduleCache[fileName].sourceCode = sourceCode;
|
|
|
|
moduleCache[fileName].outputCode = outputCode;
|
2018-10-28 19:41:10 -04:00
|
|
|
moduleCache[fileName].sourceMap = sourceMap;
|
2018-09-02 10:33:08 -04:00
|
|
|
} else {
|
|
|
|
moduleCache[fileName] = mockModuleInfo(
|
|
|
|
fileName,
|
|
|
|
fileName,
|
2018-10-21 22:14:27 -04:00
|
|
|
MediaType.TypeScript,
|
2018-09-02 10:33:08 -04:00
|
|
|
sourceCode,
|
2018-10-28 19:41:10 -04:00
|
|
|
outputCode,
|
|
|
|
sourceMap
|
2018-09-02 10:33:08 -04:00
|
|
|
);
|
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
},
|
|
|
|
codeFetch(moduleSpecifier: string, containingFile: string): ModuleInfo {
|
|
|
|
codeFetchStack.push({ moduleSpecifier, containingFile });
|
|
|
|
if (containingFile in moduleMap) {
|
|
|
|
if (moduleSpecifier in moduleMap[containingFile]) {
|
|
|
|
return moduleMap[containingFile][moduleSpecifier];
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 19:41:10 -04:00
|
|
|
return mockModuleInfo(null, null, null, null, null, null);
|
2018-08-22 17:17:26 -04:00
|
|
|
},
|
|
|
|
exit(code: number): never {
|
2018-10-30 23:50:40 -04:00
|
|
|
throw new Error(`Unexpected call to os.exit(${code})`);
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
};
|
2018-09-04 15:23:38 -04:00
|
|
|
const tsMock = {
|
2019-01-13 23:44:11 -05:00
|
|
|
createLanguageService() {
|
|
|
|
return {} as any;
|
2018-08-22 17:17:26 -04:00
|
|
|
},
|
|
|
|
formatDiagnosticsWithColorAndContext(
|
2019-01-13 23:44:11 -05:00
|
|
|
diagnostics: ReadonlyArray<any>,
|
|
|
|
_host: any
|
2018-08-22 17:17:26 -04:00
|
|
|
): string {
|
2018-10-30 23:50:40 -04:00
|
|
|
return JSON.stringify(diagnostics.map(({ messageText }) => messageText));
|
2018-08-22 17:17:26 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const serviceMock = {
|
2019-01-13 23:44:11 -05:00
|
|
|
getCompilerOptionsDiagnostics() {
|
2018-08-22 17:17:26 -04:00
|
|
|
return originals._service.getCompilerOptionsDiagnostics.call(
|
|
|
|
originals._service
|
|
|
|
);
|
|
|
|
},
|
2019-01-13 23:44:11 -05:00
|
|
|
getEmitOutput(fileName: string) {
|
2018-08-22 17:17:26 -04:00
|
|
|
getEmitOutputStack.push(fileName);
|
2019-01-13 23:44:11 -05:00
|
|
|
return originals._service.getEmitOutput.call(originals._service, fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
},
|
2019-01-13 23:44:11 -05:00
|
|
|
getSemanticDiagnostics(fileName: string) {
|
2018-08-22 17:17:26 -04:00
|
|
|
return originals._service.getSemanticDiagnostics.call(
|
|
|
|
originals._service,
|
|
|
|
fileName
|
|
|
|
);
|
|
|
|
},
|
2019-01-13 23:44:11 -05:00
|
|
|
getSyntacticDiagnostics(fileName: string) {
|
2018-08-22 17:17:26 -04:00
|
|
|
return originals._service.getSyntacticDiagnostics.call(
|
|
|
|
originals._service,
|
|
|
|
fileName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const mocks = {
|
|
|
|
_log: logMock,
|
|
|
|
_os: osMock,
|
|
|
|
_ts: tsMock,
|
2019-01-13 23:44:11 -05:00
|
|
|
_service: serviceMock
|
2018-08-22 17:17:26 -04:00
|
|
|
};
|
|
|
|
|
2018-08-28 00:22:57 -04:00
|
|
|
/**
|
|
|
|
* Setup the mocks for a test
|
|
|
|
*/
|
|
|
|
function setup() {
|
|
|
|
// monkey patch mocks on instance
|
2018-08-22 17:17:26 -04:00
|
|
|
Object.assign(compilerInstance, mocks);
|
2018-08-28 00:22:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Teardown the mocks for a test
|
|
|
|
*/
|
|
|
|
function teardown() {
|
|
|
|
// reset compiler internal state
|
|
|
|
(compilerInstance as any)._moduleMetaDataMap.clear();
|
|
|
|
(compilerInstance as any)._fileNamesMap.clear();
|
|
|
|
|
|
|
|
// reset mock states
|
|
|
|
codeFetchStack = [];
|
|
|
|
codeCacheStack = [];
|
|
|
|
logStack = [];
|
|
|
|
getEmitOutputStack = [];
|
|
|
|
|
|
|
|
assertEqual(mockDepsStack.length, 0);
|
|
|
|
assertEqual(mockFactoryStack.length, 0);
|
|
|
|
mockDepsStack = [];
|
|
|
|
mockFactoryStack = [];
|
|
|
|
|
|
|
|
// restore original properties and methods
|
|
|
|
Object.assign(compilerInstance, originals);
|
|
|
|
}
|
2018-08-22 17:17:26 -04:00
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
test(function testJsonEsmTemplate() {
|
|
|
|
const result = jsonEsmTemplate(
|
|
|
|
`{ "hello": "world", "foo": "bar" }`,
|
|
|
|
"/foo.ts"
|
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
result,
|
|
|
|
`const _json = JSON.parse(\`{ "hello": "world", "foo": "bar" }\`)\n` +
|
|
|
|
`export default _json;\n` +
|
|
|
|
`//# sourceURL=/foo.ts`
|
|
|
|
);
|
2018-10-30 23:50:40 -04:00
|
|
|
});
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
test(function compilerInstance() {
|
2019-01-13 23:44:11 -05:00
|
|
|
assert(Compiler != null);
|
|
|
|
assert(Compiler.instance() != null);
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Testing the internal APIs
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
test(function compilerCompile() {
|
2019-01-06 14:17:13 -05:00
|
|
|
// equal to `deno foo/bar.ts`
|
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
const moduleMetaData = compilerInstance.compile(
|
|
|
|
"foo/bar.ts",
|
|
|
|
"/root/project"
|
|
|
|
);
|
2019-01-06 14:17:13 -05:00
|
|
|
assertEqual(moduleMetaData.sourceCode, fooBarTsSource);
|
|
|
|
assertEqual(moduleMetaData.outputCode, fooBarTsOutput);
|
|
|
|
|
|
|
|
assertEqual(
|
|
|
|
codeFetchStack.length,
|
|
|
|
1,
|
|
|
|
"Module should have only been fetched once."
|
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
codeCacheStack.length,
|
|
|
|
1,
|
|
|
|
"Compiled code should have only been cached once."
|
|
|
|
);
|
|
|
|
const [codeCacheCall] = codeCacheStack;
|
|
|
|
assertEqual(codeCacheCall.fileName, "/root/project/foo/bar.ts");
|
|
|
|
assertEqual(codeCacheCall.sourceCode, fooBarTsSource);
|
|
|
|
assertEqual(codeCacheCall.outputCode, fooBarTsOutput);
|
|
|
|
assertEqual(codeCacheCall.sourceMap, fooBarTsSourcemap);
|
|
|
|
teardown();
|
|
|
|
});
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
test(function compilerCompilerMultiModule() {
|
2019-01-06 14:17:13 -05:00
|
|
|
// equal to `deno foo/baz.ts`
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
compilerInstance.compile("foo/baz.ts", "/root/project");
|
|
|
|
assertEqual(codeFetchStack.length, 2, "Two modules fetched.");
|
|
|
|
assertEqual(codeCacheStack.length, 1, "Only one module compiled.");
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-23 16:29:31 -04:00
|
|
|
});
|
|
|
|
|
2019-01-06 14:17:13 -05:00
|
|
|
test(function compilerLoadJsonModule() {
|
2018-09-02 10:33:08 -04:00
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
compilerInstance.compile("loadConfig.ts", "/root/project");
|
|
|
|
assertEqual(codeFetchStack.length, 2, "Two modules fetched.");
|
|
|
|
assertEqual(codeCacheStack.length, 1, "Only one module compiled.");
|
2019-01-06 14:17:13 -05:00
|
|
|
teardown();
|
2018-09-02 10:33:08 -04:00
|
|
|
});
|
|
|
|
|
2019-01-06 14:17:13 -05:00
|
|
|
test(function compilerResolveModule() {
|
2018-10-30 23:50:40 -04:00
|
|
|
setup();
|
2019-01-06 14:17:13 -05:00
|
|
|
const moduleMetaData = compilerInstance.resolveModule(
|
|
|
|
"foo/baz.ts",
|
|
|
|
"/root/project"
|
|
|
|
);
|
2019-01-13 23:44:11 -05:00
|
|
|
console.log(moduleMetaData);
|
2019-01-06 14:17:13 -05:00
|
|
|
assertEqual(moduleMetaData.sourceCode, fooBazTsSource);
|
2019-01-13 23:44:11 -05:00
|
|
|
assertEqual(moduleMetaData.outputCode, null);
|
|
|
|
assertEqual(moduleMetaData.sourceMap, null);
|
2019-01-06 14:17:13 -05:00
|
|
|
assertEqual(moduleMetaData.scriptVersion, "1");
|
|
|
|
|
|
|
|
assertEqual(codeFetchStack.length, 1, "Only initial module is resolved.");
|
|
|
|
teardown();
|
2018-10-30 23:50:40 -04:00
|
|
|
});
|
|
|
|
|
2019-01-06 14:17:13 -05:00
|
|
|
test(function compilerResolveModuleUnknownMediaType() {
|
2018-10-21 22:14:27 -04:00
|
|
|
setup();
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-01-06 14:17:13 -05:00
|
|
|
compilerInstance.resolveModule("some.txt", "/root/project");
|
2018-10-21 22:14:27 -04:00
|
|
|
} catch (e) {
|
|
|
|
assert(e instanceof Error);
|
|
|
|
assertEqual(
|
|
|
|
e.message,
|
|
|
|
`Unknown media type for: "some.txt" from "/root/project".`
|
|
|
|
);
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(didThrow);
|
|
|
|
teardown();
|
|
|
|
});
|
|
|
|
|
2019-01-13 23:44:11 -05:00
|
|
|
test(function compilerRecompileFlag() {
|
2019-01-06 14:17:13 -05:00
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
|
|
|
assertEqual(
|
|
|
|
getEmitOutputStack.length,
|
|
|
|
1,
|
|
|
|
"Expected only a single emitted file."
|
|
|
|
);
|
|
|
|
// running compiler against same file should use cached code
|
|
|
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
|
|
|
assertEqual(
|
|
|
|
getEmitOutputStack.length,
|
|
|
|
1,
|
|
|
|
"Expected only a single emitted file."
|
|
|
|
);
|
|
|
|
compilerInstance.recompile = true;
|
|
|
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
|
|
|
assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file.");
|
|
|
|
assert(
|
|
|
|
getEmitOutputStack[0] === getEmitOutputStack[1],
|
|
|
|
"Expected same file to be emitted twice."
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
|
|
|
teardown();
|
|
|
|
});
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
// TypeScript LanguageServiceHost APIs
|
|
|
|
|
|
|
|
test(function compilerGetCompilationSettings() {
|
2018-10-28 19:41:10 -04:00
|
|
|
const expectedKeys = [
|
2018-08-22 17:17:26 -04:00
|
|
|
"allowJs",
|
2018-10-28 19:41:10 -04:00
|
|
|
"checkJs",
|
2018-08-22 17:17:26 -04:00
|
|
|
"module",
|
|
|
|
"outDir",
|
2018-10-30 23:50:40 -04:00
|
|
|
"resolveJsonModule",
|
2018-10-28 19:41:10 -04:00
|
|
|
"sourceMap",
|
2018-08-22 17:17:26 -04:00
|
|
|
"stripComments",
|
|
|
|
"target"
|
2018-10-28 19:41:10 -04:00
|
|
|
];
|
|
|
|
const result = compilerInstance.getCompilationSettings();
|
|
|
|
for (const key of expectedKeys) {
|
2018-08-22 17:17:26 -04:00
|
|
|
assert(key in result, `Expected "${key}" in compiler options.`);
|
|
|
|
}
|
2018-10-28 19:41:10 -04:00
|
|
|
assertEqual(Object.keys(result).length, expectedKeys.length);
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetNewLine() {
|
|
|
|
const result = compilerInstance.getNewLine();
|
|
|
|
assertEqual(result, "\n", "Expected newline value of '\\n'.");
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetScriptFileNames() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
compilerInstance.compile("foo/bar.ts", "/root/project");
|
2018-08-22 17:17:26 -04:00
|
|
|
const result = compilerInstance.getScriptFileNames();
|
|
|
|
assertEqual(result.length, 1, "Expected only a single filename.");
|
|
|
|
assertEqual(result[0], "/root/project/foo/bar.ts");
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetScriptKind() {
|
2018-10-21 22:14:27 -04:00
|
|
|
setup();
|
2019-01-06 14:17:13 -05:00
|
|
|
compilerInstance.resolveModule("foo.ts", "/moduleKinds");
|
|
|
|
compilerInstance.resolveModule("foo.d.ts", "/moduleKinds");
|
|
|
|
compilerInstance.resolveModule("foo.js", "/moduleKinds");
|
|
|
|
compilerInstance.resolveModule("foo.json", "/moduleKinds");
|
|
|
|
compilerInstance.resolveModule("foo.txt", "/moduleKinds");
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptKind("/moduleKinds/foo.ts"),
|
2019-01-13 23:44:11 -05:00
|
|
|
ScriptKind.TS
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptKind("/moduleKinds/foo.d.ts"),
|
2019-01-13 23:44:11 -05:00
|
|
|
ScriptKind.TS
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptKind("/moduleKinds/foo.js"),
|
2019-01-13 23:44:11 -05:00
|
|
|
ScriptKind.JS
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptKind("/moduleKinds/foo.json"),
|
2019-01-13 23:44:11 -05:00
|
|
|
ScriptKind.JSON
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptKind("/moduleKinds/foo.txt"),
|
2019-01-13 23:44:11 -05:00
|
|
|
ScriptKind.JS
|
2019-01-06 14:17:13 -05:00
|
|
|
);
|
2018-10-21 22:14:27 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetScriptVersion() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2019-01-13 23:44:11 -05:00
|
|
|
const moduleMetaData = compilerInstance.compile(
|
2019-01-06 14:17:13 -05:00
|
|
|
"foo/bar.ts",
|
|
|
|
"/root/project"
|
|
|
|
);
|
2018-08-22 17:17:26 -04:00
|
|
|
assertEqual(
|
2019-01-06 14:17:13 -05:00
|
|
|
compilerInstance.getScriptVersion(moduleMetaData.fileName),
|
2018-08-22 17:17:26 -04:00
|
|
|
"1",
|
|
|
|
"Expected known module to have script version of 1"
|
|
|
|
);
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetScriptVersionUnknown() {
|
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getScriptVersion("/root/project/unknown_module.ts"),
|
|
|
|
"",
|
|
|
|
"Expected unknown module to have an empty script version"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetScriptSnapshot() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2019-01-06 14:17:13 -05:00
|
|
|
const moduleMetaData = compilerInstance.resolveModule(
|
|
|
|
"foo/bar.ts",
|
|
|
|
"/root/project"
|
|
|
|
);
|
|
|
|
const result = compilerInstance.getScriptSnapshot(moduleMetaData.fileName);
|
2018-08-22 17:17:26 -04:00
|
|
|
assert(result != null, "Expected snapshot to be defined.");
|
|
|
|
assertEqual(result.getLength(), fooBarTsSource.length);
|
|
|
|
assertEqual(
|
|
|
|
result.getText(0, 6),
|
|
|
|
"import",
|
|
|
|
"Expected .getText() to equal 'import'"
|
|
|
|
);
|
|
|
|
assertEqual(result.getChangeRange(result), undefined);
|
2018-08-24 00:07:01 -04:00
|
|
|
// This is and optional part of the `IScriptSnapshot` API which we don't
|
|
|
|
// define, os checking for the lack of this property.
|
2018-08-22 17:17:26 -04:00
|
|
|
assert(!("dispose" in result));
|
2019-01-06 14:17:13 -05:00
|
|
|
|
|
|
|
assert(
|
|
|
|
result === moduleMetaData,
|
|
|
|
"result should strictly equal moduleMetaData"
|
|
|
|
);
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetCurrentDirectory() {
|
|
|
|
assertEqual(compilerInstance.getCurrentDirectory(), "");
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerGetDefaultLibFileName() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2018-10-11 17:23:13 -04:00
|
|
|
assertEqual(
|
|
|
|
compilerInstance.getDefaultLibFileName(),
|
|
|
|
"$asset$/lib.deno_runtime.d.ts"
|
|
|
|
);
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerUseCaseSensitiveFileNames() {
|
|
|
|
assertEqual(compilerInstance.useCaseSensitiveFileNames(), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerReadFile() {
|
|
|
|
let doesThrow = false;
|
|
|
|
try {
|
|
|
|
compilerInstance.readFile("foobar.ts");
|
|
|
|
} catch (e) {
|
|
|
|
doesThrow = true;
|
|
|
|
assert(e.message.includes("Not implemented") === true);
|
|
|
|
}
|
|
|
|
assert(doesThrow);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerFileExists() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2019-01-06 14:17:13 -05:00
|
|
|
const moduleMetaData = compilerInstance.resolveModule(
|
|
|
|
"foo/bar.ts",
|
|
|
|
"/root/project"
|
|
|
|
);
|
|
|
|
assert(compilerInstance.fileExists(moduleMetaData.fileName));
|
2018-10-11 17:23:13 -04:00
|
|
|
assert(compilerInstance.fileExists("$asset$/lib.deno_runtime.d.ts"));
|
2018-08-22 17:17:26 -04:00
|
|
|
assertEqual(
|
|
|
|
compilerInstance.fileExists("/root/project/unknown-module.ts"),
|
|
|
|
false
|
|
|
|
);
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function compilerResolveModuleNames() {
|
2018-08-28 00:22:57 -04:00
|
|
|
setup();
|
2018-08-22 17:17:26 -04:00
|
|
|
const results = compilerInstance.resolveModuleNames(
|
2018-09-01 13:47:13 -04:00
|
|
|
["foo/bar.ts", "foo/baz.ts", "deno"],
|
2018-08-22 17:17:26 -04:00
|
|
|
"/root/project"
|
|
|
|
);
|
2018-09-01 13:47:13 -04:00
|
|
|
assertEqual(results.length, 3);
|
2018-08-22 17:17:26 -04:00
|
|
|
const fixtures: Array<[string, boolean]> = [
|
|
|
|
["/root/project/foo/bar.ts", false],
|
|
|
|
["/root/project/foo/baz.ts", false],
|
2018-10-11 17:23:13 -04:00
|
|
|
["$asset$/lib.deno_runtime.d.ts", true]
|
2018-08-22 17:17:26 -04:00
|
|
|
];
|
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
const result = results[i];
|
|
|
|
const [resolvedFileName, isExternalLibraryImport] = fixtures[i];
|
|
|
|
assertEqual(result.resolvedFileName, resolvedFileName);
|
|
|
|
assertEqual(result.isExternalLibraryImport, isExternalLibraryImport);
|
|
|
|
}
|
2018-08-28 00:22:57 -04:00
|
|
|
teardown();
|
2018-08-22 17:17:26 -04:00
|
|
|
});
|
2019-01-03 22:09:07 -05:00
|
|
|
|
|
|
|
test(function compilerResolveEmptyFile() {
|
|
|
|
setup();
|
|
|
|
const result = compilerInstance.resolveModuleNames(
|
|
|
|
["empty_file.ts"],
|
|
|
|
"/moduleKinds"
|
|
|
|
);
|
|
|
|
assertEqual(result[0].resolvedFileName, "/moduleKinds/empty_file.ts");
|
|
|
|
teardown();
|
|
|
|
});
|