Comparing version 1.9.0 to 1.9.1
{ | ||
"name": "esnext", | ||
"version": "1.9.0", | ||
"version": "1.9.1", | ||
"description": "Update your project to the latest ECMAScript syntax.", | ||
@@ -5,0 +5,0 @@ "main": "dist/esnext.umd.js", |
@@ -55,5 +55,12 @@ import MagicString from 'magic-string'; | ||
tokensForNode(node: Object): Array<Token> { | ||
return this.tokensInRange(...node.range); | ||
} | ||
tokensBetweenNodes(left: Object, right: Object): Array<Token> { | ||
return this.tokensInRange(left.range[1], right.range[0]); | ||
} | ||
tokensInRange(start: number, end: number): Array<Token> { | ||
const result = []; | ||
const tokens = this.tokens; | ||
const [ start, end ] = node.range; | ||
for (let i = 0; i < tokens.length; i++) { | ||
@@ -60,0 +67,0 @@ const { range } = tokens[i]; |
@@ -38,3 +38,22 @@ import BaseContext from '../context'; | ||
this.remove(node.key.range[1], node.value.range[1]); | ||
const [ keyToken, colonToken, valueToken ] = this.module.tokensForNode(node); | ||
const sourceBetweenKeyAndColon = this.slice(keyToken.range[1], colonToken.range[0]); | ||
const sourceBetweenColonAndValue = this.slice(colonToken.range[1], valueToken.range[0]); | ||
// `a /* 1 */ : /* 2 */ a` -> `/* 1 *//* 2 */a` | ||
// ^^^^^^^^^^^ ^^^^^^^ | ||
this.overwrite( | ||
keyToken.range[0], | ||
colonToken.range[1], | ||
sourceBetweenKeyAndColon.trim() | ||
); | ||
// `a /* 1 */ : /* 2 */ a` -> `/* 1 *//* 2 */a` | ||
// ^^^^^^^^^ ^^^^^^^ | ||
this.overwrite( | ||
colonToken.range[1], | ||
valueToken.range[0], | ||
sourceBetweenColonAndValue.trim() | ||
); | ||
this.metadata.properties.push(clone(node)); | ||
@@ -41,0 +60,0 @@ node.shorthand = true; |
import BaseContext from '../context'; | ||
import clone from '../utils/clone'; | ||
import estraverse from 'estraverse'; // TODO: import { traverse } from 'estraverse'; | ||
import groupContentBetweenElements from '../utils/groupContentBetweenElements'; | ||
import replace from '../utils/replace'; | ||
@@ -47,6 +48,30 @@ import type Module from '../module'; | ||
combine(node: Object, parts: Array<Object>): Object { | ||
if (parts.every(isString)) { | ||
const annotatedParts = parts.map((part, i) => { | ||
const previousPart = parts[i - 1]; | ||
const nextPart = parts[i + 1]; | ||
const annotatedPart = { | ||
node: part, | ||
prefix: '', | ||
suffix: '' | ||
}; | ||
if (previousPart) { | ||
const [ , prefix ] = this.insignificantContentSeparatedByPlus(previousPart, part); | ||
annotatedPart.prefix = prefix.replace(/^\s*/, ''); | ||
} | ||
if (nextPart) { | ||
const [ suffix ] = this.insignificantContentSeparatedByPlus(part, nextPart); | ||
annotatedPart.suffix = suffix.replace(/\s*$/, ''); | ||
} | ||
return annotatedPart; | ||
}); | ||
console.log(annotatedParts); | ||
if (annotatedParts.every(part => isString(part.node) && !part.prefix && !part.suffix)) { | ||
return this.combineStrings(parts); | ||
} else { | ||
return this.buildTemplateString(node, parts); | ||
return this.buildTemplateString(node, annotatedParts); | ||
} | ||
@@ -84,16 +109,13 @@ } | ||
const firstPart = parts[0]; | ||
let cooked = firstPart.value; | ||
let raw = firstPart.raw.slice(1, -1); | ||
this.overwrite(firstPart.range[0], firstPart.range[0] + 1, '`'); | ||
const firstNode = firstPart.node; | ||
let cooked = ''; | ||
let raw = ''; | ||
this.insert(firstNode.range[0], '`'); | ||
parts.forEach(part => this.escape('`', part.range[0] + 1, part.range[1] - 1)); | ||
for (let i = 0; i < parts.length - 1; i++) { | ||
const thisPart = parts[i]; | ||
const nextPart = parts[i + 1]; | ||
if (isString(nextPart)) { | ||
cooked += nextPart.value; | ||
raw += nextPart.raw.slice(1, -1); | ||
} else { | ||
expressions.push(nextPart); | ||
parts.forEach(({ node, prefix, suffix }, i) => { | ||
if (prefix || suffix || !isString(node)) { | ||
// This one has to be an interpolated expression. | ||
this.insert(node.range[0], `\${${prefix}`); | ||
this.insert(node.range[1], `${suffix}}`); | ||
expressions.push(node); | ||
quasis.push({ | ||
@@ -106,18 +128,17 @@ type: Syntax.TemplateElement, | ||
raw = ''; | ||
} | ||
if (isString(thisPart)) { | ||
if (isString(nextPart)) { | ||
this.remove(thisPart.range[1] - 1, nextPart.range[0] + 1); | ||
} else { | ||
this.overwrite(thisPart.range[1] - 1, nextPart.range[0], '${'); | ||
} | ||
} else { | ||
if (isString(nextPart)) { | ||
this.overwrite(thisPart.range[1], nextPart.range[0] + 1, '}'); | ||
} else { | ||
this.overwrite(thisPart.range[1], nextPart.range[0], '}${'); | ||
} | ||
// This one can become a quasi, | ||
cooked += node.value; | ||
raw += node.raw.slice(1, -1); | ||
this.remove(node.range[0], node.range[0] + 1); | ||
this.remove(node.range[1] - 1, node.range[1]); | ||
this.escape('`', ...node.range); | ||
} | ||
} | ||
const nextPart = parts[i + 1]; | ||
if (nextPart) { | ||
this.remove(node.range[1], nextPart.node.range[0]); | ||
} | ||
}); | ||
quasis.push({ | ||
@@ -129,11 +150,83 @@ type: Syntax.TemplateElement, | ||
const lastPart = parts[parts.length - 1]; | ||
if (isString(lastPart)) { | ||
this.overwrite(lastPart.range[1] - 1, node.range[1], '`'); | ||
} else { | ||
this.overwrite(lastPart.range[1], node.range[1], '}`'); | ||
} | ||
this.overwrite( | ||
parts[parts.length - 1].node.range[1], | ||
node.range[1], | ||
'`' | ||
); | ||
// TODO: test nested template strings | ||
//parts.forEach(part => this.escape('`', part.node.range[0] + 1, part.node.range[1] - 1)); | ||
// | ||
//for (let i = 0; i < parts.length - 1; i++) { | ||
// const thisPart = parts[i]; | ||
// const thisNode = thisPart.node; | ||
// const nextPart = parts[i + 1]; | ||
// const nextNode = nextPart.node; | ||
// if (isString(nextNode)) { | ||
// cooked += nextNode.value; | ||
// raw += nextNode.raw.slice(1, -1); | ||
// } else { | ||
// expressions.push(nextNode); | ||
// quasis.push({ | ||
// type: Syntax.TemplateElement, | ||
// tail: false, | ||
// value: { cooked, raw: raw.replace(/`/g, '\\`') } | ||
// }); | ||
// cooked = ''; | ||
// raw = ''; | ||
// } | ||
// if (isString(thisNode)) { | ||
// if (isString(nextNode)) { | ||
// this.remove( | ||
// thisNode.range[1] - 1, | ||
// nextNode.range[0] + 1 | ||
// ); | ||
// } else { | ||
// this.overwrite( | ||
// thisNode.range[1] - 1, | ||
// nextNode.range[0], | ||
// `\${` | ||
// ); | ||
// } | ||
// } else { | ||
// if (isString(nextNode)) { | ||
// this.overwrite( | ||
// thisNode.range[1], | ||
// nextNode.range[0] + 1, | ||
// `}` | ||
// ); | ||
// } else { | ||
// this.overwrite( | ||
// thisNode.range[1], | ||
// nextNode.range[0], | ||
// `}\${` | ||
// ); | ||
// } | ||
// } | ||
//} | ||
// | ||
//quasis.push({ | ||
// type: Syntax.TemplateElement, | ||
// tail: true, | ||
// value: { cooked, raw } | ||
//}); | ||
// | ||
//const lastPart = parts[parts.length - 1]; | ||
//const lastNode = lastPart.node; | ||
//if (isString(lastNode)) { | ||
// this.overwrite(lastNode.range[1] - 1, node.range[1], '`'); | ||
//} else { | ||
// this.overwrite(lastNode.range[1], node.range[1], '}`'); | ||
//} | ||
return { type: Syntax.TemplateLiteral, expressions, quasis }; | ||
} | ||
insignificantContentSeparatedByPlus(left: Object, right: Object): Array<string> { | ||
return groupContentBetweenElements( | ||
[left, ...this.module.tokensBetweenNodes(left, right), right], | ||
token => token.type === 'Punctuator' && token.value === '+', | ||
(left, right) => this.slice(left.range[1], right.range[0]) | ||
).map(strings => strings.join('')); | ||
} | ||
} | ||
@@ -140,0 +233,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
202389
24
6053