Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
upgrades to tslint@4 and corresponding plugins/rule definitions/tscon…
Browse files Browse the repository at this point in the history
…fig settings, upgrades to typescript@2.1 and registers new options, closes #1, #2, #3, #4
  • Loading branch information
Dimitri Benin committed Dec 14, 2016
1 parent b39d7fe commit b6fbefe
Show file tree
Hide file tree
Showing 7 changed files with 1,421 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ node_modules
.idea
*.iml

# built atrifacts
# built artifacts
dist
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Custom rules for tslint",
"main": "tslint.json",
"scripts": {
"build": "tsc -p tsconfig.json --outDir ./dist",
"build": "tsc --outDir ./dist",
"build:clean": "npm run clean && npm run build",
"ci": "npm run cover && npm run coveralls && npm run lint",
"clean": "rimraf dist coverage",
Expand All @@ -13,6 +13,7 @@
"cover": "istanbul cover --root ./dist --dir ./coverage runTests.js -- -b ./dist -c ./coverage && istanbul report --dir ./coverage",
"dist": "npm run clean && npm test && npm run lint && rimraf dist/tests",
"lint": "tslint 'src/**/*.ts'",
"lint:fix": "npm run lint -- --fix",
"prepublish": "npm run dist",
"pretest": "npm run build",
"test": "node runTests.js -b ./dist"
Expand All @@ -32,7 +33,8 @@
},
"homepage": "https://github.com/BendingBender/tslint-rules#readme",
"dependencies": {
"lodash": "^4.16.4"
"lodash": "^4.16.4",
"tslib": "^1.2.0"
},
"devDependencies": {
"@types/lodash": "^4.14.37",
Expand All @@ -41,9 +43,9 @@
"glob": "^7.1.1",
"istanbul": "^0.4.5",
"rimraf": "^2.5.4",
"tslint": "^3.15.1",
"tslint-microsoft-contrib": "^2.0.12",
"typescript": "^2.0.3",
"tslint": "^4.0.2",
"tslint-microsoft-contrib": "^4.0.0",
"typescript": "~2.1.4",
"yargs": "^6.0.0"
},
"engines": {
Expand Down
35 changes: 19 additions & 16 deletions src/importBarrelsRule.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { SourceFile, ImportDeclaration, Expression, SyntaxKind, StringLiteral } from 'typescript';
import * as Lint from 'tslint/lib/lint';
import * as path from 'path';
import * as fs from 'fs';
import * as path from 'path';
import { IRuleMetadata, RuleFailure, Rules, RuleWalker, Utils } from 'tslint/lib';
import { Expression, ImportDeclaration, SourceFile, StringLiteral, SyntaxKind } from 'typescript';

export class Rule extends Lint.Rules.AbstractRule {
public static metadata:Lint.IRuleMetadata = {
export class Rule extends Rules.AbstractRule {
public static metadata:IRuleMetadata = {
ruleName: 'import-barrels',
description: Lint.Utils.dedent`
description: Utils.dedent`
Enforces usage of barrels (\`index.ts\`) when importing from a directory that has a barrel file.`,
rationale: Lint.Utils.dedent`
rationale: Utils.dedent`
Allows directories that contain multiple modules to be handled as a single module with a single public interface
and opaque inner structure.
This rule works only for ES2015 module syntax \`import\` statements and checks only **relative** module paths.`,
optionsDescription: Lint.Utils.dedent`
optionsDescription: Utils.dedent`
An argument object may be optionally provided, with the following properties:
* \`noExplicitBarrels = false\`: disallows usage of explicitly named barrels in import statements (\`import foo from './foo/index'\`)
Expand All @@ -40,17 +40,18 @@ export class Rule extends Lint.Rules.AbstractRule {
maxLength: 1,
},
type: 'maintainability',
typescriptOnly: false,
};

public static USE_BARREL_FAILURE_STRING = 'Use barrel (index) files for imports if they are available for path: ';
public static NO_EXPLICIT_BARRELS_FAILURE_STRING = "Don't import barrel files by name, import containing directory instead for path: ";

public apply(sourceFile:SourceFile):Lint.RuleFailure[] {
public apply(sourceFile:SourceFile):RuleFailure[] {
return this.applyWithWalker(new ImportBarrelsWalker(sourceFile, this.getOptions()));
}
}

class ImportBarrelsWalker extends Lint.RuleWalker {
class ImportBarrelsWalker extends RuleWalker {
private statCache = new Map<string, fs.Stats>();

protected visitImportDeclaration(node:ImportDeclaration) {
Expand All @@ -64,7 +65,7 @@ class ImportBarrelsWalker extends Lint.RuleWalker {
super.visitImportDeclaration(node);
}

private getModuleExpressionErrorMessage(expression:Expression):string {
private getModuleExpressionErrorMessage(expression:Expression):string|null {
if (expression.kind !== SyntaxKind.StringLiteral) {
return null;
}
Expand Down Expand Up @@ -104,7 +105,7 @@ class ImportBarrelsWalker extends Lint.RuleWalker {
.some(file => this.isFile(file)) ? Rule.USE_BARREL_FAILURE_STRING + expression.getText() : null;
}

private getModuleStats(modulePath:string):fs.Stats {
private getModuleStats(modulePath:string):fs.Stats|null {
let stats = this.cachedStatSync(modulePath);

if (!stats) {
Expand Down Expand Up @@ -141,17 +142,19 @@ class ImportBarrelsWalker extends Lint.RuleWalker {
return stats != null && stats.isFile();
}

private cachedStatSync(path:string):fs.Stats {
private cachedStatSync(path:string):fs.Stats|null {
if (this.statCache.has(path)) {
return this.statCache.get(path);
return <fs.Stats>this.statCache.get(path);
}

const stat = this.statSync(path);
this.statCache.set(path, stat);
if (stat !== null) {
this.statCache.set(path, stat);
}
return stat;
}

private statSync(path:string):fs.Stats {
private statSync(path:string):fs.Stats|null {
try {
return fs.statSync(path);
} catch (e) {
Expand Down
25 changes: 13 additions & 12 deletions src/jasmineNoLambdaExpressionCallbacksRule.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { SourceFile, CallExpression, SyntaxKind, Identifier, ArrowFunction, Expression } from 'typescript';
import * as Lint from 'tslint/lib/lint';
import * as includes from 'lodash/includes';
import * as find from 'lodash/find';
import { IRuleMetadata, RuleFailure, Rules, RuleWalker, Utils } from 'tslint/lib';
import { ArrowFunction, CallExpression, Expression, Identifier, SourceFile, SyntaxKind } from 'typescript';
import includes = require('lodash/includes');
import find = require('lodash/find');

export class Rule extends Lint.Rules.AbstractRule {
public static metadata:Lint.IRuleMetadata = {
export class Rule extends Rules.AbstractRule {
public static metadata:IRuleMetadata = {
ruleName: 'jasmine-no-lambda-expression-callbacks',
description: Lint.Utils.dedent`
description: Utils.dedent`
Disallows usage of ES6-style lambda expressions as callbacks to Jasmine BDD functions.`,
rationale: Lint.Utils.dedent`
rationale: Utils.dedent`
Lambda expressions don't create lexical \`this\` bindings in order for \`this\` bindings from outer function scopes to be
visible inside of lambda expressions. This beats Jasmine's own system of managing shared state by passing in a dictionary object
as \`this\` reference to the user-provided callbacks to take over the memory management from the JavaScript VM to prevent memory
Expand All @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: ['true'],
options: null,
type: 'maintainability',
typescriptOnly: false,
};

public static FAILURE_STRING = "Don't use lambda expressions as callbacks to jasmine functions";
Expand All @@ -47,12 +48,12 @@ export class Rule extends Lint.Rules.AbstractRule {
'afterAll',
];

public apply(sourceFile:SourceFile):Lint.RuleFailure[] {
public apply(sourceFile:SourceFile):RuleFailure[] {
return this.applyWithWalker(new JasmineNoLambdaExpressionCallbacksWalker(sourceFile, this.getOptions()));
}
}

class JasmineNoLambdaExpressionCallbacksWalker extends Lint.RuleWalker {
class JasmineNoLambdaExpressionCallbacksWalker extends RuleWalker {
protected visitCallExpression(node:CallExpression) {
const invalidLambdaExpression = this.getInvalidLambdaExpression(node);

Expand All @@ -63,7 +64,7 @@ class JasmineNoLambdaExpressionCallbacksWalker extends Lint.RuleWalker {
super.visitCallExpression(node);
}

private getInvalidLambdaExpression(node:CallExpression):ArrowFunction {
private getInvalidLambdaExpression(node:CallExpression):ArrowFunction|null {
if (node.expression.kind !== SyntaxKind.Identifier) {
return null;
}
Expand All @@ -80,7 +81,7 @@ class JasmineNoLambdaExpressionCallbacksWalker extends Lint.RuleWalker {
return null;
}

private getLambdaExpressionFromArg(apiArg:Expression):ArrowFunction {
private getLambdaExpressionFromArg(apiArg:Expression):ArrowFunction|null {
if (apiArg.kind === SyntaxKind.ArrowFunction) {
return <ArrowFunction>apiArg;
} else if (apiArg.kind === SyntaxKind.CallExpression) {
Expand Down
40 changes: 24 additions & 16 deletions src/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"no-any": false,
"no-inferrable-types": true,
"no-internal-module": true,
"no-namespace": false,
"no-namespace": true,
"no-reference": true,
"no-var-requires": true,
"only-arrow-functions": false,
"prefer-for-of": true,
"typedef": false,
"typedef-whitespace": [
true,
Expand All @@ -40,7 +41,6 @@
"curly": true,
"forin": true,
"label-position": true,
"label-undefined": true,
"no-arg": true,
"no-bitwise": true,
"no-conditional-assignment": true,
Expand All @@ -54,20 +54,22 @@
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
// no-for-in-array requires type info
"no-for-in-array": false,
"no-invalid-this": [
true,
"check-function-in-method"
],
"no-null-keyword": false,
"no-shadowed-variable": true,
"no-shadowed-variable": false,
"no-string-literal": false,
"no-switch-case-fall-through": true,
"no-unreachable": true,
"no-unsafe-finally": true,
"no-unused-expression": true,
"no-unused-new": true,
"no-unused-variable": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"radix": true,
Expand All @@ -78,9 +80,9 @@
"allow-null-check"
],
"use-isnan": true,
"use-strict": [
"cyclomatic-complexity": [
true,
"check-module"
20
],
"eofline": true,
"indent": [
Expand All @@ -104,20 +106,19 @@
"no-require-imports": false,
"no-trailing-whitespace": true,
"object-literal-sort-keys": false,
"trailing-comma": [
"trailing-comma": true,
"align": false,
"array-type": [
true,
{
"multiline": "always",
"singleline": "never"
}
"array"
],
"align": false,
"arrow-parens": false,
"class-name": true,
"comment-format": [
true,
"check-space"
],
"completed-docs": false,
"file-header": false,
"interface-name": [
true,
Expand All @@ -127,7 +128,7 @@
"new-parens": true,
"no-angle-bracket-type-assertion": false,
"no-consecutive-blank-lines": false,
"no-constructor-vars": false,
"no-parameter-properties": false,
"object-literal-key-quotes": [
true,
"as-needed"
Expand All @@ -142,7 +143,13 @@
"check-whitespace"
],
"one-variable-per-declaration": false,
"ordered-imports": false,
"ordered-imports": [
true,
{
"import-sources-order": "case-insensitive",
"named-imports-order": "case-insensitive"
}
],
"quotemark": [
true,
"single",
Expand All @@ -166,6 +173,7 @@
"check-separator",
"check-module"
],
// tslint-microsoft-contrib
"chai-prefer-contains-to-index-of": false,
"chai-vague-errors": false,
"export-name": false,
Expand Down
34 changes: 22 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": false,
"alwaysStrict": false,
"baseUrl": "./src",
"importHelpers": true,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"lib": [
"es6"
],
"module": "commonjs",
"moduleResolution": "node",
"noEmitHelpers": false,
"strictNullChecks": false,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"baseUrl": "./src",
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"paths": {},
"lib": [
"es6"
]
"pretty": true,
"skipLibCheck": false,
"sourceMap": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"target": "es5"
},
"exclude": [
"node_modules",
"dist"
],
"compileOnSave": false,
"buildOnSave": false,
"atom": { "rewriteTsconfig": false }
"atom": {
"rewriteTsconfig": false
}
}
Loading

0 comments on commit b6fbefe

Please sign in to comment.