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:
// - `...Type` => `Type[]`
// - `...(TypeA|TypeB)` => `(TypeA|TypeB)[]`
const isRestType = type.startsWith('...');
if (isRestType) {
type = type.replace(/^\.\.\./, '');
const restPattern = /^\.\.\./;
const isRest = type.match(restPattern);
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
// 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.
const singleType = splittedType.replace(/(\[\])+$/, '');
// Handle eventual type conflict in static constructors block. For
@ -185,14 +213,13 @@ function parseType(type, options) {
}
return splittedType;
}).join(' | ');
if (isRestType) {
type += '[]';
}
// We declare settable properties as nullable to be compatible with
// TypeScript `strictNullChecks` option (#1664).
if (options.isSettableProperty && type !== 'any') {
type += ' | null';
// Regroup types.
if (isWrapped) {
type = `(${type})`;
}
if (isRest) {
type += '[]';
}
return type;

View file

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

View file

@ -75,7 +75,7 @@ var HitResult = Base.extend(/** @lends HitResult# */{
*
* @name HitResult#color
* @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
* @property
* @type Color
* @type ?Color
*
* @example {@paperscript}
* // 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
* @property
* @type Color
* @type ?Color
*
* @example {@paperscript}
* // 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
* @name Item#shadowColor
* @type Color
* @type ?Color
*
* @example {@paperscript}
* // 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
* @property
* @type Color
* @type ?Color
*/
}, Base.each(['rotate', 'scale', 'shear', 'skew'], function(key) {
var rotate = key === 'rotate';
@ -3769,7 +3769,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onFrame
* @property
* @type Function
* @type ?Function
* @see View#onFrame
*
* @example {@paperscript}
@ -3796,7 +3796,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseDown
* @property
* @type Function
* @type ?Function
* @see View#onMouseDown
*
* @example {@paperscript}
@ -3846,7 +3846,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseDrag
* @property
* @type Function
* @type ?Function
* @see View#onMouseDrag
*
* @example {@paperscript height=240}
@ -3875,7 +3875,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseUp
* @property
* @type Function
* @type ?Function
* @see View#onMouseUp
*
* @example {@paperscript}
@ -3905,7 +3905,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onClick
* @property
* @type Function
* @type ?Function
* @see View#onClick
*
* @example {@paperscript}
@ -3955,7 +3955,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onDoubleClick
* @property
* @type Function
* @type ?Function
* @see View#onDoubleClick
*
* @example {@paperscript}
@ -4005,7 +4005,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseMove
* @property
* @type Function
* @type ?Function
* @see View#onMouseMove
*
* @example {@paperscript}
@ -4036,7 +4036,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseEnter
* @property
* @type Function
* @type ?Function
* @see View#onMouseEnter
*
* @example {@paperscript}
@ -4098,7 +4098,7 @@ new function() { // Injection scope for hit-test functions shared with project
*
* @name Item#onMouseLeave
* @property
* @type Function
* @type ?Function
* @see View#onMouseLeave
*
* @example {@paperscript}

View file

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

View file

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

View file

@ -135,7 +135,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
*
* @name Tool#onMouseDown
* @property
* @type Function
* @type ?Function
*
* @example {@paperscript}
* // Creating circle shaped paths where the user presses the mouse button:
@ -157,7 +157,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
*
* @name Tool#onMouseDrag
* @property
* @type Function
* @type ?Function
*
* @example {@paperscript}
* // 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
* @property
* @type Function
* @type ?Function
*
* @example {@paperscript}
* // Moving a path to the position of the mouse:
@ -206,7 +206,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
*
* @name Tool#onMouseUp
* @property
* @type Function
* @type ?Function
*
* @example {@paperscript}
* // Creating circle shaped paths where the user releases the mouse:
@ -234,7 +234,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
*
* @name Tool#onKeyDown
* @property
* @type Function
* @type ?Function
*
* @example {@paperscript}
* // Scaling a path whenever the user presses the space bar:
@ -268,7 +268,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
*
* @name Tool#onKeyUp
* @property
* @type Function
* @type ?Function
*
* @example
* tool.onKeyUp = function(event) {

View file

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