fix(typescript): workaround - don't wrap directives in parens, fixes #2074 (#2107)

master
Lucas Azzola 2017-06-12 12:10:56 +10:00 committed by Christopher Chedeau
parent e8f12cf621
commit 486a89bfdc
4 changed files with 25 additions and 9 deletions

View File

@ -130,7 +130,7 @@ FastPath.prototype.map = function map(callback /*, name1, name2, ... */) {
return result;
};
FastPath.prototype.needsParens = function() {
FastPath.prototype.needsParens = function(options) {
const parent = this.getParentNode();
if (!parent) {
return false;
@ -441,7 +441,11 @@ FastPath.prototype.needsParens = function() {
if (
typeof node.value === "string" &&
parent.type === "ExpressionStatement" &&
!parent.directive
// TypeScript workaround for eslint/typescript-eslint-parser#267
// See corresponding workaround in printer.js case: "Literal"
((options.parser !== "typescript" && !parent.directive) ||
(options.parser === "typescript" &&
options.originalText.substr(util.locStart(node) - 1, 1) === "("))
) {
// To avoid becoming a directive
const grandParent = this.getParentNode(1);

View File

@ -166,7 +166,7 @@ function genericPrint(path, options, printPath, args) {
} else {
// Nodes with decorators can't have parentheses, so we can avoid
// computing path.needsParens() except in this case.
needsParens = path.needsParens();
needsParens = path.needsParens(options);
}
if (node.type) {
@ -1137,7 +1137,7 @@ function genericPrintNoParens(path, options, print, args) {
return printNumber(n.extra.raw);
case "BooleanLiteral": // Babel 6 Literal split
case "StringLiteral": // Babel 6 Literal split
case "Literal":
case "Literal": {
if (n.regex) {
return printRegex(n.regex);
}
@ -1147,7 +1147,18 @@ function genericPrintNoParens(path, options, print, args) {
if (typeof n.value !== "string") {
return "" + n.value;
}
return nodeStr(n, options); // Babel 6
// TypeScript workaround for eslint/typescript-eslint-parser#267
// See corresponding workaround in fast-path.js needsParens()
const grandParent = path.getParentNode(1);
const isTypeScriptDirective =
options.parser === "typescript" &&
typeof n.value === "string" &&
grandParent &&
(grandParent.type === "Program" ||
grandParent.type === "BlockStatement");
return nodeStr(n, options, isTypeScriptDirective);
}
case "Directive":
return path.call(print, "value"); // Babel 6
case "DirectiveLiteral":
@ -4103,7 +4114,7 @@ function adjustClause(node, clause, forceSpace) {
return indent(concat([line, clause]));
}
function nodeStr(node, options, isFlowDirectiveLiteral) {
function nodeStr(node, options, isFlowOrTypeScriptDirectiveLiteral) {
const raw = node.extra ? node.extra.raw : node.raw;
// `rawContent` is the string exactly like it appeared in the input source
// code, with its enclosing quote.
@ -4117,7 +4128,8 @@ function nodeStr(node, options, isFlowDirectiveLiteral) {
let shouldUseAlternateQuote = false;
const isDirectiveLiteral =
isFlowDirectiveLiteral || node.type === "DirectiveLiteral";
isFlowOrTypeScriptDirectiveLiteral || node.type === "DirectiveLiteral";
let canChangeDirectiveQuotes = false;
// If `rawContent` contains at least one of the quote preferred for enclosing

View File

@ -1 +1 @@
run_spec(__dirname, null, ["babylon" /*, "typescript"*/]);
run_spec(__dirname, null, ["babylon", "typescript"]);

View File

@ -1 +1 @@
run_spec(__dirname, { parser: "babylon" }, ["flow"]);
run_spec(__dirname, { parser: "babylon" }, ["flow", "typescript"]);