mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-08-01 08:38:54 -04:00
Add support for nullable in documentation
This commit is contained in:
parent
cc15696750
commit
0c885964d3
8 changed files with 75 additions and 48 deletions
gulp/typescript
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue