ember-template-recast
Advanced tools
Comparing version 3.1.2 to 3.1.3
@@ -0,1 +1,10 @@ | ||
## v3.1.3 (2019-08-16) | ||
#### :bug: Bug Fix | ||
* [#92](https://github.com/ember-template-lint/ember-template-recast/pull/92) Fix mutating an element attribute with an initial valueless attribute. ([@rwjblue](https://github.com/rwjblue)) | ||
* [#91](https://github.com/ember-template-lint/ember-template-recast/pull/91) Ensure updating a void element does not print a closing tag. ([@rwjblue](https://github.com/rwjblue)) | ||
#### Committers: 1 | ||
- Robert Jackson ([@rwjblue](https://github.com/rwjblue)) | ||
## v3.1.2 (2019-08-15) | ||
@@ -2,0 +11,0 @@ |
{ | ||
"name": "ember-template-recast", | ||
"version": "3.1.2", | ||
"version": "3.1.3", | ||
"description": "Non-destructive template transformer.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
const { preprocess, print: _print } = require('@glimmer/syntax'); | ||
const { preprocess, print: _print, traverse } = require('@glimmer/syntax'); | ||
const { sortByLoc } = require('./utils'); | ||
@@ -6,6 +6,62 @@ | ||
const leadingWhitespace = /(^\s+)/; | ||
const trailingWhitespace = /(\s+)$/; | ||
const attrNodeParts = /(^[^=]+)(\s+)?(=)?(\s+)?(\S+)?/; | ||
const hashPairParts = /(^[^=]+)(\s+)?=(\s+)?(\S+)/; | ||
const voidTagNames = new Set([ | ||
'area', | ||
'base', | ||
'br', | ||
'col', | ||
'command', | ||
'embed', | ||
'hr', | ||
'img', | ||
'input', | ||
'keygen', | ||
'link', | ||
'meta', | ||
'param', | ||
'source', | ||
'track', | ||
'wbr', | ||
]); | ||
function getLines(source) { | ||
let result = source.match(reLines); | ||
return result.slice(0, -1); | ||
} | ||
/* | ||
This is needed to address issues in the glimmer-vm AST _before_ any of the nodes and node | ||
values are cached. The specific issues being worked around are: | ||
* https://github.com/glimmerjs/glimmer-vm/pull/953 | ||
* https://github.com/glimmerjs/glimmer-vm/pull/954 | ||
*/ | ||
function fixASTIssues(ast) { | ||
traverse(ast, { | ||
AttrNode(node) { | ||
// TODO: manually working around https://github.com/glimmerjs/glimmer-vm/pull/953 | ||
if (node.value.type === 'TextNode' && node.value.chars === '') { | ||
// \n is not valid within an attribute name (it would indicate two attributes) | ||
// always assume the attribute ends on the starting line | ||
node.loc.end.line = node.loc.start.line; | ||
node.loc.end.column = node.loc.start.column + node.name.length; | ||
} | ||
}, | ||
ConcatStatement(node) { | ||
// TODO: manually working around https://github.com/glimmerjs/glimmer-vm/pull/954 | ||
if ( | ||
node.parts[0].type === 'TextNode' && | ||
node.parts[0].loc.start.column > node.loc.start.column + 1 | ||
) { | ||
node.parts[0].loc.start.column = node.parts[0].loc.start.column - 1; | ||
} | ||
}, | ||
}); | ||
return ast; | ||
} | ||
module.exports = class ParseResult { | ||
@@ -20,3 +76,5 @@ constructor(template) { | ||
this.source = template.match(reLines); | ||
ast = fixASTIssues(ast); | ||
this.source = getLines(template); | ||
this._originalAst = ast; | ||
@@ -38,29 +96,2 @@ | ||
}; | ||
// TODO: this sucks, manually working around https://github.com/glimmerjs/glimmer-vm/pull/953 | ||
if (node.type === 'AttrNode' && node.value.type === 'TextNode' && node.value.chars === '') { | ||
let extraneousWhitespaceMatch = nodeInfo.source.match(trailingWhitespace); | ||
let lineOffset = 0; | ||
let columnOffset = 0; | ||
if (extraneousWhitespaceMatch) { | ||
let [, whitespace] = extraneousWhitespaceMatch; | ||
let whitespaceLines = whitespace.match(reLines); | ||
lineOffset = whitespaceLines.length - 1; | ||
columnOffset = whitespaceLines[whitespaceLines.length - 1].length; | ||
} | ||
nodeInfo.source = nodeInfo.source.trimRight(); | ||
node.loc.end.line = node.loc.end.line - lineOffset; | ||
node.loc.end.column = node.loc.end.column - columnOffset; | ||
} | ||
// TODO: this sucks, manually working around https://github.com/glimmerjs/glimmer-vm/pull/954 | ||
if ( | ||
node.type === 'ConcatStatement' && | ||
node.parts[0].type === 'TextNode' && | ||
node.parts[0].loc.start.column > node.loc.start.column + 1 | ||
) { | ||
node.parts[0].loc.start.column = node.parts[0].loc.start.column - 1; | ||
} | ||
this.nodeInfo.set(node, nodeInfo); | ||
@@ -149,3 +180,6 @@ | ||
currentLine++; | ||
line = this.source[currentLine]; | ||
// for templates that are completely empty the outer Template loc is line | ||
// 0, column 0 for both start and end defaulting to empty string prevents | ||
// more complicated logic below | ||
line = this.source[currentLine] || ''; | ||
@@ -378,2 +412,3 @@ if (currentLine === firstLine) { | ||
} | ||
let openPartsSource = originalOpenParts | ||
@@ -435,8 +470,13 @@ .map(part => this.sourceForLoc(part.loc)) | ||
let closeSource = selfClosing ? '' : `</${original.tag}>`; | ||
let closeSource = selfClosing | ||
? '' | ||
: voidTagNames.has(original.tag) | ||
? '' | ||
: `</${original.tag}>`; | ||
if (dirtyFields.has('tag')) { | ||
openSource = `<${ast.tag}`; | ||
closeSource = selfClosing ? '' : `</${ast.tag}>`; | ||
closeSource = selfClosing ? '' : voidTagNames.has(ast.tag) ? '' : `</${ast.tag}>`; | ||
dirtyFields.delete('tag'); | ||
@@ -443,0 +483,0 @@ } |
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
54913
1226