8000 feat: report diagnostic errors on compilerOptions - #1457 (#1470) · fuse-box/fuse-box@7c9b7bd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 7c9b7bd

Browse files
eddywnchanged
authored andcommitted
feat: report diagnostic errors on compilerOptions - #1457 (#1470)
* feat: add echo blue for TS diagnostic messages * feat: add helper function to ensure typescript is installed * feat: check for typescript installed in index.js * fix: use ScriptTarget enum from typescript * feat: parse and validate compilerOptions * fix: fix hash on quantum css splitting test * fix: remove unset compilerOptions * fix: quantum css splitting test hash * fix: revert "fix: quantum css splitting test hash" This reverts commit 40bcb2e. * fix: fix language level and tsconfig checking messages fix: set languageLevel in BundleWriter to ES5 (uppercase enum) fix: fix language level enum key (uppercased) test: fix script target value as uppercase feat: add interface for tsConfig when array of compiler options fix: fix types to custom tsconfig to allow array of compiler options fix: make TS error messages prettier fix: remove console.log from TypescriptConfig
1 parent 6c6c562 commit 7c9b7bd

File tree

11 files changed

+229
-50
lines changed

11 files changed

+229
-50
lines changed

src/Log.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ export class Log {
469469
log.red(msg).echo();
470470
return this;
471471
}
472+
public echoBlue(msg) {
473+
log.blue(msg).echo();
474+
return this;
475+
}
472476
public echoBreak() {
473477
log.green(`\n -------------- \n`).echo();
474478
return this;

src/Utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Log } from "./Log";
99
const userFuseDir = Config.PROJECT_ROOT;
1010
const stylesheetExtensions = new Set<string>([".css", ".sass", ".scss", ".styl", ".less", ".pcss"]);
1111
const MBLACKLIST = ["freelist", "sys"];
12+
export const MINIMAL_TYPESCRIPT_VERSION = "3.0";
1213
export type Concat = {
1314
add(fileName: string | null, content: string | Buffer, sourceMap?: string): void;
1415
content: Buffer;
@@ -61,6 +62,40 @@ export function printCurrentVersion() {
6162
}
6263
}
6364

