Skip to content

Commit

Permalink
Merge pull request palantir#454 from adidahiya/use-latest-tslint
Browse files Browse the repository at this point in the history
Update to tslint 2.3.0-beta and use new rules
  • Loading branch information
adidahiya committed Jun 23, 2015
2 parents b008900 + ade1f2a commit 3b3c56c
Show file tree
Hide file tree
Showing 126 changed files with 1,249 additions and 1,341 deletions.
10 changes: 5 additions & 5 deletions lib/tslint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ declare module Lint {
getOptions(): any;
hasOption(option: string): boolean;
skip(node: ts.Node): void;
createFailure(start: number, width: number, failure: string): Lint.RuleFailure;
createFailure(start: number, width: number, failure: string): RuleFailure;
addFailure(failure: RuleFailure): void;
private existsFailure(failure);
}
Expand Down Expand Up @@ -149,9 +149,9 @@ declare module Lint {
getPosition(): number;
getLineAndCharacter(): ts.LineAndCharacter;
toJson(): {
position: number;
line: number;
character: number;
line: number;
position: number;
};
equals(ruleFailurePosition: RuleFailurePosition): boolean;
}
Expand All @@ -165,8 +165,8 @@ declare module Lint {
constructor(sourceFile: ts.SourceFile, start: number, end: number, failure: string, ruleName: string);
getFileName(): string;
getRuleName(): string;
getStartPosition(): Lint.RuleFailurePosition;
getEndPosition(): Lint.RuleFailurePosition;
getStartPosition(): RuleFailurePosition;
getEndPosition(): RuleFailurePosition;
getFailure(): string;
toJson(): any;
equals(ruleFailure: RuleFailure): boolean;
Expand Down
23 changes: 11 additions & 12 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

module Lint.Configuration {
var fs = require("fs");
var path = require("path");
var findup = require("findup-sync");
const fs = require("fs");
const path = require("path");
const findup = require("findup-sync");

const CONFIG_FILENAME = "tslint.json";
const DEFAULT_CONFIG = {
Expand Down Expand Up @@ -45,21 +45,21 @@ module Lint.Configuration {
configFile = findup("package.json", { cwd: inputFileLocation, nocase: true });

if (configFile) {
var content = require(configFile);
const content = require(configFile);

if (content.tslintConfig) {
return content.tslintConfig;
}
}

// Next look for tslint.json
var homeDir = getHomeDir();
const homeDir = getHomeDir();

if (!homeDir) {
return undefined;
}

var defaultPath = path.join(homeDir, CONFIG_FILENAME);
const defaultPath = path.join(homeDir, CONFIG_FILENAME);

configFile = findup(CONFIG_FILENAME, { cwd: inputFileLocation, nocase: true }) || defaultPath;

Expand All @@ -71,13 +71,12 @@ module Lint.Configuration {
}

function getHomeDir() {
var environment = global.process.env;
var paths = [environment.USERPROFILE, environment.HOME, environment.HOMEPATH, environment.HOMEDRIVE + environment.HOMEPATH];
const environment = global.process.env;
const paths = [environment.USERPROFILE, environment.HOME, environment.HOMEPATH, environment.HOMEDRIVE + environment.HOMEPATH];

for (var homeIndex in paths) {
for (const homeIndex in paths) {
if (paths.hasOwnProperty(homeIndex)) {
var homePath = paths[homeIndex];

const homePath = paths[homeIndex];
if (homePath && fs.existsSync(homePath)) {
return homePath;
}
Expand Down
27 changes: 15 additions & 12 deletions src/enableDisableRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

module Lint {
export class EnableDisableRulesWalker extends Lint.SkippableTokenAwareRuleWalker {
public enableDisableRuleMap: {[rulename: string]: Lint.IEnableDisablePosition[]} = {};

public visitSourceFile(node: ts.SourceFile): void {
public visitSourceFile(node: ts.SourceFile) {
super.visitSourceFile(node);
Lint.scanAllTokens(ts.createScanner(ts.ScriptTarget.ES5, false, node.text), (scanner: ts.Scanner) => {
var startPos = scanner.getStartPos();
const startPos = scanner.getStartPos();
if (this.tokensToSkipStartEndMap[startPos] != null) {
// tokens to skip are places where the scanner gets confused about what the token is, without the proper context
// (specifically, regex, identifiers, and templates). So skip those tokens.
Expand All @@ -30,33 +30,36 @@ module Lint {
}

if (scanner.getToken() === ts.SyntaxKind.MultiLineCommentTrivia) {
var commentText = scanner.getTokenText();
var startPosition = scanner.getTokenPos();
const commentText = scanner.getTokenText();
const startPosition = scanner.getTokenPos();
this.handlePossibleTslintSwitch(commentText, startPosition);
}
});
}

private handlePossibleTslintSwitch(commentText: string, startingPosition: number) {
var currentPosition = startingPosition;
const currentPosition = startingPosition;
// regex is: start of string followed by "/*" followed by any amount of whitespace followed by "tslint:"
if (commentText.match(/^\/\*\s*tslint:/)) {
var commentTextParts = commentText.split(":");
const commentTextParts = commentText.split(":");
// regex is: start of string followed by either "enable" or "disable"
// followed by either whitespace or end of string
var enableOrDisableMatch = commentTextParts[1].match(/^(enable|disable)(\s|$)/);
const enableOrDisableMatch = commentTextParts[1].match(/^(enable|disable)(\s|$)/);
if (enableOrDisableMatch != null) {
var isEnabled = enableOrDisableMatch[1] === "enable";
var position = currentPosition;
var rulesList = ["all"];
const isEnabled = enableOrDisableMatch[1] === "enable";
const position = currentPosition;
let rulesList = ["all"];
if (commentTextParts.length > 2) {
rulesList = commentTextParts[2].split(/\s+/);
}
rulesList.forEach((ruleToAdd) => {
if (!(ruleToAdd in this.enableDisableRuleMap)) {
this.enableDisableRuleMap[ruleToAdd] = [];
}
this.enableDisableRuleMap[ruleToAdd].push({position: position, isEnabled: isEnabled});
this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: isEnabled,
position: position
});
});
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/formatterLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

module Lint {
var fs = require("fs");
var path = require("path");
var _s = require("underscore.string");
const fs = require("fs");
const path = require("path");
const _s = require("underscore.string");

var moduleDirectory = path.dirname(module.filename);
var CORE_FORMATTERS_DIRECTORY = path.resolve(moduleDirectory, "..", "build", "formatters");
const moduleDirectory = path.dirname(module.filename);
const CORE_FORMATTERS_DIRECTORY = path.resolve(moduleDirectory, "..", "build", "formatters");

export function findFormatter(name: string, formattersDirectory?: string) {
if (typeof(name) === "function") {
return name;
}

var camelizedName = _s.camelize(name + "Formatter");
const camelizedName = _s.camelize(name + "Formatter");

// first check for core formatters
var Formatter = loadFormatter(CORE_FORMATTERS_DIRECTORY, camelizedName);
let Formatter = loadFormatter(CORE_FORMATTERS_DIRECTORY, camelizedName);
if (Formatter) {
return Formatter;
}
Expand All @@ -48,19 +48,19 @@ module Lint {
}

function loadFormatter(...paths: string[]) {
var formatterPath = paths.reduce((p, c) => path.join(p, c), "");
var fullPath = path.resolve(moduleDirectory, formatterPath);
const formatterPath = paths.reduce((p, c) => path.join(p, c), "");
const fullPath = path.resolve(moduleDirectory, formatterPath);

if (fs.existsSync(fullPath + ".js")) {
var formatterModule = require(fullPath);
const formatterModule = require(fullPath);
return formatterModule.Formatter;
}

return undefined;
}

function loadFormatterModule(name: string) {
var src: string;
let src: string;
try {
src = require.resolve(name);
} catch (e) {
Expand Down
5 changes: 2 additions & 3 deletions src/formatters/jsonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

export class Formatter extends Lint.Formatters.AbstractFormatter {
public format(failures: Lint.RuleFailure[]): string {
var failuresJSON = failures.map((failure) => failure.toJson());

const failuresJSON = failures.map((failure) => failure.toJson());
return JSON.stringify(failuresJSON);
}
}
12 changes: 6 additions & 6 deletions src/formatters/pmdFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

export class Formatter extends Lint.Formatters.AbstractFormatter {
public format(failures: Lint.RuleFailure[]): string {
var output = "<pmd version=\"tslint\">";
let output = "<pmd version=\"tslint\">";

failures.forEach((failure: Lint.RuleFailure) => {
var failureString = failure.getFailure()
for (let failure of failures) {
const failureString = failure.getFailure()
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\'/g, "&#39;")
.replace(/\"/g, "&quot;");

var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();

output += "<file name=\"" + failure.getFileName();
output += "\"><violation begincolumn=\"" + (lineAndCharacter.character + 1);
output += "\" beginline=\"" + (lineAndCharacter.line + 1);
output += "\" priority=\"1\"";
output += " rule=\"" + failureString + "\"> </violation></file>";
});
}

output += "</pmd>";
return output;
Expand Down
15 changes: 7 additions & 8 deletions src/formatters/proseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

export class Formatter extends Lint.Formatters.AbstractFormatter {
public format(failures: Lint.RuleFailure[]): string {
const outputLines = failures.map((failure: Lint.RuleFailure) => {
const fileName = failure.getFileName();
const failureString = failure.getFailure();

var outputLines = failures.map((failure: Lint.RuleFailure) => {
var fileName = failure.getFileName();
var failureString = failure.getFailure();
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const positionTuple = `[${lineAndCharacter.line + 1}, ${lineAndCharacter.character + 1}]`;

var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
var positionTuple = "[" + (lineAndCharacter.line + 1) + ", " + (lineAndCharacter.character + 1) + "]";

return fileName + positionTuple + ": " + failureString;
return `${fileName}${positionTuple}: ${failureString}`;
});

return outputLines.join("\n") + "\n";
Expand Down
12 changes: 6 additions & 6 deletions src/formatters/verboseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
export class Formatter extends Lint.Formatters.AbstractFormatter {
public format(failures: Lint.RuleFailure[]): string {

var outputLines = failures.map((failure: Lint.RuleFailure) => {
var fileName = failure.getFileName();
var failureString = failure.getFailure();
var ruleName = failure.getRuleName();
const outputLines = failures.map((failure: Lint.RuleFailure) => {
const fileName = failure.getFileName();
const failureString = failure.getFailure();
const ruleName = failure.getRuleName();

var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
var positionTuple = "[" + (lineAndCharacter.line + 1) + ", " + (lineAndCharacter.character + 1) + "]";
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const positionTuple = "[" + (lineAndCharacter.line + 1) + ", " + (lineAndCharacter.character + 1) + "]";

return "(" + ruleName + ") " + fileName + positionTuple + ": " + failureString;
});
Expand Down
2 changes: 0 additions & 2 deletions src/language/formatter/abstractFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

module Lint.Formatters {
export class AbstractFormatter implements Lint.IFormatter {
/* tslint:disable:no-unused-variable */
public format(failures: Lint.RuleFailure[]): string {
throw Lint.abstract();
}
/* tslint:enable:no-unused-variable */
}
}
18 changes: 8 additions & 10 deletions src/language/languageServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
* limitations under the License.
*/

/* tslint:disable:no-unused-variable */

module Lint {
export function createLanguageServiceHost(fileName: string, source: string) {
var host: ts.LanguageServiceHost = {
const host: ts.LanguageServiceHost = {
getCompilationSettings: () => Lint.createCompilerOptions(),
getCurrentDirectory: () => "",
getDefaultLibFileName: () => "lib.d.ts",
getScriptFileNames: () => [fileName],
getScriptVersion: () => "1",
getScriptIsOpen: () => true,
getScriptSnapshot: () => {
return {
getText: (start, end) => source.substring(start, end),
getChangeRange: (oldSnapshot) => undefined,
getLength: () => source.length,
getLineStartPositions: () => ts.computeLineStarts(source),
getChangeRange: (oldSnapshot) => undefined
getText: (start, end) => source.substring(start, end)
};
},
getCurrentDirectory: () => "",
getScriptIsOpen: () => true,
getCompilationSettings: () => Lint.createCompilerOptions(),
getDefaultLibFileName: () => "lib.d.ts",
getScriptVersion: () => "1",
log: (message) => { /* */ }
};

Expand Down
9 changes: 4 additions & 5 deletions src/language/rule/abstractRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ module Lint.Rules {
private options: Lint.IOptions;

constructor(ruleName: string, value: any, disabledIntervals: Lint.IDisabledInterval[]) {
var ruleArguments: any[] = [];
let ruleArguments: any[] = [];

if (Array.isArray(value) && value.length > 1) {
ruleArguments = value.slice(1);
}

this.value = value;
this.options = {
disabledIntervals: disabledIntervals,
ruleArguments: ruleArguments,
ruleName: ruleName,
disabledIntervals: disabledIntervals
ruleName: ruleName
};
}

Expand All @@ -50,7 +50,7 @@ module Lint.Rules {
}

public isEnabled(): boolean {
var value = this.value;
const value = this.value;

if (typeof value === "boolean") {
return value;
Expand All @@ -63,5 +63,4 @@ module Lint.Rules {
return false;
}
}

}
Loading

0 comments on commit 3b3c56c

Please sign in to comment.