Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return ts.forEachChild(sourceFile, function cb(node) {
switch (node.kind) {
case ts.SyntaxKind.FunctionDeclaration:
if (allowDeclarations) {
break;
}
// falls through
case ts.SyntaxKind.FunctionExpression: {
var f = node;
if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
ctx.addFailureAtNode(utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile), Rule.FAILURE_STRING);
}
}
}
return ts.forEachChild(node, cb);
});
}
function checkEqualsGreaterThanTokenInNode(node) {
if (!options.operator) {
return;
}
var equalsGreaterThanToken = utils.getChildOfKind(node, ts.SyntaxKind.EqualsGreaterThanToken, sourceFile);
// condition so we don't crash if the arrow is somehow missing
if (equalsGreaterThanToken === undefined) {
return;
}
checkForTrailingWhitespace(equalsGreaterThanToken.getFullStart());
checkForTrailingWhitespace(equalsGreaterThanToken.getEnd());
}
function checkForTrailingWhitespace(position, whiteSpacePos) {
function createFix(
arrowFunction: ts.FunctionLikeDeclaration,
body: ts.Block,
expr: ts.Expression,
text: string,
): Lint.Fix | undefined {
const statement = expr.parent;
const returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword)!;
const arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken)!;
const openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken)!;
const closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken)!;
const semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
const anyComments =
hasComments(arrow) ||
hasComments(openBrace) ||
hasComments(statement) ||
hasComments(returnKeyword) ||
hasComments(expr) ||
(semicolon !== undefined && hasComments(semicolon)) ||
hasComments(closeBrace);
return anyComments
? undefined
: [
// Object literal must be wrapped in `()`
return ts.forEachChild(sourceFile, function cb(node): void {
switch (node.kind) {
case ts.SyntaxKind.FunctionDeclaration:
if (allowDeclarations) {
break;
}
// falls through
case ts.SyntaxKind.FunctionExpression: {
const f = node as ts.FunctionLikeDeclaration;
if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
ctx.addFailureAtNode(
utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile)!,
Rule.FAILURE_STRING,
);
}
}
}
return ts.forEachChild(node, cb);
});
}
function createFix(
arrowFunction: ts.FunctionLikeDeclaration,
body: ts.Block,
expr: ts.Expression,
text: string,
): Lint.Fix | undefined {
const statement = expr.parent;
const returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword)!;
const arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken)!;
const openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken)!;
const closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken)!;
const semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
const anyComments =
hasComments(arrow) ||
hasComments(openBrace) ||
hasComments(statement) ||
hasComments(returnKeyword) ||
hasComments(expr) ||
(semicolon !== undefined && hasComments(semicolon)) ||
hasComments(closeBrace);
return anyComments
? undefined
: [
// Object literal must be wrapped in `()`
...(expr.kind === ts.SyntaxKind.ObjectLiteralExpression
? [
function createFix(arrowFunction, body, expr, text) {
var statement = expr.parent;
var returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword);
var arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken);
var openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken);
var closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken);
var semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
var anyComments = hasComments(arrow) ||
hasComments(openBrace) ||
hasComments(statement) ||
hasComments(returnKeyword) ||
hasComments(expr) ||
(semicolon !== undefined && hasComments(semicolon)) ||
hasComments(closeBrace);
return anyComments
? undefined
: (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
? [
Lint.Replacement.appendText(expr.getStart(), "("),
function createFix(arrowFunction, body, expr, text) {
var statement = expr.parent;
var returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword);
var arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken);
var openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken);
var closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken);
var semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
var anyComments = hasComments(arrow) ||
hasComments(openBrace) ||
hasComments(statement) ||
hasComments(returnKeyword) ||
hasComments(expr) ||
(semicolon !== undefined && hasComments(semicolon)) ||
hasComments(closeBrace);
return anyComments
? undefined
: (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
? [
Lint.Replacement.appendText(expr.getStart(), "("),
Lint.Replacement.appendText(expr.getEnd(), ")"),
]
private shouldIgnoreVariableDeclaration(node: ts.Node): boolean {
const ignoreFunctions: boolean =
this.options[OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION] === true;
return (
ignoreFunctions &&
(utils.getChildOfKind(node, ts.SyntaxKind.ArrowFunction) !== undefined ||
utils.getChildOfKind(node, ts.SyntaxKind.FunctionExpression) !== undefined)
);
}
public visitForStatement(node: ts.ForStatement) {
if (node.initializer === undefined && node.incrementor === undefined) {
const sourceFile = this.getSourceFile();
const start = node.getStart(sourceFile);
const closeParenEnd = utils.getChildOfKind(node, ts.SyntaxKind.CloseParenToken, sourceFile)!.getEnd();
const width = closeParenEnd - start;
let fix: Lint.Fix;
if (node.condition === undefined) {
fix = this.createFix(new Lint.Replacement(start, width, 'while (true)'));
} else {
const conditionEnd = node.condition.getEnd();
fix = this.createFix(
new Lint.Replacement(start,
node.condition.getStart(sourceFile) - start,
'while ('),
new Lint.Replacement(conditionEnd,
closeParenEnd - conditionEnd - 1,
''),
);
}
if (node.members.length === 0) {
if (!this.options.allowEmptyClass) {
this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword), Rule.FAILURE_EMPTY_CLASS);
}
return;
}
var allMembersAreConstructors = node.members.every(tsutils_1.isConstructorDeclaration);
if (allMembersAreConstructors &&
!this.options.allowConstructorOnly &&
!node.members.some(isConstructorWithShorthandProps)) {
this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_CONSTRUCTOR_ONLY);
}
if (!allMembersAreConstructors &&
!this.options.allowStaticOnly &&
!node.members.some(isNonStaticMember)) {
this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_STATIC_ONLY);
}
};
return NoUnnecessaryClassWalker;