Skip to content

Commit

Permalink
Strip JSON comments before parsing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisreddington committed Nov 8, 2024
1 parent 5fcb847 commit c73f024
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ function isDevcontainerContent(obj: unknown): obj is DevcontainerContent {
return true
}

// Add this helper function to strip comments
function stripJsonComments(jsonString: string): string {
// Remove single line comments (// ...)
return jsonString.replace(/\/\/.*$/gm, '')
}

export async function run(): Promise<void> {
try {
const extensionsList = core.getInput('extensions-list', { required: true })
Expand All @@ -95,7 +101,9 @@ export async function run(): Promise<void> {
const fileContent = await fs.promises.readFile(devcontainerPath, 'utf8')
let parsedContent: unknown
try {
parsedContent = JSON.parse(fileContent) as unknown
// Strip comments before parsing
const cleanJson = stripJsonComments(fileContent)
parsedContent = JSON.parse(cleanJson) as unknown
} catch (error) {
throw new Error(
`Invalid JSON in devcontainer.json: ${error instanceof Error ? error.message : String(error)}`
Expand Down

0 comments on commit c73f024

Please sign in to comment.