Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 8043bb8

Browse files
committed
reorganize directory structure
Signed-off-by: 迷渡 <justjavac@gmail.com>
1 parent 73fd3e2 commit 8043bb8

File tree

3 files changed

+119
-65
lines changed

3 files changed

+119
-65
lines changed

src/index.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import mockRequire from "mock-require";
77
import ts_module from "typescript/lib/tsserverlibrary";
88

99
import { Logger } from "./logger";
10-
import { getDenoDir } from "./shared";
10+
import { getDenoDir, getGlobalDtsPath, getLocalDtsPath, getDtsPathForVscode } from "./utils";
1111

1212
let logger: Logger;
1313

@@ -19,7 +19,6 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
1919
const OPTIONS: ts_module.CompilerOptions = {
2020
allowJs: true,
2121
checkJs: true,
22-
allowNonTsExtensions: true,
2322
esModuleInterop: true,
2423
module: typescript.ModuleKind.ESNext,
2524
moduleResolution: typescript.ModuleResolutionKind.NodeJs,
@@ -36,6 +35,7 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
3635
};
3736

3837
const OPTIONS_OVERWRITE_BY_DENO: ts_module.CompilerOptions = {
38+
allowNonTsExtensions: false,
3939
jsx: OPTIONS.jsx,
4040
module: OPTIONS.module,
4141
moduleResolution: OPTIONS.moduleResolution,
@@ -44,6 +44,10 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
4444
noEmit: OPTIONS.noEmit,
4545
noEmitHelpers: OPTIONS.noEmitHelpers,
4646
target: typescript.ScriptTarget.ESNext,
47+
paths: {
48+
"abc": ['./c.ts'],
49+
"abc.ts": ['./c.ts'],
50+
},
4751
};
4852

4953
return {
@@ -66,7 +70,9 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
6670
redirectedReference?: ts_module.ResolvedProjectReference,
6771
options: ts_module.CompilerOptions = OPTIONS,
6872
) => {
69-
moduleNames = moduleNames.map(convertRemoteToLocalCache).map(stripExtNameDotTs);
73+
moduleNames = moduleNames
74+
.map(convertRemoteToLocalCache)
75+
.map(stripExtNameDotTs);
7076

7177
return resolveModuleNames.call(
7278
info.languageServiceHost,
@@ -83,6 +89,7 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
8389
info.languageServiceHost.getCompilationSettings = () => {
8490
const projectConfig = getCompilationSettings.call(info.languageServiceHost);
8591
const compilationSettings = merge(merge(OPTIONS, projectConfig), OPTIONS_OVERWRITE_BY_DENO);
92+
compilationSettings.baseUrl = info.project.getCurrentDirectory();
8693
logger.info(`compilationSettings:${JSON.stringify(compilationSettings)}`);
8794
return compilationSettings;
8895
};
@@ -142,6 +149,46 @@ module.exports = function init({ typescript }: { typescript: typeof ts_module })
142149
return details;
143150
};
144151

152+
// const getSemanticDiagnostics = info.languageService.getSemanticDiagnostics;
153+
154+
// info.languageService.getSemanticDiagnostics = (filename: string) => {
155+
// const diagnostics = getSemanticDiagnostics(filename);
156+
157+
// // ref: https://github.com/denoland/deno/blob/da8cb408c878aa6e90542e26173f1f14b5254d29/cli/js/compiler/util.ts#L262
158+
// const ignoredDiagnostics = [
159+
// // TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
160+
// // not a module.
161+
// 2306,
162+
// // TS1375: 'await' expressions are only allowed at the top level of a file
163+
// // when that file is a module, but this file has no imports or exports.
164+
// // Consider adding an empty 'export {}' to make this file a module.
165+
// 1375,
166+
// // TS1103: 'for-await-of' statement is only allowed within an async function
167+
// // or async generator.
168+
// 1103,
169+
// // TS2691: An import path cannot end with a '.ts' extension. Consider
170+
// // importing 'bad-module' instead.
171+
// 2691,
172+
// // TS5009: Cannot find the common subdirectory path for the input files.
173+
// 5009,
174+
// // TS5055: Cannot write file
175+
// // 'http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js'
176+
// // because it would overwrite input file.
177+
// 5055,
178+
// // TypeScript is overly opinionated that only CommonJS modules kinds can
179+
// // support JSON imports. Allegedly this was fixed in
180+
// // Microsoft/TypeScript#26825 but that doesn't seem to be working here,
181+
// // so we will ignore complaints about this compiler setting.
182+
// 5070,
183+
// // TS7016: Could not find a declaration file for module '...'. '...'
184+
// // implicitly has an 'any' type. This is due to `allowJs` being off by
185+
// // default but importing of a JavaScript module.
186+
// 7016,
187+
// ];
188+
189+
// return diagnostics.filter((v: ts_module.Diagnostic) => !ignoredDiagnostics.includes(v.code));
190+
// };
191+
145192
return info.languageService;
146193
},
147194

