Skip to content

Commit

Permalink
Merge pull request #35 from manuth/master
Browse files Browse the repository at this point in the history
Add `symbol` type Workaround
  • Loading branch information
kaelzhang authored Aug 17, 2022
2 parents ed1d9f4 + c774e4f commit 80fcd67
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ interface Location {
}
```
### Query comments in TypeScript
`comment-json` provides a `symbol`-type called `CommentSymbol` which can be used for querying comments.
Furthermore, a type `CommentDescriptor` is provided for enforcing properly formatted symbol names:
```ts
import { CommentDescriptor, CommentSymbol, parse } from "comment-json"

const parsed = parse(`{ /* test */ "foo": "bar" }`)
const symbolName: CommentDescriptor: "before-prop:foo"; // typescript only allows properly formatted symbol names here
console.log(parsed[Symbol.for(symbolName) as CommentSymbol][0].value)
```
In this example, casting to `Symbol.for(symbolName)` to `CommentSymbol` is mandatory.
Otherwise, TypeScript won't detect that you're trying to query comments.
### Parse into an object without comments
```js
Expand Down
19 changes: 17 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@
// Definitions by: Jason Dent <https://github.com/Jason3S>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare const commentSymbol: unique symbol

export type CommentPrefix = 'before'
| 'after-prop'
| 'after-colon'
| 'after-value'
| 'after'

export type CommentDescriptor = `${CommentPrefix}:${string}`
| 'before'
| 'before-all'
| 'after-all'

export type CommentSymbol = typeof commentSymbol

export class CommentArray<TValue> extends Array<TValue> {
[key: symbol]: CommentToken
[commentSymbol]: CommentToken[]
}

export type CommentJSONValue = number
Expand All @@ -17,7 +32,7 @@ export type CommentJSONValue = number

export interface CommentObject {
[key: string]: CommentJSONValue
[key: symbol]: CommentToken
[commentSymbol]: CommentToken[]
}

export interface CommentToken {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "npm run test:only",
"test:only": "npm run test:ts && npm run test:node",
"test:ts": "tsc test/ts/test-ts.ts && node test/ts/test-ts.js",
"test:ts": "tsc -b test/ts/tsconfig.build.json && node test/ts/test-ts.js",
"test:node": "NODE_DEBUG=comment-json nyc ava --timeout=10s --verbose",
"test:dev": "npm run test:only && npm run report:dev",
"lint": "eslint .",
Expand Down
15 changes: 14 additions & 1 deletion test/ts/test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {

CommentArray,
CommentObject,
assign
assign,
CommentDescriptor,
CommentSymbol
} from '../..'

const assert = (test: boolean, message: string): void => {
Expand Down Expand Up @@ -33,3 +35,14 @@ assert(stringify(obj, null, 2) === `{
}`, 'assign')

assert(Array.isArray(tokenize(str)), 'tokenize')

const comment = "this is a comment"
let commentDescriptor: CommentDescriptor = `before:0`

const commentSrc = `[
//${comment}
"bar"
]`

assert((parse(commentSrc) as CommentArray<string>)[Symbol.for(commentDescriptor) as CommentSymbol][0].value === comment, 'comment parse')
commentDescriptor = "before";
8 changes: 8 additions & 0 deletions test/ts/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": [],
"references": [
{
"path": "."
}
]
}
9 changes: 9 additions & 0 deletions test/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES2015"
},
"include": [
"./test-ts.ts"
]
}

0 comments on commit 80fcd67

Please sign in to comment.