ember-template-recast
Advanced tools
Comparing version 4.1.4 to 4.1.5
@@ -0,1 +1,14 @@ | ||
## v4.1.5 (2020-07-15) | ||
#### :bug: Bug Fix | ||
* [#303](https://github.com/ember-template-lint/ember-template-recast/pull/303) Preserve formatting when switching a template's body ([@dcyriller](https://github.com/dcyriller)) | ||
* [#302](https://github.com/ember-template-lint/ember-template-recast/pull/302) Improve whitespace preservation around dirty attributes / comments / modifiers ([@dcyriller](https://github.com/dcyriller)) | ||
#### :memo: Documentation | ||
* [#307](https://github.com/ember-template-lint/ember-template-recast/pull/307) Add an intro note to README ([@dcyriller](https://github.com/dcyriller)) | ||
#### Committers: 2 | ||
- Cyrille ([@dcyriller](https://github.com/dcyriller)) | ||
- [@dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | ||
## v4.1.4 (2020-04-27) | ||
@@ -2,0 +15,0 @@ |
{ | ||
"name": "ember-template-recast", | ||
"version": "4.1.4", | ||
"version": "4.1.5", | ||
"description": "Non-destructive template transformer.", | ||
@@ -31,11 +31,11 @@ "keywords": [ | ||
"dependencies": { | ||
"@glimmer/syntax": "^0.51.1", | ||
"@glimmer/syntax": "^0.54.1", | ||
"async-promise-queue": "^1.0.5", | ||
"colors": "^1.4.0", | ||
"commander": "^5.1.0", | ||
"globby": "^11.0.0", | ||
"globby": "^11.0.1", | ||
"ora": "^4.0.4", | ||
"slash": "^3.0.0", | ||
"tmp": "^0.1.0", | ||
"workerpool": "^5.0.4" | ||
"tmp": "^0.2.1", | ||
"workerpool": "^6.0.0" | ||
}, | ||
@@ -45,12 +45,12 @@ "devDependencies": { | ||
"common-tags": "^1.8.0", | ||
"eslint": "^6.8.0", | ||
"eslint": "^7.4.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"execa": "^4.0.0", | ||
"jest": "^25.4.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"execa": "^4.0.3", | ||
"jest": "^26.1.0", | ||
"lerna-changelog": "^1.0.1", | ||
"prettier": "^2.0.5", | ||
"release-it": "^13.5.5", | ||
"release-it-lerna-changelog": "^2.2.0" | ||
"release-it": "^13.6.5", | ||
"release-it-lerna-changelog": "^2.3.0" | ||
}, | ||
@@ -57,0 +57,0 @@ "engines": { |
# ember-template-recast | ||
With ember-template-recast, transform a template's AST and reprint it. Its | ||
formatting will be preserved. | ||
For instance, it is possible to change a component's property while preserving | ||
its formatting: | ||
```js | ||
const recast = require('ember-template-recast'); | ||
const template = ` | ||
<Sidebar | ||
foo="bar" | ||
item={{hmmm}} | ||
/> | ||
`; | ||
// parse | ||
let ast = recast.parse(template); | ||
// transform | ||
ast.body[1].attributes[1].value.path = builders.path('this.hmmm'); | ||
let ouput = recast.print(ast); | ||
output === ` | ||
<Sidebar | ||
foo="bar" | ||
item={{this.hmmm}} | ||
/> | ||
`; // is true! | ||
``` | ||
## APIs | ||
@@ -4,0 +37,0 @@ |
@@ -5,5 +5,6 @@ const { traverse, builders, Walker } = require('@glimmer/syntax'); | ||
const PARSE_RESULT_FOR = new WeakMap(); | ||
const NODES_INFO = new Map(); | ||
function parse(template) { | ||
let result = new ParseResult(template); | ||
let result = new ParseResult(template, NODES_INFO); | ||
@@ -10,0 +11,0 @@ PARSE_RESULT_FOR.set(result.ast, result); |
@@ -89,3 +89,3 @@ const { preprocess, print: _print, traverse } = require('@glimmer/syntax'); | ||
module.exports = class ParseResult { | ||
constructor(template) { | ||
constructor(template, nodesInfo) { | ||
let ast = preprocess(template, { | ||
@@ -101,3 +101,3 @@ mode: 'codemod', | ||
this.nodeInfo = new Map(); | ||
this.nodeInfo = nodesInfo; | ||
this.ancestor = new Map(); | ||
@@ -411,19 +411,22 @@ this.dirtyFields = new Map(); | ||
let joinOpenPartsWith = ' '; | ||
if (originalOpenParts.length > 1) { | ||
joinOpenPartsWith = this.sourceForLoc({ | ||
start: originalOpenParts[0].loc.end, | ||
end: originalOpenParts[1].loc.start, | ||
let openPartsSource = originalOpenParts.reduce((acc, part, index, parts) => { | ||
let partSource = this.sourceForLoc(part.loc); | ||
if (index === parts.length - 1) { | ||
return acc + partSource; | ||
} | ||
let joinPartWith = this.sourceForLoc({ | ||
start: parts[index].loc.end, | ||
end: parts[index + 1].loc.start, | ||
}); | ||
} | ||
if (joinOpenPartsWith.trim() !== '') { | ||
// if the autodetection above resulted in some non whitespace | ||
// values, reset to `' '` | ||
joinOpenPartsWith = ' '; | ||
} | ||
if (joinPartWith.trim() !== '') { | ||
// if the autodetection above resulted in some non whitespace | ||
// values, reset to `' '` | ||
joinPartWith = ' '; | ||
} | ||
let openPartsSource = originalOpenParts | ||
.map((part) => this.sourceForLoc(part.loc)) | ||
.join(joinOpenPartsWith); | ||
return acc + partSource + joinPartWith; | ||
}, ''); | ||
@@ -523,4 +526,23 @@ let postPartsWhitespace = ''; | ||
openPartsSource = openParts.map((part) => this.print(part)).join(joinOpenPartsWith); | ||
openPartsSource = openParts.reduce((acc, part, index, parts) => { | ||
let partSource = this.print(part); | ||
if (index === parts.length - 1) { | ||
return acc + partSource; | ||
} | ||
let joinPartWith = this.sourceForLoc({ | ||
start: parts[index].loc.end, | ||
end: parts[index + 1].loc.start, | ||
}); | ||
if (joinPartWith === '' || joinPartWith.trim() !== '') { | ||
// if the autodetection above resulted in some non whitespace | ||
// values, reset to `' '` | ||
joinPartWith = ' '; | ||
} | ||
return acc + partSource + joinPartWith; | ||
}, ''); | ||
if (originalOpenParts.length === 0) { | ||
@@ -527,0 +549,0 @@ postTagWhitespace = ' '; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70807
1305
152
+ Added@glimmer/interfaces@0.54.2(transitive)
+ Added@glimmer/syntax@0.54.2(transitive)
+ Added@glimmer/util@0.54.2(transitive)
+ Addedtmp@0.2.3(transitive)
+ Addedworkerpool@6.5.1(transitive)
- Removed@glimmer/interfaces@0.51.1(transitive)
- Removed@glimmer/syntax@0.51.1(transitive)
- Removed@glimmer/util@0.51.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedtmp@0.1.0(transitive)
- Removedworkerpool@5.0.4(transitive)
- Removedwrappy@1.0.2(transitive)
Updated@glimmer/syntax@^0.54.1
Updatedglobby@^11.0.1
Updatedtmp@^0.2.1
Updatedworkerpool@^6.0.0