pcan / reflec-ts Goto Github PK
View Code? Open in Web Editor NEWTypeScript compiler with Reflection capabilities
License: Other
TypeScript compiler with Reflection capabilities
License: Other
Hello, thank you for this project.
Now we can write somthng like this
interface UiButtonProps {
description: string;
}
for (let member of UiButtonProps.members) {
let memberDescription = `Member ${member.name} is ${member.type.kind}`;
if (member.type.kind === 'interface' || member.type.kind === 'class') {
memberDescription += ' - name: ' + (<NamedType>member.type).name;
}
console.log(memberDescription);
}
But i think it's not good;
Standard reflection framework just provide functions to reflect.
And we can just pass Class or Interface to function;
If your aim - jsut provide intellisence for usage reflector functions - we can create any type like Type in .Net reflection
interface Type {
members: Array<any>,
/...
}
// and then
import reflect from 'ts-reflect';
class MyTestClass{
}
reflect(MyTestClass).members.forEach( member=> console.log(member));
I know tha we can't pass interface to function cause it's typescript syntax limitation;
What do you think about it?
The AST enhancer should not consider .d.ts
files, since it's not possible to inject import statement, class decorators or registerClass
calls.
Implement type serialization for Union & Intersection field types.
See issue in examples: pcan/reflec-ts-examples#2. Currently, optional fields are not emitted in Reflection.js
Read target
and module
options in compilerOptions
when emitting Reflection.js. For example, there is no need to do environment checks like typeof module == 'object' ...
if we are targeting to a particular module system.
Implement dynamic class loading in classForName
. We want to achieve something like this
let clazz = Reflection.classForName("folder1.folder2.sourceFile#module1.MyClass");
let ctor = clazz.getConstructor();
let obj = new ctor();
Note that folder1/folder2/sourceFile.js
has not been loaded yet, so registerClass
has not been executed yet for MyClass
.
At the moment, we have to deal with:
classForName
has to return something like a promise, or take a callback function as argument.We would not want to make two different APIs for the two systems. One possible solution may be an async function
version of Reflection.classForName
, but this will force us to drop support for targets before ES2015 (due to a TypeScript compiler rule).
Suggestions are welcome.
Instead of building an object hierarchy for types in Reflection.js, use a flat structure like the following:
Reflection.$libs['default'] = {
'main' : {
'MainClass': _l[1],
},
'common.file1' : {
'MyClass': _l[2],
'module1.MyInterface': _l[3],
},
'common.file2' : {
'MyService': _l[0],
}
};
Reflection.js will contain invalid code if there is a parameterized type for some field.
The following code reproduces it:
interface TypedInterface<T> {
a: T;
}
interface Broken {
brokenField: TypedInterface<string>;
}
Expected output in Reflection.js:
_t.kind = 'reference';
_t.type = _l[3];
_t.typeParameters = [_type_string, ];
Actual output in Reflection.js:
_t.kind = 'reference';
_t.type = _l[3];
typeParameters: [_type_string, ],
Remove common runtime parts from Reflection.js and make a new npm library reflec-ts-runtime
that can be included as runtime dependency.
Add classForName
and interfaceForName
to the Reflection
namespace. Each of these function will accept one or two strings:
folder1.folder2.fileName#module1.module2.ClassOrInterface
folder1.folder2.fileName
and module.module2.ClassOrInterface
and it will return the Class
or Interface
object, respectively.
Create a RegisterClass
decorator that delegates to registerClass
function. When experimentalDecorators
is enabled, we can decorate classes instead of passing constructors to registerClass
.
Remove $reflection
variable and introduce constants for interfaces. Each module won't export $reflection
any more, so each class/interface will preserve its level of visibility.
In order to get class metadata, a new method getClass()
will be added to Function
.
Integrate current TypeScript test infrastructure (harness, compiler, fourslash) with reflec-ts in order to build reflection test cases.
Using reflec-ts 0.3.9 with Node 8.6.0. I have the following basic project structure:
src/tests.js
class testClassA {
a: string
}
console.log(new testClassA());
tsconfig.json
{
"compileOnSave": true,
"include": [
"src/*"
],
"compilerOptions": {
"target": "ES2017",
"noImplicitAny": true,
"removeComments": false,
"sourceMap": true,
"outDir": "build"
},
"reflectionEnabled": true
}
I'm running this command:
node d:\git\test-project\node_modules\reflec-ts\bin\tsc -p d:\git\test-project
I get the following output:
d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:53636
throw e;
^Error: Debug Failure. False expression: length must be non-negative, is NaN
at Object.assert (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:2834:23)
at Object.createFileDiagnostic (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:2004:15)
at createDiagnosticForNodeInSourceFile (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:7304:19)
at Object.createDiagnosticForNode (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:7299:16)
at error (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:22449:22)
at resolveName (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:22891:25)
at getResolvedSymbol (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:29693:67)
at checkIdentifier (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:30742:26)
at checkExpressionWorker (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:34344:28)
at checkExpression (d:\git\test-project\node_modules\reflec-ts\lib\tsc.js:34328:42)
The terminal process terminated with exit code: 1
If I change reflectionEnabled to false in tsconfig.json then the build succeeds. Not sure what I'm doing wrong! It's weird because I've successfully used reflec-ts before, but it was on a different PC.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.