65+
export function ensureTypescriptInstalled(): never | void {
66+
const NO_TYPESCRIPT_MESSAGE = `
67+
FuseBox failed to initialize. Please check that:
68+
- you have TypeScript installed
69+
-> if using NPM, run on your terminal:\n
70+
npm i -D typescript\n
71+
-> if using Yarn, run on your terminal:\n
72+
yarn add typescript --dev\n
73+
- the TypeScript version installed is >= ${MINIMAL_TYPESCRIPT_VERSION}
74+
`;
75+
const TYPESCRIPT_NOT_SUPPORTED = `
76+
FuseBox failed to initialize. Please check that:
77+
- the TypeScript version installed is >= ${MINIMAL_TYPESCRIPT_VERSION}
78+
`;
79+
try {
80+
const ts = require("typescript");
81+
if (typeof ts.versionMajorMinor === "string") {
82+
if (Number(MINIMAL_TYPESCRIPT_VERSION) > Number(ts.versionMajorMinor)) {
83+
console.log(TYPESCRIPT_NOT_SUPPORTED);
84+
process.exit(1);
85+
}
86+
return
87+
}
88+
console.log(NO_TYPESCRIPT_MESSAGE);
89+
process.exit(1);
90+
} catch (error) {
91+
if (error.code === "MODULE_NOT_FOUND") {
92+
console.log(NO_TYPESCRIPT_MESSAGE);
93+
process.exit(1);
94+
}
95+
throw error;
96+
}
97+
}
98+
6499
export function getDateTime() {
65100
const data = new Date();
66101
let hours: string | number = data.getHours();

src/analysis/plugins/LanguageLevel.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { File, ScriptTarget } from "../../core/File";
33
export class LanguageLevel {
44
public static onNode(file: File, node: any, parent: any) {
55
if (node.async === true) {
6-
file.setLanguageLevel(ScriptTarget.es2017);
6+
file.setLanguageLevel(ScriptTarget.ES2017);
77
} else if (node.kind === "const") {
8-
file.setLanguageLevel(ScriptTarget.es2015);
8+
file.setLanguageLevel(ScriptTarget.ES2017);
99
} else if (node.kind === "let") {
10-
file.setLanguageLevel(ScriptTarget.es2015);
10+
file.setLanguageLevel(ScriptTarget.ES2015);
1111
} else if (node.type === "ArrowFunctionExpression") {
12-
file.setLanguageLevel(ScriptTarget.es2015);
12+
file.setLanguageLevel(ScriptTarget.ES2015);
1313
} else if (node.type === "TemplateLiteral") {
14-
file.setLanguageLevel(ScriptTarget.es2015);
14+
file.setLanguageLevel(ScriptTarget.ES2015);
1515
} else if (node.type === "ClassDeclaration") {
16-
file.setLanguageLevel(ScriptTarget.es2015);
16+
file.setLanguageLevel(ScriptTarget.ES2015);
1717
}
1818
}
1919
public static onEnd(file: File) {

src/core/CombinedTargetAndLanguageLevel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class CombinedTargetAndLanguageLevel {
1616
return level ? ScriptTarget[level] : undefined;
1717
}
1818

19-
public getLanguageLevelOrDefault(defaultLanguageLevel: ScriptTarget = ScriptTarget.es2018) {
19+
public getLanguageLevelOrDefault(defaultLanguageLevel: ScriptTarget = ScriptTarget.ES2018) {
2020
const languageLevel = this.getLanguageLevel();
2121
return languageLevel ? languageLevel : defaultLanguageLevel;
2222
}

src/core/File.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ import { SourceMapGenerator } from "./SourceMapGenerator";
66
import { utils, each } from "realm-utils";
77
import * as fs from "fs";
88
import * as path from "path";
9+
import * as ts from "typescript"
910
import { ensureFuseBoxPath, readFuseBoxModule, isStylesheetExtension, fastHash, joinFuseBoxPath } from "../Utils";
1011

1112
/**
12-
* Same Target Enumerator used in TypeScript
13+
* Same Target Enumerator used in current installed version of TypeScript
1314
*/
14-
export enum ScriptTarget {
15-
es5 = 1,
16-
es6 = 2,
17-
es2015 = 3,
18-
es2016 = 4,
19-
es2017 = 5,
20-
es2018 = 6,
21-
esnext = 7,
22-
}
15+
export { ScriptTarget } from "typescript";
2316

2417
/**
2518
*
@@ -30,7 +23,7 @@ export enum ScriptTarget {
3023
export class File {
3124
public isFuseBoxBundle = false;
3225

33-
public languageLevel = ScriptTarget.es2018;
26+
public languageLevel = ts.ScriptTarget.ES2018;
3427

3528
public es6module = false;
3629

@@ -204,7 +197,7 @@ export class File {
204197
return file;
205198
}
206199

207-
public setLanguageLevel(level: ScriptTarget) {
200+
public setLanguageLevel(level: ts.ScriptTarget) {
208201
if (this.languageLevel < level) {
209202
this.languageLevel = level;
210203
}
@@ -473,12 +466,12 @@ export class File {
473466
this.context.fuse.producer.addWarning(
474467
"unresolved",
475468
`Statement "${this.info.fuseBoxPath}" has failed to resolve in module "${this.collection &&
476-
this.collection.name}"`,
469+
this.collection.name}"`,
477470
);
478471
} else {
479472
this.addError(
480473
`Asset reference "${this.info.fuseBoxPath}" has failed to resolve in module "${this.collection &&
481-
this.collection.name}"`,
474+
this.collection.name}"`,
482475
);
483476
}
484477
}
@@ -637,20 +630,11 @@ export class File {
637630
}
638631
}
639632

640-
public transpileUsingTypescript() {
633+
public transpileUsingTypescript(): ts.TranspileOutput | never {
641634
try {
642-
const ts = require("typescript");
643-
try {
644-
return ts.transpileModule(this.contents, this.getTranspilationConfig());
645-
} catch (e) {
646-
this.context.fatal(`${this.info.absPath}: ${e}`);
647-
return;
648-
}
635+
return ts.transpileModule(this.contents, this.getTranspilationConfig())
649636
} catch (e) {
650-
this.context.fatal(`TypeScript automatic transpilation has failed. Please check that:
651-
- You have TypeScript installed
652-
- Your tsconfig.json file is not malformed.\nError message: ${e.message}`);
653-
return;
637+
this.context.fatal(`${this.info.absPath}: ${e}`);
654638
}
655639
}
656640

src/core/FuseBox.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "fs";
22
import * as process from "process";
3+
import * as ts from "typescript";
34
import { each, utils, chain, Chainable } from "realm-utils";
45
import { CustomTransformers } from "typescript";
56
import { ensureUserPath, contains, printCurrentVersion } from "./../Utils";
@@ -21,10 +22,43 @@ import { CombinedTargetAndLanguageLevel } from './CombinedTargetAndLanguageLevel
2122

2223
const appRoot = require("app-root-path");
2324

25+
/**
26+
* Typecheck custom tsconfig provided as array.
27+
* It's Compiler options but replaced enums to equivalent strings keys
28+
* as it'd be seen in a `tsconfig.json` file
29+
* Check `typescript.d.ts`:
30+
* - `CompilerOptionsValue`
31+
* - `CompilerOptions`
32+
* e.g:
33+
* - Instead of `target: 1`, user can input `target: 'ES5'`
34+
*/
35+
export type tsConfigFileCompilerOptions = {
36+
[key in keyof ts.CompilerOptions]:
37+
ts.CompilerOptions[key] extends string
38+
? ts.CompilerOptions[key]
39+
: ts.CompilerOptions[key] extends boolean
40+
? ts.CompilerOptions[key]
41+
: ts.CompilerOptions[key] extends ts.ScriptTarget
42+
? keyof typeof ts.ScriptTarget
43+
: ts.CompilerOptions[key] extends ts.JsxEmit
44+
? keyof typeof ts.JsxEmit
45+
: ts.CompilerOptions[key] extends string[]
46+
? string[]
47+
: ts.CompilerOptions[key] extends ts.ModuleKind
48+
? keyof typeof ts.ModuleKind
49+
: ts.CompilerOptions[key] extends ts.ModuleResolutionKind
50+
? keyof typeof ts.ModuleResolutionKind
51+
: ts.CompilerOptions extends ts.NewLineKind
52+
? keyof typeof ts.NewLineKind
53+
: ts.CompilerOptions extends ts.MapLike<string[]>
54+
? ts.MapLike<string[]>
55+
: string | number | boolean | (string | number)[] | string[] | ts.MapLike<string[]> | null | undefined
56+
}
57+
2458
export interface FuseBoxOptions {
2559
homeDir?: string;
2660
modulesFolder?: string | string[];
27-
tsConfig?: string;
61+
tsConfig?: string | tsConfigFileCompilerOptions[];
2862
package?: string | { name: string; main: string };
2963
dynamicImportsEnabled?: boolean;
3064
cache?: boolean;

0 commit comments

Comments
 (0)
0