jsdoctypeparser
Advanced tools
Comparing version 5.0.1 to 5.1.0
'use strict'; | ||
/** @typedef {import('./lib/parsing').AstNode} AstNode */ | ||
const {parse, JSDocTypeSyntaxError} = require('./lib/parsing.js'); | ||
@@ -4,0 +6,0 @@ const {publish, createDefaultPublisher} = require('./lib/publishing.js'); |
'use strict'; | ||
/** | ||
* @typedef AstNode | ||
* @property {string} type | ||
* @property {'none'|'single'|'double'} [quoteStyle] | ||
* @property {string} [key] | ||
* @property {string} [name] | ||
* @property {string} [number] | ||
* @property {string} [path] | ||
* @property {string} [string] | ||
* @property {boolean} [hasEventPrefix] | ||
* @property {boolean} [typeName] | ||
* @property {Object<string,any>} [meta] | ||
* @property {AstNode} [returns] | ||
* @property {AstNode} [new] | ||
* @property {AstNode} [value] | ||
* @property {AstNode} [left] | ||
* @property {AstNode} [right] | ||
* @property {AstNode} [owner] | ||
* @property {AstNode} [subject] | ||
* @property {AstNode} [this] | ||
* @property {AstNode[]} [entries] | ||
* @property {AstNode[]} [objects] | ||
* @property {AstNode[]} [params] | ||
*/ | ||
const {SyntaxError: JSDocTypeSyntaxError, parse} = require('../peg_lib/jsdoctype.js'); | ||
@@ -17,5 +42,7 @@ | ||
* @param {string} typeExprStr Type expression string. | ||
* @return {Object} AST. | ||
* @return {AstNode} AST. | ||
*/ | ||
parse, | ||
parse (typeExprStr) { | ||
return parse(typeExprStr); | ||
}, | ||
}; |
'use strict'; | ||
const {OPTIONAL} = require('./NodeType'); | ||
const {OptionalTypeSyntax} = require('./SyntaxType'); | ||
const {format} = require('util'); | ||
/** @typedef {import('./parsing').AstNode} AstNode */ | ||
/** @typedef {(node) => string} ConcretePublish */ | ||
/** @typedef {{ [T in import('./NodeType').Type]: (node, publish: ConcretePublish) => string }} Publisher */ | ||
/** @typedef {{ [T in import('./NodeType').Type]: (node: AstNode, publish: ConcretePublish) => string }} Publisher */ | ||
/** | ||
* @param node | ||
* @param {AstNode} node | ||
* @param {Publisher} [opt_publisher] | ||
@@ -23,4 +26,4 @@ * @return {string} | ||
* @param {string} str | ||
* @param {boolean} quoteStyle | ||
* @returns {str} Formatted string | ||
* @param {'none'|'single'|'double'|undefined} quoteStyle | ||
* @returns {string} Formatted string | ||
*/ | ||
@@ -71,3 +74,9 @@ function addQuotesForName (str, quoteStyle) { | ||
if (!entryNode.value) return addQuotesForName(entryNode.key, entryNode.quoteStyle); | ||
return format('%s: %s', addQuotesForName(entryNode.key, entryNode.quoteStyle), concretePublish(entryNode.value)); | ||
const keySuffix = ( | ||
entryNode.value.type === OPTIONAL && | ||
entryNode.value.meta.syntax === OptionalTypeSyntax.SUFFIX_KEY_QUESTION_MARK | ||
) | ||
? '?' | ||
: ''; | ||
return format('%s%s: %s', addQuotesForName(entryNode.key, entryNode.quoteStyle), keySuffix, concretePublish(entryNode.value)); | ||
}, | ||
@@ -102,2 +111,5 @@ TUPLE (tupleNode, concretePublish) { | ||
OPTIONAL (optionalNode, concretePublish) { | ||
if (optionalNode.meta.syntax === OptionalTypeSyntax.SUFFIX_KEY_QUESTION_MARK) { | ||
return concretePublish(optionalNode.value); | ||
} | ||
return format('%s=', concretePublish(optionalNode.value)); | ||
@@ -104,0 +116,0 @@ }, |
@@ -69,2 +69,3 @@ 'use strict'; | ||
SUFFIX_EQUALS_SIGN: 'SUFFIX_EQUALS_SIGN', | ||
SUFFIX_KEY_QUESTION_MARK: 'SUFFIX_KEY_QUESTION_MARK', | ||
}; | ||
@@ -71,0 +72,0 @@ |
@@ -16,10 +16,12 @@ 'use strict'; | ||
const childNodeInfo = _collectChildNodeInfo(node); | ||
childNodeInfo.forEach(function([childNode, parentPropName, parentNode]) { | ||
traverse(childNode, opt_onEnter ? (node, pn, pNode) => { | ||
opt_onEnter(node, pn || parentPropName, pNode || parentNode); | ||
} : null, opt_onLeave ? (node, pn, pNode) => { | ||
opt_onLeave(node, pn || parentPropName, pNode || parentNode); | ||
} : null); | ||
}); | ||
if (node) { | ||
const childNodeInfo = _collectChildNodeInfo(node); | ||
childNodeInfo.forEach(function([childNode, parentPropName, parentNode]) { | ||
traverse(childNode, opt_onEnter ? (node, pn, pNode) => { | ||
opt_onEnter(node, pn || parentPropName, pNode || parentNode); | ||
} : null, opt_onLeave ? (node, pn, pNode) => { | ||
opt_onLeave(node, pn || parentPropName, pNode || parentNode); | ||
} : null); | ||
}); | ||
} | ||
@@ -26,0 +28,0 @@ if (opt_onLeave) opt_onLeave(node, null, null); |
{ | ||
"name": "jsdoctypeparser", | ||
"description": "Strict JsDoc type expression parser.", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"author": "Kuniwak <orga.chem.job@gmail.com>", | ||
@@ -34,5 +34,7 @@ "contributors": [ | ||
"prepare": "npm-run-all build", | ||
"pretest": "npm-run-all lint build", | ||
"test": "mocha tests", | ||
"lint": "eslint lib tests", | ||
"pretest": "npm-run-all lint build typecheck", | ||
"test": "npm-run-all mocha typecheck", | ||
"lint": "eslint .", | ||
"mocha": "mocha tests", | ||
"typecheck": "tsc", | ||
"clean": "rimraf ./peg_lib", | ||
@@ -43,15 +45,22 @@ "postclean": "mkdirp ./peg_lib", | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-push": "npm test" | ||
} | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/node": "^12.0.12", | ||
"@types/node": "^12.7.10", | ||
"chai": "^4.2.0", | ||
"eslint": "^6.0.1", | ||
"eslint": "^6.5.1", | ||
"husky": "^3.0.8", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^6.1.4", | ||
"mocha": "^6.2.1", | ||
"npm-run-all": "^4.1.5", | ||
"object.entries-ponyfill": "^1.0.1", | ||
"pegjs": "^0.10.0", | ||
"rimraf": "^2.6.3" | ||
"rimraf": "^3.0.0", | ||
"typescript": "^3.6.3" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -495,3 +495,3 @@ 'use strict'; | ||
Parser.parse(typeExprStr); | ||
}).to.throw('Expected ",", ":", "}", or [ \\t\\r\\n ] but "O" found.'); | ||
}).to.throw('Expected ",", ":", "?", "}", or [ \\t\\r\\n ] but "O" found.'); | ||
}); | ||
@@ -503,3 +503,3 @@ | ||
Parser.parse(typeExprStr); | ||
}).to.throw('Expected ",", ":", "}", or [ \\t\\r\\n ] but "O" found.'); | ||
}).to.throw('Expected ",", ":", "?", "}", or [ \\t\\r\\n ] but "O" found.'); | ||
}); | ||
@@ -506,0 +506,0 @@ |
@@ -169,2 +169,12 @@ 'use strict'; | ||
}); | ||
it('should return an optional record type by type', function() { | ||
const node = parse('{myNum: number=}'); | ||
expect(publish(node)).to.equal('{myNum: number=}'); | ||
}); | ||
it('should return an optional record type by key', function() { | ||
const node = parse('{myNum?: number}'); | ||
expect(publish(node)).to.equal('{myNum?: number}'); | ||
}); | ||
}); | ||
@@ -171,0 +181,0 @@ |
@@ -357,2 +357,12 @@ 'use strict'; | ||
}, | ||
'should visit an empty variadic node': { | ||
given: { type: NodeType.VARIADIC, value: null }, | ||
then: [ | ||
// eventName, node && node.type, propName, parentNode && parentNode.type | ||
['enter', NodeType.VARIADIC, null, null], | ||
['enter', null, 'value', NodeType.VARIADIC], | ||
['leave', null, 'value', NodeType.VARIADIC], | ||
['leave', NodeType.VARIADIC, null, null], | ||
], | ||
}, | ||
'should visit an optional node': { | ||
@@ -563,3 +573,3 @@ given: { | ||
return function(node, propName, parentNode) { | ||
result.push([eventName, node.type, propName, parentNode && parentNode.type]); | ||
result.push([eventName, node && node.type, propName, parentNode && parentNode.type]); | ||
}; | ||
@@ -566,0 +576,0 @@ } |
@@ -14,3 +14,3 @@ { | ||
"lib" | ||
] | ||
], | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
8048118
9878
11