Add support for nullable in documentation

This commit is contained in:
Samuel Asensi 2019-07-12 11:52:53 +02:00 committed by Jürg Lehni
parent cc15696750
commit 0c885964d3
8 changed files with 75 additions and 48 deletions

View file

@ -154,13 +154,41 @@ function parseType(type, options) {
// `...` prefix and add `[]` as a suffix: // `...` prefix and add `[]` as a suffix:
// - `...Type` => `Type[]` // - `...Type` => `Type[]`
// - `...(TypeA|TypeB)` => `(TypeA|TypeB)[]` // - `...(TypeA|TypeB)` => `(TypeA|TypeB)[]`
const isRestType = type.startsWith('...'); const restPattern = /^\.\.\./;
if (isRestType) { const isRest = type.match(restPattern);
type = type.replace(/^\.\.\./, ''); if (isRest) {
type = type.replace(restPattern, '');
} }
const wrappedPattern = /^\(([^\)]+)\)$/;
const isWrapped = type.match(wrappedPattern);
if (isWrapped) {
type = type.replace(wrappedPattern, '$1');
}
// Handle multiple types possibility by splitting on `|` then re-joining // Handle multiple types possibility by splitting on `|` then re-joining
// back parsed types. // back parsed types.
type = type.split('|').map(splittedType => { const types = type.split('|');
// Hanle nullable type:
// - `?Type` => `Type|null`
// - `?TypeA|TypeB` => `TypeA|TypeB|null`
// - `?TypeA|?TypeB` => `TypeA|TypeB|null`
// If at least one type is nullable, we add null type at the end of the
// list.
const nullablePattern = /^\?/;
let isNullable = false;
for (let i = 0; i < types.length; i++) {
if (types[i].match(nullablePattern)) {
types[i] = types[i].replace(nullablePattern, '');
isNullable = true;
}
}
if (isNullable) {
types.push('null');
}
type = types.map(splittedType => {
// Get type without array suffix `[]` for easier matching. // Get type without array suffix `[]` for easier matching.
const singleType = splittedType.replace(/(\[\])+$/, ''); const singleType = splittedType.replace(/(\[\])+$/, '');
// Handle eventual type conflict in static constructors block. For // Handle eventual type conflict in static constructors block. For
@ -185,14 +213,13 @@ function parseType(type, options) {
} }
return splittedType; return splittedType;
}).join(' | '); }).join(' | ');
if (isRestType) {
type += '[]';
}
// We declare settable properties as nullable to be compatible with // Regroup types.
// TypeScript `strictNullChecks` option (#1664). if (isWrapped) {
if (options.isSettableProperty && type !== 'any') { type = `(${type})`;
type += ' | null'; }
if (isRest) {
type += '[]';
} }
return type; return type;

View file

