Comments (3)
Technically the location object that really matters is newNode.original.loc
, so you might be able to do newNode.original = oldNode.original
. Then, if newNode.type === oldNode.type
, the reprinter should be able to reuse the source from oldNode
.
Those .original
properties get set in the copyAst
function:
https://github.com/benjamn/recast/blob/1ddd828daaed35a43999331acafd761bf5430edf/lib/parser.js#L125
The idea is that recast.parse
hands you back a copied AST where every node starts out with an .original
property referring to the AST node originally produced by Esprima, so as you modify the copied tree the new nodes you introduce won't have an .original
property, so they will be printed by the pretty-printer instead of getting reprinted using the original source.
from ast-types.
@benjamn your answer is now 4 years old and I don't think it's still working. Is there a way to define the node location with the newest ast-types
and recast
versions? Your help will be really appreciate.
I guess something like that should work but it will not :(
const node = builders.callExpression(builders.identifier('foo'), [])
node.loc = {
start: { line: 10, column: 0 },
end: { line: 10, column: 3 }
}
const {map} = recast.print(node)
from ast-types.
I have made also a live demo to show the issue https://runkit.com/gianlucaguarini/5c5b1271e4b0c90013575af7 @benjamn any help would be really appreciated!
I am using ast-types
and recast
in the new @riotjs/compiler
and this is the only issue am not able to solve since a couple of weeks
from ast-types.
Related Issues (20)
- Question: Any guide on how to move from the traversal API to the new APIs?
- Group statements without using BlockStatement (ie. no braces)?
- [Flow] Exception on indexed access types HOT 3
- 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
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.