Skip to content

Commit

Permalink
Merge pull request #8 from Microsoft/more
Browse files Browse the repository at this point in the history
More fixes
  • Loading branch information
Andy authored and sandersn committed Nov 29, 2021
1 parent 5ae256e commit 29a9097
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/dtslint/dtslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
// TODO: once https://github.com/palantir/tslint/pull/2229 is in, enable this
"only-arrow-functions": false,

"no-var-keyword": false,
"no-var": true,

"interface-name": [true, "never-prefix"],
"max-line-length": [true, 200],
"no-consecutive-blank-lines": true,
"object-literal-key-quotes": [true, "as-needed"],
"one-line": [
true,
"check-catch",
Expand Down
8 changes: 6 additions & 2 deletions packages/dtslint/src/rules/noPaddingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ function walk(ctx: Lint.WalkContext<void>): void {
case ts.SyntaxKind.OpenBracketToken:
case ts.SyntaxKind.OpenBraceToken:
if (blankLineInBetween(child.getEnd(), children[i + 1].getStart())) {
ctx.addFailureAtNode(child, "Don't leave a blank line after '{'");
fail("after");
}
break;

case ts.SyntaxKind.CloseParenToken:
case ts.SyntaxKind.CloseBracketToken:
case ts.SyntaxKind.CloseBraceToken:
if (blankLineInBetween(child.getStart() - 1, children[i - 1].getEnd() - 1)) {
ctx.addFailureAtNode(child, "Don't leave a blank line before '}'");
fail("before");
}
break;

default:
cb(child);
}

function fail(kind: "before" | "after"): void {
ctx.addFailureAtNode(child, `Don't leave a blank line ${kind} '${ts.tokenToString(child.kind)}'`);
}
}
});

Expand Down
51 changes: 51 additions & 0 deletions packages/dtslint/src/rules/noVarRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as Lint from "tslint";
import * as ts from "typescript";

// TODO: pull request to update tslint's `no-var-keyword`

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-var",
description: "Forbids 'var'.",
optionsDescription: "Not configurable.",
options: null,
type: "style",
typescriptOnly: false,
};

public static FAILURE_STRING = "Do not use 'var'.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}

function walk(ctx: Lint.WalkContext<void>): void {
const { sourceFile } = ctx;
ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node) {
switch (node.kind) {
case ts.SyntaxKind.VariableStatement:
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)
&& !Lint.isBlockScopedVariable(node as ts.VariableStatement)
// Global 'var' declaration OK.
&& !(sourceFile.isDeclarationFile && !ts.isExternalModule(ctx.sourceFile) && node.parent === sourceFile)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
}
break;

case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.ForInStatement:
case ts.SyntaxKind.ForOfStatement: {
const { initializer } = node as ts.ForStatement | ts.ForInStatement | ts.ForOfStatement;
if (initializer && initializer.kind === ts.SyntaxKind.VariableDeclarationList &&
// tslint:disable-next-line no-bitwise
!Lint.isNodeFlagSet(initializer, ts.NodeFlags.Let | ts.NodeFlags.Const)) {
ctx.addFailureAtNode(initializer, Rule.FAILURE_STRING);
}
break;
}
}

ts.forEachChild(node, cb);
});
}

0 comments on commit 29a9097

Please sign in to comment.