Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

New feature: lint js files #1515

Merged
merged 20 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,83 @@ export const rules = {
"check-typecast",
],
};
export const jsRules = {
"align": [true,
"parameters",
"statements",
],
"class-name": true,
"curly": true,
"eofline": true,
"forin": true,
"indent": [true, "spaces"],
"jsdoc-format": true,
"label-position": true,
"max-line-length": [true, 120],
"new-parens": true,
"no-arg": true,
"no-bitwise": true,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": true,
"no-console": [true,
"debug",
"info",
"log",
"time",
"timeEnd",
"trace",
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-reference": true,
"no-shadowed-variable": true,
"no-string-literal": true,
"no-switch-case-fall-through": false,
"no-trailing-whitespace": true,
"no-unreachable": true,
"no-unused-expression": true,
"no-unused-new": true,
// disable this rule as it is very heavy performance-wise and not that useful
"no-use-before-declare": false,
"object-literal-sort-keys": true,
"one-line": [true,
"check-catch",
"check-else",
"check-finally",
"check-open-brace",
"check-whitespace",
],
"one-variable-per-declaration": [true,
"ignore-for-loop",
],
"quotemark": [true, "double", "avoid-escape"],
"radix": true,
"semicolon": [true, "always"],
"switch-default": true,
"trailing-comma": [true,
{
"multiline": "always",
"singleline": "never",
},
],
"triple-equals": [true, "allow-null-check"],
"use-isnan": true,
"variable-name": [true,
"ban-keywords",
"check-format",
"allow-pascal-case",
],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type",
"check-typecast",
],
};
/* tslint:enable:object-literal-key-quotes */
31 changes: 31 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {arrayify, objectify, stripComments} from "./utils";

