-
Notifications
You must be signed in to change notification settings - Fork 3k
Adding plumbing to support more granular and maintainable typings #870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
david-driscoll
wants to merge
5
commits into
ReactiveX:master
from
david-driscoll:common-type-plumbing
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4627e3d
style(typings): Use typeof to capture typings for all static Observab…
david-driscoll ad583eb
chore(typings): Adding plumbing to allow the types added to Observabl…
david-driscoll b007210
style(typings): Added `ObservableInput` type
david-driscoll 065cd5c
chore(tsconfig): Remove codeFormatOptions, moving to separate PR
david-driscoll 5fd05a2
style(typings): Added the ability to compute typings
david-driscoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* tslint:disable:class-name */ /* tslint:disable:no-unused-variable */ /* tslint:disable:max-line-length */ | ||
import {Observable} from './Observable'; | ||
import {ConnectableObservable} from './observable/ConnectableObservable'; | ||
import {Scheduler} from './Scheduler'; | ||
import {Notification} from './Notification'; | ||
import {Subject} from './Subject'; | ||
import {Observer} from './Observer'; | ||
import {GroupedObservable} from './operator/groupBy-support'; | ||
import {GroupByObservable} from './operator/groupBy'; | ||
import {TimeInterval} from './operator/extended/timeInterval'; | ||
import {ObservableInput, ObservableOrPromise, ArrayOrIterator, _Selector, _IndexSelector, _SwitchMapResultSelector, _ObservableMergeMapProjector, _IteratorMergeMapProjector, _Predicate, _PredicateObservable, _Comparer, _Accumulator, _MergeAccumulator} from './types'; | ||
|
||
/* ||| MARKER ||| */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As part of my rebase I also made a tweak here, any generate-rated interfaces will be but between the two markers, allowing maintenance to happen directly in the |
||
/* ||| MARKER ||| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {Observable} from './Observable'; | ||
export type ObservableOrPromise<T> = Observable<T> | Promise<T>; | ||
export type ArrayOrIterator<T> = Iterator<T> | ArrayLike<T> | Array<T>; | ||
export type ObservableInput<T> = Observable<T> | Promise<T> | Iterator<T> | ArrayLike<T>; | ||
|
||
export type _Selector<T, TResult> = (value: T) => TResult; | ||
export type _IndexSelector<T, TResult> = (value: T, index: number) => TResult; | ||
export type _SwitchMapResultSelector<T1, T2, TResult> = (outerValue: T1, innerValue: T2, outerIndex: number, innerIndex: number) => TResult; | ||
export type _ObservableMergeMapProjector<T, R> = (value: T, index: number) => ObservableOrPromise<R>; | ||
export type _IteratorMergeMapProjector<T, R> = (value: T, index: number) => ArrayOrIterator<R>; | ||
|
||
export type _Predicate<T> = _Selector<T, boolean>; | ||
export type _PredicateObservable<T> = (value: T, index: number, observable: Observable<T>) => boolean; | ||
|
||
export type _Comparer<T, TResult> = (value1: T, value2: T) => TResult; | ||
export type _Accumulator<T, TAcc> = (acc: TAcc, value: T) => TAcc; | ||
export type _MergeAccumulator<T, TAcc> = (acc: TAcc, value: T) => Observable<TAcc>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare type IterableShim<T> = Iterable<T>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
var fs = require('fs'); | ||
var regex = /export interface .*?Operators<T> \{([\S|\s]*)\}/; | ||
|
||
var core = fs.readFileSync('./src/CoreOperators.ts').toString(); | ||
var kitchenSink = fs.readFileSync('./src/Rx.KitchenSink.ts').toString(); | ||
var combinedMethods = core.match(regex)[1].trim() + '\n' + kitchenSink.match(regex)[1].trim(); | ||
var contents = combinedMethods.split('\n'); | ||
|
||
var operators = {}; | ||
var fileResult = ''; | ||
|
||
for (var i = 0; i < contents.length; i++) { | ||
var item = contents[i].trim(); | ||
if (item) { | ||
var file = item.match(/(.*?)\: operator.operator_proto_(.*?)<T>;/); | ||
if (!file) { | ||
continue; | ||
} | ||
|
||
var filename = file[2].trim(); | ||
var fileContent; | ||
|
||
if (fs.existsSync('./src/operator/' + filename + '.ts')) { | ||
fileContent = fs.readFileSync('./src/operator/' + filename + '.ts').toString('utf8'); | ||
} else { | ||
fileContent = fs.readFileSync('./src/operator/extended/' + filename + '.ts').toString('utf8'); | ||
} | ||
|
||
fileContent = computeTypingsFor(fileContent); | ||
|
||
var methods = []; | ||
|
||
var r = new RegExp('export function [_]?' + filename + '([\\s|\\S]*?[\\;\\{])', 'g'); | ||
|
||
do { | ||
var result = r.exec(fileContent); | ||
if (result) { | ||
var method = result[1].trim(); | ||
if (methods.length > 0 && method.indexOf('{') > -1) { | ||
continue; | ||
} | ||
|
||
method = method.split(/\n/g) | ||
.filter(function (x) { return !!x; }) | ||
.map(function (x) { return ('' + x).trim(); }) | ||
.join(' ') | ||
.replace(/ = .*?([\,|\)])/g, '$1'); | ||
|
||
if (method[method.length - 1] === ';' || method[method.length - 1] === '{') { | ||
method = method.substr(0, method.length - 1).trim(); | ||
} | ||
|
||
method = method.replace(/^<T>/, '').replace(/^<T, /, '<'); | ||
methods.push(method); | ||
} | ||
} while(result); | ||
|
||
if (!operators[filename]) { | ||
operators[filename] = true; | ||
fileResult += 'export interface operator_proto_' + filename + '<T> {\n ' + methods.join(';\n ') + ';\n}\n'; | ||
} | ||
} | ||
} | ||
|
||
var typingsContent = fs.readFileSync('./src/operator-typings.ts').toString(); | ||
fileResult = '/* ||| MARKER ||| */\n' + fileResult + '/* ||| MARKER ||| */'; | ||
typingsContent = typingsContent.replace(/(\/\* \|\|\| MARKER \|\|\| \*\/[\s|\S]*?\/\* \|\|\| MARKER \|\|\| \*\/)/, fileResult); | ||
fs.writeFileSync('./src/operator-typings.ts', typingsContent); | ||
|
||
|
||
function computeTypingsFor(s) { | ||
var captureRegex = /\/\*\-\-([\s|\S]*?)-\-\*\//g; | ||
var computeNumberRegex = /\*compute (\d.*?)?\*/; | ||
var tokenRegex = /\{.*?\}/g; | ||
|
||
s = s.replace(captureRegex, function(capture) { | ||
capture = capture.trim(); | ||
capture = capture.substr(3, capture.length - 3 * 2); | ||
var compute = computeNumberRegex.exec(capture); | ||
if (compute) { | ||
compute = compute[1] || '6'; | ||
} else { | ||
compute = '6'; | ||
} | ||
var range = compute.split('-'); | ||
if (range.length === 1) { | ||
var start = 1; | ||
var end = range[0]; | ||
} else { | ||
var start = range[0]; | ||
var end = range[1]; | ||
} | ||
|
||
capture = capture.replace(computeNumberRegex, '').trim(); | ||
|
||
var tokenResult; | ||
var results = []; | ||
for (var number = start; number <= end; number++) { | ||
var res = capture.replace(tokenRegex, function(capture, index, str) { | ||
var items = []; | ||
capture = capture.substr(1, capture.length - 2); | ||
for (var i = start; i <= number; i++) { | ||
var typeName = 'T' + (i === 1 ? '' : i); | ||
items.push(capture.replace(/\|X\|/g, typeName).replace(/\|v\|/g, 'v' + i)); | ||
} | ||
|
||
return items.join(', '); | ||
}); | ||
results.push(res); | ||
} | ||
|
||
return results.join('\n'); | ||
}); | ||
|
||
return s; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is going to introduce circular dependencies. If you rebase, you can run
npm run check_circular_dependencies
and see.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They'll get thrown away, because they're just used strictly for the types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you able to share how this will be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when it's transpiled to JS, it'll strip anything that's only used for type definitions (but retained in the .d.ts file)
=>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I'm feeling this specific changes (import types then assign static method with bring it via
typeof
) is orthogonal and can be separated - is my understanding correct? (at least just taking this changes locally into TOT seems working)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, those changes by themselves don't really depend on everything else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about separate this change into another PR? this change's quite straightforward and small, can be checked easily. Also it'll reduce this PR's scope.