Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ See the [TypeScript docs][tsconfig] for information on setting up `tsconfig.json
* **loadSync(cwd: string, path?: string): { path?: string, config: any }** Synchronous `load`.
* **readFile(filename: string): Promise<any>** Read a JSON file as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
* **readFileSync(filename: string): any** Synchronous `readFile`.
* **parse(contents: string, filename: string): any** Parse file contents as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
* **parse(contents: string): any** Parse file contents as `tsconfig.json` (strip BOM, parse JSON and support empty contents).

## Contributing

Expand Down
21 changes: 21 additions & 0 deletions src/tsconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ describe('tsconfig', function () {
}
},
path: join(TEST_DIR, 'cwd/tsconfig.json')
},
{
args: [TEST_DIR, 'extends/tsconfig-third.json'],
config: {
compilerOptions: {
module: 'commonjs',
noImplicitAny: true,
outDir: 'dist-third',
removeComments: true,
sourceMap: true,
preserveConstEnums: true,
rootDirs: [
'src',
'test'
]
},
files: [
'./src/bar.ts'
]
},
path: join(TEST_DIR, 'extends/tsconfig-third.json')
}
]

Expand Down
41 changes: 38 additions & 3 deletions src/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,23 @@ export function readFile (filename: string): Promise<any> {
return reject(err)
}

let obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for let here, mergeObjects is mutating target anyway.

try {
return resolve(parse(contents, filename))
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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)
})
})
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const key

if (source[key] instanceof Object) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make a little isObject helper over using instanceof personally.

Object.assign(source[key], mergeObjects(target[key], source[key]))
Copy link
Member

Choose a reason for hiding this comment

The 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 else statement and make both assign to target and delete the final Object.assign to avoid additional work.

}
}
Object.assign(target, source)
return target
}
14 changes: 14 additions & 0 deletions tests/extends/tsconfig-second.json
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"
]
}

6 changes: 6 additions & 0 deletions tests/extends/tsconfig-third.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig-second.json",
"compilerOptions": {
"outDir": "dist-third"
}
}
18 changes: 18 additions & 0 deletions tests/extends/tsconfig.json
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` */
]
}