jsdoc-type-pratt-parser
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -5,4 +5,6 @@ import { KeyValueResult } from './result/NonTerminalResult'; | ||
export declare function assertTerminal(result?: IntermediateResult): TerminalResult; | ||
export declare function assertKeyValueOrTerminal(result: IntermediateResult): KeyValueResult | TerminalResult; | ||
export declare function assertKeyValueOrName(result: IntermediateResult): KeyValueResult | NameResult; | ||
export declare function assertPlainKeyValueOrTerminal(result: IntermediateResult): KeyValueResult | TerminalResult; | ||
export declare function assertPlainKeyValueOrName(result: IntermediateResult): KeyValueResult | NameResult; | ||
export declare function assertPlainKeyValue(result: IntermediateResult): KeyValueResult; | ||
export declare function assertNumberOrVariadicName(result: IntermediateResult): NumberResult | NameResult | VariadicResult<NameResult>; | ||
export declare function isPlainKeyValue(result: IntermediateResult): result is KeyValueResult; |
@@ -14,3 +14,3 @@ import { Token } from './lexer/Token'; | ||
export declare class UnexpectedTypeError extends Error { | ||
constructor(result: IntermediateResult); | ||
constructor(result: IntermediateResult, message?: string); | ||
} |
@@ -13,3 +13,3 @@ import { QuoteStyle, TerminalResult } from './TerminalResult'; | ||
type: 'JsdocTypeKeyValue'; | ||
value: string; | ||
key: string; | ||
right: TerminalResult | undefined; | ||
@@ -20,2 +20,3 @@ optional: boolean; | ||
quote: QuoteStyle | undefined; | ||
hasLeftSideExpression: false; | ||
}; | ||
@@ -32,2 +33,5 @@ } | ||
right: TerminalResult; | ||
meta: { | ||
hasLeftSideExpression: true; | ||
}; | ||
} | ||
@@ -34,0 +38,0 @@ export interface PropertyResult { |
@@ -194,3 +194,3 @@ import { JsdocObjectKeyValueResult, KeyValueResult, PropertyResult } from './NonTerminalResult'; | ||
type: 'JsdocTypeTuple'; | ||
elements: TerminalResult[]; | ||
elements: TerminalResult[] | KeyValueResult[]; | ||
} | ||
@@ -197,0 +197,0 @@ /** |
{ | ||
"name": "jsdoc-type-pratt-parser", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -39,2 +39,3 @@ "main": "dist/index.js", | ||
"@types/sinon": "^10.0.0", | ||
"@types/sinon-chai": "^3.2.5", | ||
"benchmark": "^2.1.4", | ||
@@ -51,2 +52,3 @@ "catharsis": "^0.9.0", | ||
"sinon": "^10.0.0", | ||
"sinon-chai": "^3.7.0", | ||
"ts-node": "^9.1.1", | ||
@@ -98,3 +100,4 @@ "ts-standard": "^10.0.0", | ||
"access": "public" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -16,5 +16,5 @@ import { KeyValueResult } from './result/NonTerminalResult' | ||
export function assertKeyValueOrTerminal (result: IntermediateResult): KeyValueResult | TerminalResult { | ||
if (result.type === 'JsdocTypeKeyValue' && 'value' in result) { | ||
return result | ||
export function assertPlainKeyValueOrTerminal (result: IntermediateResult): KeyValueResult | TerminalResult { | ||
if (result.type === 'JsdocTypeKeyValue') { | ||
return assertPlainKeyValue(result) | ||
} | ||
@@ -24,8 +24,17 @@ return assertTerminal(result) | ||
export function assertKeyValueOrName (result: IntermediateResult): KeyValueResult | NameResult { | ||
if (result.type === 'JsdocTypeKeyValue' && 'value' in result) { | ||
export function assertPlainKeyValueOrName (result: IntermediateResult): KeyValueResult | NameResult { | ||
if (result.type === 'JsdocTypeName') { | ||
return result | ||
} else if (result.type !== 'JsdocTypeName') { | ||
throw new UnexpectedTypeError(result) | ||
} | ||
return assertPlainKeyValue(result) | ||
} | ||
export function assertPlainKeyValue (result: IntermediateResult): KeyValueResult { | ||
if (!isPlainKeyValue(result)) { | ||
if (result.type === 'JsdocTypeKeyValue') { | ||
throw new UnexpectedTypeError(result, 'Expecting no left side expression.') | ||
} else { | ||
throw new UnexpectedTypeError(result) | ||
} | ||
} | ||
return result | ||
@@ -46,1 +55,5 @@ } | ||
} | ||
export function isPlainKeyValue (result: IntermediateResult): result is KeyValueResult { | ||
return result.type === 'JsdocTypeKeyValue' && !result.meta.hasLeftSideExpression | ||
} |
@@ -45,4 +45,8 @@ import { Token } from './lexer/Token' | ||
export class UnexpectedTypeError extends Error { | ||
constructor (result: IntermediateResult) { | ||
super(`Unexpected type: '${result.type}'`) | ||
constructor (result: IntermediateResult, message?: string) { | ||
let error = `Unexpected type: '${result.type}'.` | ||
if (message !== undefined) { | ||
error += ` Message: ${message}` | ||
} | ||
super(error) | ||
@@ -49,0 +53,0 @@ Object.setPrototypeOf(this, UnexpectedTypeError.prototype) |
@@ -5,3 +5,3 @@ import { InfixParslet } from './Parslet' | ||
import { BaseFunctionParslet } from './BaseFunctionParslet' | ||
import { assertKeyValueOrName } from '../assertTypes' | ||
import { assertPlainKeyValueOrName } from '../assertTypes' | ||
import { Parser } from '../Parser' | ||
@@ -25,3 +25,3 @@ import { FunctionResult } from '../result/TerminalResult' | ||
type: 'JsdocTypeFunction', | ||
parameters: this.getParameters(left).map(assertKeyValueOrName), | ||
parameters: this.getParameters(left).map(assertPlainKeyValueOrName), | ||
arrow: true, | ||
@@ -28,0 +28,0 @@ parenthesis: true, |
import { KeyValueResult, NonTerminalResult } from '../result/NonTerminalResult' | ||
import { assertKeyValueOrTerminal } from '../assertTypes' | ||
import { assertPlainKeyValueOrTerminal } from '../assertTypes' | ||
import { UnexpectedTypeError } from '../errors' | ||
@@ -18,3 +18,3 @@ import { IntermediateResult } from '../result/IntermediateResult' | ||
return parameters.map(p => assertKeyValueOrTerminal(p)) | ||
return parameters.map(p => assertPlainKeyValueOrTerminal(p)) | ||
} | ||
@@ -21,0 +21,0 @@ |
@@ -64,3 +64,3 @@ import { PrefixParslet } from './Parslet' | ||
for (const p of result.parameters) { | ||
if (p.type === 'JsdocTypeKeyValue' && (!this.allowNamedParameters.includes(p.value) || p.meta.quote !== undefined)) { | ||
if (p.type === 'JsdocTypeKeyValue' && (!this.allowNamedParameters.includes(p.key) || p.meta.quote !== undefined)) { | ||
throw new Error(`only allowed named parameters are ${this.allowNamedParameters.join(',')} but got ${p.type}`) | ||
@@ -67,0 +67,0 @@ } |
@@ -59,3 +59,3 @@ import { InfixParslet } from './Parslet' | ||
type: 'JsdocTypeKeyValue', | ||
value: left.value.toString(), | ||
key: left.value.toString(), | ||
right: parser.parseType(Precedence.KEY_VALUE), | ||
@@ -65,3 +65,4 @@ optional: optional, | ||
meta: { | ||
quote | ||
quote, | ||
hasLeftSideExpression: false | ||
} | ||
@@ -79,3 +80,6 @@ } | ||
left: assertTerminal(left), | ||
right: parser.parseType(Precedence.KEY_VALUE) | ||
right: parser.parseType(Precedence.KEY_VALUE), | ||
meta: { | ||
hasLeftSideExpression: true | ||
} | ||
} | ||
@@ -82,0 +86,0 @@ } |
@@ -57,3 +57,3 @@ import { PrefixParslet } from './Parslet' | ||
type: 'JsdocTypeKeyValue', | ||
value: field.value.toString(), | ||
key: field.value.toString(), | ||
right: undefined, | ||
@@ -63,3 +63,4 @@ optional: optional, | ||
meta: { | ||
quote | ||
quote, | ||
hasLeftSideExpression: false | ||
} | ||
@@ -66,0 +67,0 @@ }) |
import { InfixParslet } from './Parslet' | ||
import { TokenType } from '../lexer/Token' | ||
import { Precedence } from '../Precedence' | ||
import { assertKeyValueOrTerminal } from '../assertTypes' | ||
import { assertPlainKeyValueOrTerminal } from '../assertTypes' | ||
import { NoParsletFoundError } from '../errors' | ||
@@ -32,3 +32,3 @@ import { Parser } from '../Parser' | ||
const elements: Array<TerminalResult|KeyValueResult> = [ | ||
assertKeyValueOrTerminal(left) | ||
assertPlainKeyValueOrTerminal(left) | ||
] | ||
@@ -39,3 +39,3 @@ parser.consume(',') | ||
const next = parser.parseIntermediateType(Precedence.PARAMETER_LIST) | ||
elements.push(assertKeyValueOrTerminal(next)) | ||
elements.push(assertPlainKeyValueOrTerminal(next)) | ||
} catch (e) { | ||
@@ -42,0 +42,0 @@ if (this.allowTrailingComma && e instanceof NoParsletFoundError) { |
import { PrefixParslet } from './Parslet' | ||
import { TokenType } from '../lexer/Token' | ||
import { Precedence } from '../Precedence' | ||
import { assertTerminal } from '../assertTypes' | ||
import { assertTerminal, isPlainKeyValue } from '../assertTypes' | ||
import { Parser } from '../Parser' | ||
@@ -32,3 +32,3 @@ import { ParenthesisResult } from '../result/TerminalResult' | ||
return result | ||
} else if (result.type === 'JsdocTypeKeyValue' && 'value' in result) { | ||
} else if (result.type === 'JsdocTypeKeyValue' && isPlainKeyValue(result)) { | ||
return { | ||
@@ -35,0 +35,0 @@ type: 'JsdocTypeParameterList', |
@@ -1,2 +0,2 @@ | ||
import { assertTerminal } from '../assertTypes' | ||
import { assertPlainKeyValue, assertTerminal } from '../assertTypes' | ||
import { TokenType } from '../lexer/Token' | ||
@@ -7,2 +7,3 @@ import { Parser } from '../Parser' | ||
import { TupleResult } from '../result/TerminalResult' | ||
import { IntermediateResult } from '../result/IntermediateResult' | ||
@@ -41,5 +42,13 @@ interface TupleParsletOptions { | ||
if (typeList.type === 'JsdocTypeParameterList') { | ||
result.elements = typeList.elements.map(assertTerminal) | ||
if (typeList.elements[0].type === 'JsdocTypeKeyValue') { | ||
result.elements = typeList.elements.map(assertPlainKeyValue) | ||
} else { | ||
result.elements = typeList.elements.map(assertTerminal) | ||
} | ||
} else { | ||
result.elements = [assertTerminal(typeList)] | ||
if (typeList.type === 'JsdocTypeKeyValue') { | ||
result.elements = [assertPlainKeyValue(typeList)] | ||
} else { | ||
result.elements = [assertTerminal(typeList)] | ||
} | ||
} | ||
@@ -51,3 +60,3 @@ | ||
if (!this.allowQuestionMark && result.elements.some(e => e.type === 'JsdocTypeUnknown')) { | ||
if (!this.allowQuestionMark && result.elements.some((e: IntermediateResult) => e.type === 'JsdocTypeUnknown')) { | ||
throw new Error('Question mark in tuple not allowed') | ||
@@ -54,0 +63,0 @@ } |
@@ -19,3 +19,3 @@ import { QuoteStyle, TerminalResult } from './TerminalResult' | ||
type: 'JsdocTypeKeyValue' | ||
value: string | ||
key: string | ||
right: TerminalResult | undefined | ||
@@ -26,2 +26,3 @@ optional: boolean | ||
quote: QuoteStyle | undefined | ||
hasLeftSideExpression: false | ||
} | ||
@@ -39,2 +40,5 @@ } | ||
right: TerminalResult | ||
meta: { | ||
hasLeftSideExpression: true | ||
} | ||
} | ||
@@ -41,0 +45,0 @@ |
@@ -242,3 +242,3 @@ import { JsdocObjectKeyValueResult, KeyValueResult, PropertyResult } from './NonTerminalResult' | ||
type: 'JsdocTypeTuple' | ||
elements: TerminalResult[] | ||
elements: TerminalResult[]|KeyValueResult[] | ||
} | ||
@@ -245,0 +245,0 @@ |
import { extractSpecialParams, notAvailableTransform, transform, TransformRules } from './transform' | ||
import { assertTerminal } from '../assertTypes' | ||
import { assertTerminal, isPlainKeyValue } from '../assertTypes' | ||
import { TerminalResult } from '../result/TerminalResult' | ||
@@ -242,6 +242,6 @@ import { quote } from './stringify' | ||
JsdocTypeKeyValue: (result, transform) => { | ||
if ('value' in result) { | ||
if (isPlainKeyValue(result)) { | ||
return { | ||
type: 'FieldType', | ||
key: makeName(quote(result.value, result.meta.quote)), | ||
key: makeName(quote(result.key, result.meta.quote)), | ||
value: result.right === undefined ? undefined : transform(result.right) | ||
@@ -248,0 +248,0 @@ } |
@@ -16,2 +16,3 @@ import { TransformRules } from './transform' | ||
} from '../result/TerminalResult' | ||
import { isPlainKeyValue } from '../assertTypes' | ||
@@ -93,6 +94,6 @@ export function identityTransformRules (): TransformRules<NonTerminalResult> { | ||
JsdocTypeKeyValue: (result, transform) => { | ||
if ('value' in result) { | ||
if (isPlainKeyValue(result)) { | ||
return { | ||
type: 'JsdocTypeKeyValue', | ||
value: result.value, | ||
key: result.key, | ||
right: result.right === undefined ? undefined : transform(result.right) as TerminalResult, | ||
@@ -107,3 +108,4 @@ optional: result.optional, | ||
left: transform(result.left) as TerminalResult, | ||
right: transform(result.right) as TerminalResult | ||
right: transform(result.right) as TerminalResult, | ||
meta: result.meta | ||
} | ||
@@ -142,3 +144,3 @@ } | ||
type: 'JsdocTypeTuple', | ||
elements: result.elements.map(transform) as TerminalResult[] | ||
elements: (result.elements as NonTerminalResult[]).map(transform) as TerminalResult[]|KeyValueResult[] | ||
}), | ||
@@ -145,0 +147,0 @@ |
import { extractSpecialParams, notAvailableTransform, transform, TransformRules } from './transform' | ||
import { QuoteStyle, TerminalResult } from '../result/TerminalResult' | ||
import { assertTerminal } from '../assertTypes' | ||
import { assertTerminal, isPlainKeyValue } from '../assertTypes' | ||
import { NonTerminalResult } from '../result/NonTerminalResult' | ||
@@ -272,3 +273,3 @@ export type JtpResult = | ||
type: 'TUPLE', | ||
entries: result.elements.map(transform) | ||
entries: (result.elements as NonTerminalResult[]).map(transform) | ||
}), | ||
@@ -311,3 +312,3 @@ | ||
type: 'NAMED_PARAMETER', | ||
name: param.value, | ||
name: param.key, | ||
typeName: transform(param.right) | ||
@@ -361,3 +362,3 @@ } | ||
JsdocTypeKeyValue: (result, transform) => { | ||
if ('left' in result) { | ||
if (!isPlainKeyValue(result)) { | ||
throw new Error('Keys may not be typed in jsdoctypeparser.') | ||
@@ -369,3 +370,3 @@ } | ||
type: 'RECORD_ENTRY', | ||
key: result.value.toString(), | ||
key: result.key.toString(), | ||
quoteStyle: getQuoteStyle(result.meta.quote), | ||
@@ -390,3 +391,3 @@ value: null, | ||
type: 'RECORD_ENTRY', | ||
key: result.value.toString(), | ||
key: result.key.toString(), | ||
quoteStyle: getQuoteStyle(result.meta.quote), | ||
@@ -393,0 +394,0 @@ value: right, |
import { transform, TransformRules } from './transform' | ||
import { NonTerminalResult } from '../result/NonTerminalResult' | ||
import { TerminalResult } from '../result/TerminalResult' | ||
import { isPlainKeyValue } from '../assertTypes' | ||
@@ -47,3 +48,3 @@ function applyPosition (position: 'prefix' | 'suffix', target: string, value: string): string { | ||
JsdocTypeTuple: (result, transform) => `[${result.elements.map(transform).join(', ')}]`, | ||
JsdocTypeTuple: (result, transform) => `[${(result.elements as NonTerminalResult[]).map(transform).join(', ')}]`, | ||
@@ -80,3 +81,3 @@ JsdocTypeVariadic: (result, transform) => result.meta.position === undefined | ||
JsdocTypeKeyValue: (result, transform) => { | ||
if ('value' in result) { | ||
if (isPlainKeyValue(result)) { | ||
let text = '' | ||
@@ -86,3 +87,3 @@ if (result.readonly) { | ||
} | ||
text += quote(result.value, result.meta.quote) | ||
text += quote(result.key, result.meta.quote) | ||
if (result.optional) { | ||
@@ -89,0 +90,0 @@ text += '?' |
@@ -38,5 +38,5 @@ import { KeyValueResult, NonTerminalResult } from '../result/NonTerminalResult' | ||
if (param.type === 'JsdocTypeKeyValue' && param.meta.quote === undefined) { | ||
if (param.value === 'this') { | ||
if (param.key === 'this') { | ||
result.this = param.right | ||
} else if (param.value === 'new') { | ||
} else if (param.key === 'new') { | ||
result.new = param.right | ||
@@ -43,0 +43,0 @@ } else { |
import { NonTerminalResult } from './result/NonTerminalResult' | ||
import { TerminalResult } from './result/TerminalResult' | ||
import { visitorKeys } from './visitorKeys' | ||
type NodeVisitor = (node: NonTerminalResult, parentNode?: NonTerminalResult, property?: string) => void | ||
function _traverse (node: NonTerminalResult, parentNode?: NonTerminalResult, property?: string, onEnter?: NodeVisitor, onLeave?: NodeVisitor): void { | ||
onEnter?.(node, parentNode, property) | ||
if ('left' in node && node.left !== undefined) { | ||
_traverse(node.left, node, 'left', onEnter, onLeave) | ||
} | ||
if ('element' in node && node.element !== undefined) { | ||
_traverse(node.element, node, 'element', onEnter, onLeave) | ||
} | ||
if ('elements' in node && node.elements !== undefined) { | ||
for (const element of node.elements) { | ||
_traverse(element, node, 'elements', onEnter, onLeave) | ||
function _traverse<T extends NonTerminalResult, U extends NonTerminalResult> (node: T, parentNode?: U, property?: keyof U, onEnter?: NodeVisitor, onLeave?: NodeVisitor): void { | ||
onEnter?.(node, parentNode, property as string) | ||
const keysToVisit = visitorKeys[node.type] as Array<keyof T> | ||
if (keysToVisit !== undefined) { | ||
for (const key of keysToVisit) { | ||
const value = node[key] | ||
if (value !== undefined) { | ||
if (Array.isArray(value)) { | ||
for (const element of value) { | ||
_traverse(element as unknown as NonTerminalResult, node, key, onEnter, onLeave) | ||
} | ||
} else { | ||
_traverse(value as unknown as NonTerminalResult, node, key, onEnter, onLeave) | ||
} | ||
} | ||
} | ||
} | ||
if ('parameters' in node && node.parameters !== undefined) { | ||
for (const param of node.parameters) { | ||
_traverse(param, node, 'parameters', onEnter, onLeave) | ||
} | ||
} | ||
if ('right' in node && node.right !== undefined) { | ||
_traverse(node.right, node, 'right', onEnter, onLeave) | ||
} | ||
if ('returnType' in node && node.returnType !== undefined) { | ||
_traverse(node.returnType, node, 'returnType', onEnter, onLeave) | ||
} | ||
onLeave?.(node, parentNode, property) | ||
onLeave?.(node, parentNode, property as string) | ||
} | ||
@@ -32,0 +29,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
229080
6499
24