Skip to content

Commit

Permalink
Add support for comments in jsonc
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Kowalski committed Jul 25, 2024
1 parent 8af61e0 commit ad7b124
Show file tree
Hide file tree
Showing 7 changed files with 1,263 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ["@babel/preset-env"],
};
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const jestConfig: JestConfigWithTsJest = {
isolatedModules: true,
},
],
"^.+\\.[jt]sx?$": "babel-jest",
},
cacheDirectory: "<rootDir>/jestCache",
collectCoverage: false,
Expand All @@ -21,6 +22,7 @@ const jestConfig: JestConfigWithTsJest = {
branches: 100,
},
},
transformIgnorePatterns: ["/node_modules/(?!strip-json-comments).+\\.js$"],
};

export default jestConfig;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@
"dependencies": {
"@typescript-eslint/utils": "^7.13.0",
"js-yaml": "^4.1.0",
"micromatch": "^4.0.7"
"micromatch": "^4.0.7",
"strip-json-comments": "^5.0.1"
},
"devDependencies": {
"@babel/core": "^7.24.9",
"@babel/preset-env": "^7.24.8",
"@types/eslint": "^8.56.10",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
Expand Down
12 changes: 6 additions & 6 deletions src/helpers/readConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jest.mock("js-yaml", () => ({
describe("readConfigFile", () => {
it("should return config from yaml", () => {
(readFileSync as jest.Mock).mockReturnValue('{"name":"yaml"}');
expect(readConfigFile("folderStructure.yaml")).toEqual(
'{"name":"yaml"}',
);
expect(readConfigFile("folderStructure.yaml")).toEqual({
name: "yaml",
});
});

it("should return undefined when yaml config path is incorrect", () => {
Expand All @@ -32,9 +32,9 @@ describe("readConfigFile", () => {
(load as jest.Mock).mockReturnValue(null);
(readFileSync as jest.Mock).mockReturnValue('{"name":"json"}');

expect(readConfigFile("folderStructure.json")).toEqual(
'{"name":"json"}',
);
expect(readConfigFile("folderStructure.json")).toEqual({
name: "json",
});
});

it("should return undefined when json config path is incorrect", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/readConfigFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync } from "fs";

import { load } from "js-yaml";
import stripJsonComments from "strip-json-comments";

export const readConfigFile = <T>(configPath: string): T | undefined => {
let config;
Expand All @@ -10,7 +11,7 @@ export const readConfigFile = <T>(configPath: string): T | undefined => {

if (!config)
config = JSON.parse(
JSON.stringify(readFileSync(configPath, "utf-8")),
stripJsonComments(readFileSync(configPath, "utf-8")),
);
} catch (error) {
return;
Expand Down
Loading

0 comments on commit ad7b124

Please sign in to comment.