@fimbul/mimir
Advanced tools
Comparing version 0.10.0-dev.20180512 to 0.10.0-dev.20180515
{ | ||
"name": "@fimbul/mimir", | ||
"version": "0.10.0-dev.20180512", | ||
"version": "0.10.0-dev.20180515", | ||
"description": "Core rules of the Fimbullinter project", | ||
@@ -5,0 +5,0 @@ "main": "recommended.yaml", |
@@ -7,2 +7,3 @@ "use strict"; | ||
const tsutils_1 = require("tsutils"); | ||
const utils_1 = require("../utils"); | ||
let Rule = class Rule extends ymir_1.TypedRule { | ||
@@ -15,5 +16,9 @@ apply() { | ||
if (tsutils_1.isAwaitExpression(node)) { | ||
if (node.expression.pos === re.lastIndex && | ||
!this.maybePromiseLike(this.checker.getTypeAtLocation(node.expression), node.expression)) | ||
this.addFailure(match.index, node.end, "Unnecessary 'await' of a non-Promise value.", ymir_1.Replacement.delete(match.index, node.expression.getStart(this.sourceFile))); | ||
if (node.expression.pos !== re.lastIndex || | ||
this.maybePromiseLike(this.checker.getTypeAtLocation(node.expression), node.expression)) | ||
continue; | ||
const fix = [ymir_1.Replacement.delete(match.index, node.expression.getStart(this.sourceFile))]; | ||
if (utils_1.expressionNeedsParensWhenReplacingNode(node.expression, node)) | ||
fix.push(ymir_1.Replacement.append(match.index, '('), ymir_1.Replacement.append(node.expression.end, ')')); | ||
this.addFailure(match.index, node.end, "Unnecessary 'await' of a non-Promise value.", fix); | ||
} | ||
@@ -20,0 +25,0 @@ else if (node.kind === ts.SyntaxKind.AwaitKeyword && node.end === re.lastIndex) { |
@@ -7,2 +7,3 @@ "use strict"; | ||
const tsutils_1 = require("tsutils"); | ||
const utils_1 = require("../utils"); | ||
const FAIL_MESSAGE = 'Awaiting the returned value is redundant as it is wrapped in a Promise anyway.'; | ||
@@ -18,4 +19,4 @@ let Rule = class Rule extends ymir_1.AbstractRule { | ||
const replacements = [ymir_1.Replacement.delete(keywordStart, node.expression.getStart(this.sourceFile))]; | ||
if (tsutils_1.isArrowFunction(node.parent) && node.expression.kind === ts.SyntaxKind.ObjectLiteralExpression) | ||
replacements.push(ymir_1.Replacement.append(node.expression.getStart(this.sourceFile), '('), ymir_1.Replacement.append(node.expression.getEnd(), ')')); | ||
if (utils_1.expressionNeedsParensWhenReplacingNode(node.expression, node)) | ||
replacements.push(ymir_1.Replacement.append(keywordStart, '('), ymir_1.Replacement.append(node.expression.end, ')')); | ||
this.addFailure(keywordStart, node.expression.pos, FAIL_MESSAGE, replacements); | ||
@@ -22,0 +23,0 @@ } |
@@ -95,3 +95,6 @@ "use strict"; | ||
const start = node.getStart(this.sourceFile); | ||
this.addFailure(start, node.expression.pos, message, ymir_1.Replacement.delete(start, node.expression.getStart(this.sourceFile))); | ||
const fix = [ymir_1.Replacement.delete(start, node.expression.getStart(this.sourceFile))]; | ||
if (utils_1.expressionNeedsParensWhenReplacingNode(node.expression, node)) | ||
fix.push(ymir_1.Replacement.append(start, '('), ymir_1.Replacement.append(node.end, ')')); | ||
this.addFailure(start, node.expression.pos, message, fix); | ||
} | ||
@@ -98,0 +101,0 @@ } |
@@ -7,2 +7,3 @@ "use strict"; | ||
const tsutils_1 = require("tsutils"); | ||
const utils_1 = require("../utils"); | ||
let Rule = class Rule extends ymir_1.TypedRule { | ||
@@ -61,3 +62,3 @@ apply() { | ||
const args = node.arguments; | ||
const objectNeedsParens = node.parent.kind === ts.SyntaxKind.ArrowFunction || node.parent.kind === ts.SyntaxKind.ExpressionStatement; | ||
const objectNeedsParens = utils_1.objectLiteralNeedsParens(node); | ||
const fix = [ | ||
@@ -64,0 +65,0 @@ ymir_1.Replacement.replace(node.getStart(sourceFile), args[0].getStart(sourceFile), `${objectNeedsParens ? '(' : ''}{`), |
@@ -12,1 +12,3 @@ import * as ts from 'typescript'; | ||
export declare function childStatements(node: ts.Statement): IterableIterator<ts.Statement>; | ||
export declare function expressionNeedsParensWhenReplacingNode(expr: ts.Expression, replaced: ts.Expression): boolean; | ||
export declare function objectLiteralNeedsParens(replaced: ts.Expression): boolean; |
@@ -79,2 +79,56 @@ "use strict"; | ||
exports.childStatements = childStatements; | ||
function getLeadingExpressionWithPossibleParsingAmbiguity(expr) { | ||
switch (expr.kind) { | ||
case ts.SyntaxKind.PropertyAccessExpression: | ||
case ts.SyntaxKind.ElementAccessExpression: | ||
case ts.SyntaxKind.AsExpression: | ||
case ts.SyntaxKind.CallExpression: | ||
case ts.SyntaxKind.NonNullExpression: | ||
return expr.expression; | ||
case ts.SyntaxKind.PostfixUnaryExpression: | ||
return expr.operand; | ||
case ts.SyntaxKind.BinaryExpression: | ||
return expr.left; | ||
case ts.SyntaxKind.ConditionalExpression: | ||
return expr.condition; | ||
default: | ||
return; | ||
} | ||
} | ||
function expressionNeedsParensWhenReplacingNode(expr, replaced) { | ||
while (true) { | ||
switch (expr.kind) { | ||
case ts.SyntaxKind.ObjectLiteralExpression: | ||
case ts.SyntaxKind.FunctionExpression: | ||
case ts.SyntaxKind.ClassExpression: | ||
return parentRequiresParensForNode(expr.kind, replaced); | ||
default: | ||
const current = getLeadingExpressionWithPossibleParsingAmbiguity(expr); | ||
if (current === undefined) | ||
return false; | ||
expr = current; | ||
} | ||
} | ||
} | ||
exports.expressionNeedsParensWhenReplacingNode = expressionNeedsParensWhenReplacingNode; | ||
function parentRequiresParensForNode(kind, replaced) { | ||
while (true) { | ||
const parent = replaced.parent; | ||
switch (parent.kind) { | ||
case ts.SyntaxKind.ArrowFunction: | ||
return kind === ts.SyntaxKind.ObjectLiteralExpression; | ||
case ts.SyntaxKind.ExpressionStatement: | ||
return true; | ||
case ts.SyntaxKind.ExportAssignment: | ||
return !parent.isExportEquals && kind !== ts.SyntaxKind.ObjectLiteralExpression; | ||
} | ||
if (getLeadingExpressionWithPossibleParsingAmbiguity(parent) !== replaced) | ||
return false; | ||
replaced = parent; | ||
} | ||
} | ||
function objectLiteralNeedsParens(replaced) { | ||
return parentRequiresParensForNode(ts.SyntaxKind.ObjectLiteralExpression, replaced); | ||
} | ||
exports.objectLiteralNeedsParens = objectLiteralNeedsParens; | ||
//# sourceMappingURL=utils.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
333627
3554