Skip to content

Commit

Permalink
Add support for comments and trailing commas (#58)
Browse files Browse the repository at this point in the history
* Add support for comments and trailing commas

* Switch from JSON.parse to JSON5.parse

resolves #48

* add ts typings for json5
  • Loading branch information
JakobJingleheimer authored and jonaskello committed Jul 28, 2018
1 parent 6360b2e commit bc76058
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
"typescript": "^2.4.1"
},
"dependencies": {
"@types/json5": "^0.0.29",
"deepmerge": "^2.0.1",
"json5": "^1.0.1",
"minimist": "^1.2.0",
"strip-bom": "^3.0.0",
"strip-json-comments": "^2.0.1"
"strip-bom": "^3.0.0"
},
"scripts": {
"start": "cd src && ts-node index.ts",
Expand Down
9 changes: 5 additions & 4 deletions src/tsconfig-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as path from "path";
import * as fs from "fs";
import * as deepmerge from "deepmerge";
import * as StripJsonComments from "strip-json-comments";
// tslint:disable-next-line:no-require-imports
// tslint:disable:no-require-imports
import JSON5 = require("json5");
import StripBom = require("strip-bom");
// tslint:enable:no-require-imports

/**
* Typing for the parts of tsconfig that we care about
Expand Down Expand Up @@ -108,8 +109,8 @@ export function loadTsconfig(
}

const configString = readFileSync(configFilePath);
const cleanedJson = StripBom(StripJsonComments(configString));
const config: Tsconfig = JSON.parse(cleanedJson);
const cleanedJson = StripBom(configString);
const config: Tsconfig = JSON5.parse(cleanedJson);
let extendedConfig = config.extends;

if (extendedConfig) {
Expand Down
14 changes: 14 additions & 0 deletions test/tsconfig-loader-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ describe("loadConfig", () => {
assert.deepEqual(res, config);
});

it("It should load a config with trailing commas", () => {
const config = { compilerOptions: { baseUrl: "hej" } };
const res = loadTsconfig(
"/root/dir1/tsconfig.json",
path => path === "/root/dir1/tsconfig.json",
_ => `{
"compilerOptions": {
"baseUrl": "hej",
},
}`
);
assert.deepEqual(res, config);
});

it("It should load a config with extends and overwrite baseUrl", () => {
const firstConfig = {
extends: "../base-config.json",
Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/deepmerge/-/deepmerge-1.3.2.tgz#a87837384624d63e8c3df3ae85693d574ea6b5db"

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"

"@types/minimist@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
Expand Down Expand Up @@ -1119,6 +1123,12 @@ json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

json5@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
dependencies:
minimist "^1.2.0"

jsprim@^1.2.2:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
Expand Down Expand Up @@ -1946,10 +1956,6 @@ strip-indent@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68"

strip-json-comments@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"

[email protected]:
version "5.4.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54"
Expand Down

0 comments on commit bc76058

Please sign in to comment.