-
Notifications
You must be signed in to change notification settings - Fork 21
Handle "extends" #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Handle "extends" #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,11 +163,23 @@ export function readFile (filename: string): Promise<any> { | |
| return reject(err) | ||
| } | ||
|
|
||
| let obj | ||
| try { | ||
| return resolve(parse(contents, filename)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd need to keep the signature backward compatible here, otherwise it's a breaking change. I'd just make it optional, it used to be used for debugging and would still be useful to figure out which file has the syntax error. |
||
| obj = parse(contents) | ||
| } catch (err) { | ||
| return reject(err) | ||
| } | ||
|
|
||
| if (obj.extends !== undefined) { | ||
| const filepath = filename.replace(path.basename(filename), '') | ||
| const extendsFilename = resolveSync(filepath, obj.extends) as string | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't do a synchronous operation in an async context, it defeats the purpose of supporting an async API. |
||
| const extendsObj = readFileSync(extendsFilename) | ||
| delete obj.extends | ||
|
|
||
| obj = mergeObjects(extendsObj, obj) | ||
| } | ||
|
|
||
| return resolve(obj) | ||
| }) | ||
| }) | ||
| } | ||
|
|
@@ -177,14 +189,24 @@ export function readFile (filename: string): Promise<any> { | |
| */ | ||
| export function readFileSync (filename: string): any { | ||
| const contents = fs.readFileSync(filename, 'utf8') | ||
| let obj = parse(contents) | ||
|
|
||
| if (obj.extends !== undefined) { | ||
| const filepath = filename.replace(path.basename(filename), '') | ||
| const extendsFilename = resolveSync(filepath, obj.extends) as string | ||
| const extendsObj = readFileSync(extendsFilename) | ||
| delete obj.extends | ||
|
|
||
| obj = mergeObjects(extendsObj, obj) | ||
| } | ||
|
|
||
| return parse(contents, filename) | ||
| return obj | ||
| } | ||
|
|
||
| /** | ||
| * Parse `tsconfig.json` file. | ||
| */ | ||
| export function parse (contents: string, filename: string) { | ||
| export function parse (contents: string) { | ||
| const data = stripComments(stripBom(contents)) | ||
|
|
||
| // A tsconfig.json file is permitted to be completely empty. | ||
|
|
@@ -230,3 +252,16 @@ function isFile (stats: fs.Stats | void) { | |
| function isDirectory (stats: fs.Stats | void) { | ||
| return stats ? (stats as fs.Stats).isDirectory() : false | ||
| } | ||
|
|
||
| /** | ||
| * Simple object merging | ||
| */ | ||
| function mergeObjects (target: any, source: any): any { | ||
| for (let key of Object.keys(source)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (source[key] instanceof Object) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd make a little |
||
| Object.assign(source[key], mergeObjects(target[key], source[key])) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of confusing, you're assigning backward onto the source and then copying back onto the target in a roundabout way - you could just use an |
||
| } | ||
| } | ||
| Object.assign(target, source) | ||
| return target | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist-second", | ||
| "rootDirs": [ | ||
| "src", | ||
| "test" | ||
| ] | ||
| }, | ||
| "files": [ | ||
| "./src/bar.ts" | ||
| ] | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "./tsconfig-second.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist-third" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| // Compiler options. | ||
| "compilerOptions": { | ||
| "module": "commonjs", | ||
| "noImplicitAny": true, | ||
| "removeComments": true, | ||
| "preserveConstEnums": true, | ||
| "outDir": "dist", | ||
| "sourceMap": true, | ||
| "rootDirs": [ | ||
| "src" | ||
| ] | ||
| }, | ||
| // These are the files to compile with. | ||
| "files": [ | ||
| "./src/foo.ts" /* Use `foo.ts` */ | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for
lethere,mergeObjectsis mutatingtargetanyway.