@@ -213,40 +260,3 @@ function fallbackHeader(modulePath: string): string {
213260
}
214261
return modulePath;
215262
}
216-
217-
function getDtsPathForVscode(info: ts_module.server.PluginCreateInfo): string | undefined {
218-
const bundledDtsPath = info.config.dtsPath;
219-
220-
if (bundledDtsPath && fs.existsSync(bundledDtsPath)) {
221-
return bundledDtsPath;
222-
}
223-
224-
return undefined;
225-
}
226-
227-
function getGlobalDtsPath(): string | undefined {
228-
const denoDir = getDenoDir();
229-
const globalDtsPath = path.resolve(denoDir, "lib.deno_runtime.d.ts");
230-
231-
if (fs.existsSync(globalDtsPath)) {
232-
return globalDtsPath;
233-
}
234-
235-
return undefined;
236-
}
237-
238-
function getLocalDtsPath(info: ts.server.PluginCreateInfo): string | undefined {
239-
const localDtsPath = path.resolve(
240-
info.project.getCurrentDirectory(),
241-
"node_modules",
242-
"typescript-deno-plugin",
243-
"lib",
244-
"lib.deno_runtime.d.ts",
245-
);
246-
247-
if (fs.existsSync(localDtsPath)) {
248-
return localDtsPath;
249-
}
250-
251-
return undefined;
252-
}

src/shared.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/utils.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
4+
import ts_module from "typescript/lib/tsserverlibrary";
5+
6+
export function getDenoDir(): string {
7+
// ref https://deno.land/manual.html
8+
// On Linux/Redox: $XDG_CACHE_HOME/deno or $HOME/.cache/deno
9+
// On Windows: %LOCALAPPDATA%/deno (%LOCALAPPDATA% = FOLDERID_LocalAppData)
10+
// On macOS: $HOME/Library/Caches/deno
11+
// If something fails, it falls back to $HOME/.deno
12+
let denoDir = process.env.DENO_DIR;
13+
if (denoDir === undefined) {
14+
switch (process.platform) {
15+
case "win32":
16+
denoDir = `${process.env.LOCALAPPDATA}\\deno`;
17+
break;
18+
case "darwin":
19+
denoDir = `${process.env.HOME}/Library/Caches/deno`;
20+
break;
21+
case "linux":
22+
denoDir = `${process.env.HOME}/.cache/deno`;
23+
break;
24+
default:
25+
denoDir = `${process.env.HOME}/.deno`;
26+
}
27+
}
28+
29+
return denoDir;
30+
}
31+
32+
33+
export function getGlobalDtsPath(): string | undefined {
34+
const denoDir = getDenoDir();
35+
const globalDtsPath = path.resolve(denoDir, "lib.deno_runtime.d.ts");
36+
37+
if (fs.existsSync(globalDtsPath)) {
38+
return globalDtsPath;
39+
}
40+
41+
return undefined;
42+
}
43+
44+
export function getLocalDtsPath(info: ts.server.PluginCreateInfo): string | undefined {
45+
const localDtsPath = path.resolve(
46+
info.project.getCurrentDirectory(),
47+
"node_modules",
48+
"typescript-deno-plugin",
49+
"lib",
50+
"lib.deno_runtime.d.ts",
51+
);
52+
53+
if (fs.existsSync(localDtsPath)) {
54+
return localDtsPath;
55+
}
56+
57+
return undefined;
58+
}
59+
60+
export
61+
function getDtsPathForVscode(info: ts_module.server.PluginCreateInfo): string | undefined {
62+
const bundledDtsPath = info.config.dtsPath;
63+
64+
if (bundledDtsPath && fs.existsSync(bundledDtsPath)) {
65+
return bundledDtsPath;
66+
}
67+
68+
return undefined;
69+
}

0 commit comments

Comments
 (0)