Comments (9)
@Naddiseo That's a great point. I've been thinking there would be a lot of value in allowing users of the library to instantiate their own copy of the type system, so that when (for example) Recast adds the custom File
type (see https://github.com/benjamn/recast/blob/master/lib/types.js), that doesn't interfere with any other users of the ast-types
library.
Something like this?
var types = require("ast-types").fork(/* pass true to start fresh? */);
types.registerDefs("ast-types/def/core.js");
types.registerDefs("./custom/defs");
types.registerDefs("./more/defs");
from ast-types.
That would work.
I was also thinking something like providing a language
or something.
If the files under /def
were moved to say /langs/ecma
Maybe something like:
var types = require('ast-types');
/*
languages.ECMA is defined as an array containing what is currently in `def`
*/
var langs = types.languages;
types.registerDefs(langs.ECMA)
This would allow ast-types to possible build up defs of other language to aid other parser builders (or a separate repository for language defs).
Some other things that need to be thought about
- I think the builder expects there to be at least
def("Node") / def("SourceLocation") / def("Position")
already defined. So they might need to be predefined and loaded. - It might be useful to expose
/lib/shared
from ast-types.
Another thing that I've found using the builders is that I want to be able to pass in the current location. I think one of the arguments in the builder function is a SourceLocation
it should be used as the current location.
from ast-types.
Actually, that fork()
idea is really good. My use case is for transpiling, so I want to use an ast tree/builder for the source language, then a separate for the target language (javascript in my case).
I may have a look tomorrow to see if there's an easy way to accomplish this. Do you have any ideas where to start?
from ast-types.
I still think this is a great idea, by the way. Hope to work on it for the next minor version (0.8.0).
from ast-types.
bump!
0.9.0
maybe?
Personally, I think the core components should be split out into another library (ast-types-base
for sake of argument).
I like the syntax, but I think you should pass actual modules (or a custom function) instead of strings. The problem with strings would be accurately resolving them (especially if you do split into a separate module):
var types = require('ast-types-base')(
require('ast-types/def/core'),
require('./myCustomDefs'),
function(types) {
types.def('MyType')
//...
}
);
I do like the fork
option discussed, and that seems appropriate for ast-types
, but not ast-types-base
as I'm proposing. Perhaps strings would make sense there for specifying some of the built in stuff.
This would be the equivalent, less verbose version of above:
require('ast-types').fork(
'core',
require('./myCustomDefs'),
function(types) {
types.def('MyType')
//...
}
);
One problem with the fork
method as currently being discussed would be with browserify
builds. Browserify will pick up and package everything in def/*
, even if you aren't using any of it and creating an entirely custom AST. Splitting into a separate module eliminates that problem for people implementing ground up AST definitions, and the top syntax above could still be used by those targeting browserify
to ensure only what they want from def/*
will be packaged in the bundle.
from ast-types.
@benjamn - anything I can do to move this forward?
from ast-types.
Should probably be closed since #145 was merged nearly 2.5 years (!!!) ago.
from ast-types.
Agreed, though I should mention the move to TypeScript (#300) has complicated this story, since the generated TypeScript declarations are fundamentally less modular/dynamic than the forking system.
from ast-types.
Related Issues (20)
- should all `namedTypes` `let`-exports be `PredicateType`s?
- 'visitObjectProperty' option of visit function did not work
- Removing TSPropertySignature node adds commas to the type declaration
- ASTPath<Function> is not assignable to parameter of type ASTNode
- ArrowFunctionExpression fails to check arrow functions without body and the type is also wrong HOT 3
- Decorator visitor can't work
- [Flow] did not recognize object of type "PropertyDefinition" HOT 3
- core.ts does not test whether BigInt exists HOT 4
- ast-types no longer published on npmjs.com? HOT 4
- npm install fails due to conflicting peer dependency
- `quote: 'single'` is broken in 0.15.2
- `b.functionExpression.from(functionDeclaration)` is failing on Windows HOT 1
- [Proposal] we should provide a function to build a fork with defs extracted from @babel/types HOT 4
- Jay-Jones-TOKEN_693697a3f04163a6e53f66fa9dbf0cd3e8a9d6face40e7e2483ff106fd22d9afeac596df74ae93d22dfcf4ee978cc6e2782983287695126f18c7ae1c64903e85e71e9fc814ac7d83bf74445e733a98db281d631362ddcc2c1a8228c379d98cbf447c28c9d8405c5ef7e68927f3bd926e722b5a7de234da85d3141bc7b185a1fe"/>true HOT 1
- No support for PrivateIdentifier? HOT 1
- node.extra for `RegExpLiteral` appears to produce the wrong value
- Can we add "TSSatisfiesExpression" to src/def/typescript.ts HOT 1
- ClassDeclaration type is missing decorators HOT 1
- Does `TSTypeParameter.name` need to be all of `IdentifierKind` types?
- Wrong structure of TSFunctionType node HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ast-types.