export interface IConfigurationFile {
extends?: string | string[];
jsRules?: any;
linterOptions?: {
typeCheck?: boolean,
};
Expand All @@ -34,6 +35,28 @@ export interface IConfigurationFile {
export const CONFIG_FILENAME = "tslint.json";
/* tslint:disable:object-literal-key-quotes */
export const DEFAULT_CONFIG = {
"jsRules": {
"class-name": true,
"comment-format": [true, "check-space"],
"indent": [true, "spaces"],
"no-duplicate-variable": true,
"no-eval": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"no-var-keyword": true,
"one-line": [true, "check-open-brace", "check-whitespace"],
"quotemark": [true, "double"],
"semicolon": [true, "always"],
"triple-equals": [true, "allow-null-check"],
"variable-name": [true, "ban-keywords"],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type",
],
},
"rules": {
"class-name": true,
"comment-format": [true, "check-space"],
Expand Down Expand Up @@ -204,6 +227,14 @@ export function extendConfigurationFile(config: IConfigurationFile, baseConfig:
combinedConfig.rules[name] = config.rules[name];
}

combinedConfig.jsRules = {};
for (const name of Object.keys(objectify(baseConfig.jsRules))) {
combinedConfig.jsRules[name] = baseConfig.jsRules[name];
}
for (const name of Object.keys(objectify(config.jsRules))) {
combinedConfig.jsRules[name] = config.jsRules[name];
}

return combinedConfig;
}

Expand Down
5 changes: 5 additions & 0 deletions src/language/rule/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface IRuleMetadata {
* Whether or not the rule requires type info to run.
*/
requiresTypeInfo?: boolean;

/**
* Whether or not the rule use for TypeScript only.
*/
typescriptOnly?: boolean;
}

export type RuleType = "functionality" | "maintainability" | "style" | "typescript";
Expand Down
1 change: 1 addition & 0 deletions src/language/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function getSourceFile(fileName: string, source: string): ts.SourceFile {

export function createCompilerOptions(): ts.CompilerOptions {
return {
allowJs: true,
noResolve: true,
target: ts.ScriptTarget.ES5,
};
Expand Down
33 changes: 23 additions & 10 deletions src/ruleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export interface IEnableDisablePosition {

export function loadRules(ruleConfiguration: {[name: string]: any},
enableDisableRuleMap: {[rulename: string]: IEnableDisablePosition[]},
rulesDirectories?: string | string[]): IRule[] {
rulesDirectories?: string | string[],
isJs?: boolean): IRule[] {
const rules: IRule[] = [];
const notFoundRules: string[] = [];
const notAllowedInJsRules: string[] = [];

for (const ruleName in ruleConfiguration) {
if (ruleConfiguration.hasOwnProperty(ruleName)) {
Expand All @@ -44,15 +46,19 @@ export function loadRules(ruleConfiguration: {[name: string]: any},
if (Rule == null) {
notFoundRules.push(ruleName);
} else {
const all = "all"; // make the linter happy until we can turn it on and off
const allList = (all in enableDisableRuleMap ? enableDisableRuleMap[all] : []);
const ruleSpecificList = (ruleName in enableDisableRuleMap ? enableDisableRuleMap[ruleName] : []);
const disabledIntervals = buildDisabledIntervalsFromSwitches(ruleSpecificList, allList);
rules.push(new Rule(ruleName, ruleValue, disabledIntervals));

if (Rule.metadata && Rule.metadata.deprecationMessage && shownDeprecations.indexOf(Rule.metadata.ruleName) === -1) {
console.warn(`${Rule.metadata.ruleName} is deprecated. ${Rule.metadata.deprecationMessage}`);
shownDeprecations.push(Rule.metadata.ruleName);
if (isJs && Rule.metadata.typescriptOnly != null && Rule.metadata.typescriptOnly) {
notAllowedInJsRules.push(ruleName);
} else {
const all = "all"; // make the linter happy until we can turn it on and off
const allList = (all in enableDisableRuleMap ? enableDisableRuleMap[all] : []);
const ruleSpecificList = (ruleName in enableDisableRuleMap ? enableDisableRuleMap[ruleName] : []);
const disabledIntervals = buildDisabledIntervalsFromSwitches(ruleSpecificList, allList);
rules.push(new Rule(ruleName, ruleValue, disabledIntervals));

if (Rule.metadata && Rule.metadata.deprecationMessage && shownDeprecations.indexOf(Rule.metadata.ruleName) === -1) {
console.warn(`${Rule.metadata.ruleName} is deprecated. ${Rule.metadata.deprecationMessage}`);
shownDeprecations.push(Rule.metadata.ruleName);
}
}
}
}
Expand All @@ -67,6 +73,13 @@ export function loadRules(ruleConfiguration: {[name: string]: any},
`;

throw new Error(ERROR_MESSAGE);
} else if (notAllowedInJsRules.length > 0) {
const JS_ERROR_MESSAGE = `
Could not apply to JavaScript files for the following rules specified in the configuration:
${notAllowedInJsRules.join("\n")}
`;

throw new Error(JS_ERROR_MESSAGE);
} else {
return rules;
}
Expand Down
1 change: 1 addition & 0 deletions src/rules/adjacentOverloadSignaturesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "typescript",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/alignRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ['[true, "parameters", "statements"]'],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/arrayTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: [`[true, ${OPTION_ARRAY}]`, `[true, ${OPTION_GENERIC}]`, `[true, ${OPTION_ARRAY_SIMPLE}]`],
type: "style",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/arrowParensRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/banRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [`[true, ["someGlobalMethod"], ["someObject", "someFunction"],
["someObject", "otherFunction", "Optional explanation"]]`],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/classNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/commentFormatRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ['[true, "check-space", "check-lowercase"]'],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/curlyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/eoflineRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING = "file should end with a newline";
Expand Down
1 change: 1 addition & 0 deletions src/rules/fileHeaderRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ['[true, "Copyright \\\\d{4}"]'],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/forinRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/indentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ['[true, "spaces"]'],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/interfaceNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: [`[true, "${OPTION_ALWAYS}"]`, `[true, "${OPTION_NEVER}"]`],
type: "style",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/jsdocFormatRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/labelPositionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/linebreakStyleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: [`[true, "${OPTION_LINEBREAK_STYLE_LF}"]`, `[true, "${OPTION_LINEBREAK_STYLE_CRLF}"]`],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/maxFileLineCountRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ["[true, 300]"],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/maxLineLengthRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ["[true, 120]"],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/memberAccessRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ["true", '[true, "check-accessor"]'],
type: "typescript",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/memberOrderingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class Rule extends Lint.Rules.AbstractRule {
},
optionExamples: ['[true, { "order": "fields-first" }]'],
type: "typescript",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
Expand Down
1 change: 1 addition & 0 deletions src/rules/newParensRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/noAngleBracketTypeAssertionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
1 change: 1 addition & 0 deletions src/rules/noAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
optionExamples: ["true"],
type: "typescript",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
Loading