Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsonc): annotate return types #3327

Merged
merged 5 commits into from
Apr 24, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use PascalCase for enum
Idiomatic TypeScript uses PascalCase for types and enums
  • Loading branch information
jsejcksn committed Apr 22, 2023
commit 38121e9d980164f550052743c2ecb79a91f1df7a
86 changes: 43 additions & 43 deletions jsonc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@ export function parse(
return new JSONCParser(text, { allowTrailingComma }).parse();
}

enum tokenType {
beginObject,
endObject,
beginArray,
endArray,
nameSeparator,
valueSeparator,
nullOrTrueOrFalseOrNumber,
string,
enum TokenType {
BeginObject,
EndObject,
BeginArray,
EndArray,
NameSeparator,
ValueSeparator,
NullOrTrueOrFalseOrNumber,
String,
}

type Token = {
type: Exclude<
tokenType,
tokenType.string | tokenType.nullOrTrueOrFalseOrNumber
TokenType,
TokenType.String | TokenType.NullOrTrueOrFalseOrNumber
>;
sourceText?: undefined;
position: number;
} | {
type: tokenType.string;
type: TokenType.String;
sourceText: string;
position: number;
} | {
type: tokenType.nullOrTrueOrFalseOrNumber;
type: TokenType.NullOrTrueOrFalseOrNumber;
sourceText: string;
position: number;
};
Expand Down Expand Up @@ -153,22 +153,22 @@ class JSONCParser {

switch (this.#text[i]) {
case "{":
yield { type: tokenType.beginObject, position: i } as const;
yield { type: TokenType.BeginObject, position: i } as const;
break;
case "}":
yield { type: tokenType.endObject, position: i } as const;
yield { type: TokenType.EndObject, position: i } as const;
break;
case "[":
yield { type: tokenType.beginArray, position: i } as const;
yield { type: TokenType.BeginArray, position: i } as const;
break;
case "]":
yield { type: tokenType.endArray, position: i } as const;
yield { type: TokenType.EndArray, position: i } as const;
break;
case ":":
yield { type: tokenType.nameSeparator, position: i } as const;
yield { type: TokenType.NameSeparator, position: i } as const;
break;
case ",":
yield { type: tokenType.valueSeparator, position: i } as const;
yield { type: TokenType.ValueSeparator, position: i } as const;
break;
case '"': { // parse string token
const startIndex = i;
Expand All @@ -186,7 +186,7 @@ class JSONCParser {
shouldEscapeNext = this.#text[i] === "\\" && !shouldEscapeNext;
}
yield {
type: tokenType.string,
type: TokenType.String,
sourceText: this.#text.substring(startIndex, i + 1),
position: startIndex,
} as const;
Expand All @@ -201,7 +201,7 @@ class JSONCParser {
}
i--;
yield {
type: tokenType.nullOrTrueOrFalseOrNumber,
type: TokenType.NullOrTrueOrFalseOrNumber,
sourceText: this.#text.substring(startIndex, i + 1),
position: startIndex,
} as const;
Expand All @@ -211,13 +211,13 @@ class JSONCParser {
}
#parseJSONValue(value: Token): JsonValue {
switch (value.type) {
case tokenType.beginObject:
case TokenType.BeginObject:
return this.#parseObject();
case tokenType.beginArray:
case TokenType.BeginArray:
return this.#parseArray();
case tokenType.nullOrTrueOrFalseOrNumber:
case TokenType.NullOrTrueOrFalseOrNumber:
return this.#parseNullOrTrueOrFalseOrNumber(value);
case tokenType.string:
case TokenType.String:
return this.#parseString(value);
default:
throw new SyntaxError(buildErrorMessage(value));
Expand Down Expand Up @@ -251,17 +251,17 @@ class JSONCParser {
const token1 = this.#getNext();
if (
(isFirst || this.#options.allowTrailingComma) &&
token1.type === tokenType.endObject
token1.type === TokenType.EndObject
) {
return target;
}
if (token1.type !== tokenType.string) {
if (token1.type !== TokenType.String) {
throw new SyntaxError(buildErrorMessage(token1));
}
const key = this.#parseString(token1);

const token2 = this.#getNext();
if (token2.type !== tokenType.nameSeparator) {
if (token2.type !== TokenType.NameSeparator) {
throw new SyntaxError(buildErrorMessage(token2));
}

Expand All @@ -274,10 +274,10 @@ class JSONCParser {
});

const token4 = this.#getNext();
if (token4.type === tokenType.endObject) {
if (token4.type === TokenType.EndObject) {
return target;
}
if (token4.type !== tokenType.valueSeparator) {
if (token4.type !== TokenType.ValueSeparator) {
throw new SyntaxError(buildErrorMessage(token4));
}
}
Expand All @@ -302,23 +302,23 @@ class JSONCParser {
const token1 = this.#getNext();
if (
(isFirst || this.#options.allowTrailingComma) &&
token1.type === tokenType.endArray
token1.type === TokenType.EndArray
) {
return target;
}
target.push(this.#parseJSONValue(token1));

const token2 = this.#getNext();
if (token2.type === tokenType.endArray) {
if (token2.type === TokenType.EndArray) {
return target;
}
if (token2.type !== tokenType.valueSeparator) {
if (token2.type !== TokenType.ValueSeparator) {
throw new SyntaxError(buildErrorMessage(token2));
}
}
}
#parseString(value: {
type: tokenType.string;
type: TokenType.String;
sourceText: string;
position: number;
}): string {
Expand All @@ -333,7 +333,7 @@ class JSONCParser {
return parsed;
}
#parseNullOrTrueOrFalseOrNumber(value: {
type: tokenType.nullOrTrueOrFalseOrNumber;
type: TokenType.NullOrTrueOrFalseOrNumber;
sourceText: string;
position: number;
}): null | boolean | number {
Expand Down Expand Up @@ -361,26 +361,26 @@ class JSONCParser {
function buildErrorMessage({ type, sourceText, position }: Token): string {
let token = "";
switch (type) {
case tokenType.beginObject:
case TokenType.BeginObject:
token = "{";
break;
case tokenType.endObject:
case TokenType.EndObject:
token = "}";
break;
case tokenType.beginArray:
case TokenType.BeginArray:
token = "[";
break;
case tokenType.endArray:
case TokenType.EndArray:
token = "]";
break;
case tokenType.nameSeparator:
case TokenType.NameSeparator:
token = ":";
break;
case tokenType.valueSeparator:
case TokenType.ValueSeparator:
token = ",";
break;
case tokenType.nullOrTrueOrFalseOrNumber:
case tokenType.string:
case TokenType.NullOrTrueOrFalseOrNumber:
case TokenType.String:
// Truncate the string so that it is within 30 lengths.
token = 30 < sourceText.length
? `${sourceText.slice(0, 30)}...`
Expand Down