@ -265,7 +265,7 @@ var Tween = Base.extend(Emitter, /** @lends Tween# */{
* *
* @name Tween#onUpdate * @name Tween#onUpdate
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Display tween progression values: * // Display tween progression values:

View file

@ -75,7 +75,7 @@ var HitResult = Base.extend(/** @lends HitResult# */{
* *
* @name HitResult#color * @name HitResult#color
* @property * @property
* @type Color * @type ?Color
*/ */
/** /**

View file

@ -3081,7 +3081,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#strokeColor * @name Item#strokeColor
* @property * @property
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Setting the stroke color of a path: * // Setting the stroke color of a path:
@ -3243,7 +3243,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#fillColor * @name Item#fillColor
* @property * @property
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Setting the fill color of a path to red: * // Setting the fill color of a path to red:
@ -3277,7 +3277,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @property * @property
* @name Item#shadowColor * @name Item#shadowColor
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Creating a circle with a black shadow: * // Creating a circle with a black shadow:
@ -3323,7 +3323,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#selectedColor * @name Item#selectedColor
* @property * @property
* @type Color * @type ?Color
*/ */
}, Base.each(['rotate', 'scale', 'shear', 'skew'], function(key) { }, Base.each(['rotate', 'scale', 'shear', 'skew'], function(key) {
var rotate = key === 'rotate'; var rotate = key === 'rotate';
@ -3769,7 +3769,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onFrame * @name Item#onFrame
* @property * @property
* @type Function * @type ?Function
* @see View#onFrame * @see View#onFrame
* *
* @example {@paperscript} * @example {@paperscript}
@ -3796,7 +3796,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseDown * @name Item#onMouseDown
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseDown * @see View#onMouseDown
* *
* @example {@paperscript} * @example {@paperscript}
@ -3846,7 +3846,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseDrag * @name Item#onMouseDrag
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseDrag * @see View#onMouseDrag
* *
* @example {@paperscript height=240} * @example {@paperscript height=240}
@ -3875,7 +3875,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseUp * @name Item#onMouseUp
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseUp * @see View#onMouseUp
* *
* @example {@paperscript} * @example {@paperscript}
@ -3905,7 +3905,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onClick * @name Item#onClick
* @property * @property
* @type Function * @type ?Function
* @see View#onClick * @see View#onClick
* *
* @example {@paperscript} * @example {@paperscript}
@ -3955,7 +3955,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onDoubleClick * @name Item#onDoubleClick
* @property * @property
* @type Function * @type ?Function
* @see View#onDoubleClick * @see View#onDoubleClick
* *
* @example {@paperscript} * @example {@paperscript}
@ -4005,7 +4005,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseMove * @name Item#onMouseMove
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseMove * @see View#onMouseMove
* *
* @example {@paperscript} * @example {@paperscript}
@ -4036,7 +4036,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseEnter * @name Item#onMouseEnter
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseEnter * @see View#onMouseEnter
* *
* @example {@paperscript} * @example {@paperscript}
@ -4098,7 +4098,7 @@ new function() { // Injection scope for hit-test functions shared with project
* *
* @name Item#onMouseLeave * @name Item#onMouseLeave
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseLeave * @see View#onMouseLeave
* *
* @example {@paperscript} * @example {@paperscript}

View file

@ -763,7 +763,7 @@ var Raster = Item.extend(/** @lends Raster# */{
* *
* @name Raster#onLoad * @name Raster#onLoad
* @property * @property
* @type Function * @type ?Function
* *
* @example * @example
* var url = 'http://assets.paperjs.org/images/marilyn.jpg'; * var url = 'http://assets.paperjs.org/images/marilyn.jpg';
@ -789,7 +789,7 @@ var Raster = Item.extend(/** @lends Raster# */{
* *
* @name Raster#onError * @name Raster#onError
* @property * @property
* @type Function * @type ?Function
*/ */
_getBounds: function(matrix, options) { _getBounds: function(matrix, options) {

View file

@ -411,7 +411,7 @@ var Style = Base.extend(new function() {
* *
* @name Style#strokeColor * @name Style#strokeColor
* @property * @property
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Setting the stroke color of a path: * // Setting the stroke color of a path:
@ -568,7 +568,7 @@ var Style = Base.extend(new function() {
* *
* @name Style#fillColor * @name Style#fillColor
* @property * @property
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Setting the fill color of a path to red: * // Setting the fill color of a path to red:
@ -599,7 +599,7 @@ var Style = Base.extend(new function() {
* *
* @property * @property
* @name Style#shadowColor * @name Style#shadowColor
* @type Color * @type ?Color
* *
* @example {@paperscript} * @example {@paperscript}
* // Creating a circle with a black shadow: * // Creating a circle with a black shadow:
@ -643,7 +643,7 @@ var Style = Base.extend(new function() {
* *
* @name Style#selectedColor * @name Style#selectedColor
* @property * @property
* @type Color * @type ?Color
*/ */
/** /**

View file

@ -135,7 +135,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onMouseDown * @name Tool#onMouseDown
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Creating circle shaped paths where the user presses the mouse button: * // Creating circle shaped paths where the user presses the mouse button:
@ -157,7 +157,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onMouseDrag * @name Tool#onMouseDrag
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Draw a line by adding a segment to a path on every mouse drag event: * // Draw a line by adding a segment to a path on every mouse drag event:
@ -180,7 +180,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onMouseMove * @name Tool#onMouseMove
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Moving a path to the position of the mouse: * // Moving a path to the position of the mouse:
@ -206,7 +206,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onMouseUp * @name Tool#onMouseUp
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Creating circle shaped paths where the user releases the mouse: * // Creating circle shaped paths where the user releases the mouse:
@ -234,7 +234,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onKeyDown * @name Tool#onKeyDown
* @property * @property
* @type Function * @type ?Function
* *
* @example {@paperscript} * @example {@paperscript}
* // Scaling a path whenever the user presses the space bar: * // Scaling a path whenever the user presses the space bar:
@ -268,7 +268,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* *
* @name Tool#onKeyUp * @name Tool#onKeyUp
* @property * @property
* @type Function * @type ?Function
* *
* @example * @example
* tool.onKeyUp = function(event) { * tool.onKeyUp = function(event) {

View file

@ -746,7 +746,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onFrame * @name View#onFrame
* @property * @property
* @type Function * @type ?Function
* @see Item#onFrame * @see Item#onFrame
* *
* @example {@paperscript} * @example {@paperscript}
@ -768,7 +768,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onResize * @name View#onResize
* @property * @property
* @type Function * @type ?Function
* *
* @example * @example
* // Repositioning items when a view is resized: * // Repositioning items when a view is resized:
@ -793,7 +793,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseDown * @name View#onMouseDown
* @property * @property
* @type Function * @type ?Function
* @see Item#onMouseDown * @see Item#onMouseDown
*/ */
@ -807,7 +807,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseDrag * @name View#onMouseDrag
* @property * @property
* @type Function * @type ?Function
* @see Item#onMouseDrag * @see Item#onMouseDrag
*/ */
@ -818,7 +818,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseUp * @name View#onMouseUp
* @property * @property
* @type Function * @type ?Function
* @see Item#onMouseUp * @see Item#onMouseUp
*/ */
@ -832,7 +832,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onClick * @name View#onClick
* @property * @property
* @type Function * @type ?Function
* @see Item#onClick * @see Item#onClick
*/ */
@ -846,7 +846,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onDoubleClick * @name View#onDoubleClick
* @property * @property
* @type Function * @type ?Function
* @see Item#onDoubleClick * @see Item#onDoubleClick
*/ */
@ -860,7 +860,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseMove * @name View#onMouseMove
* @property * @property
* @type Function * @type ?Function
* @see Item#onMouseMove * @see Item#onMouseMove
*/ */
@ -875,7 +875,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseEnter * @name View#onMouseEnter
* @property * @property
* @type Function * @type ?Function
* @see Item#onMouseEnter * @see Item#onMouseEnter
*/ */
@ -889,7 +889,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* *
* @name View#onMouseLeave * @name View#onMouseLeave
* @property * @property
* @type Function * @type ?Function
* @see View#onMouseLeave * @see View#onMouseLeave
*/ */