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

Commit

Permalink
Rearchitect lib, next up is tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Oct 2, 2015
1 parent c233ab3 commit 249c7d9
Show file tree
Hide file tree
Showing 259 changed files with 14,650 additions and 5,268 deletions.
3 changes: 1 addition & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = function (grunt) {
typescriptBin: "node_modules/typescript/lib/typescriptServices.js",

clean: {
bin: ["bin/tslint-cli.js"],
core: ["build/rules/", "build/formatters", "lib/tslint.*"],
lib: ["lib/**/*.js", "lib/**/*.d.ts"],
test: ["build/test/"],
},

Expand Down
2 changes: 1 addition & 1 deletion bin/tslint
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("./tslint-cli");
require("../lib/tslint-cli");
1 change: 1 addition & 0 deletions lib/configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function findConfiguration(configFile: string, inputFileLocation: string): any;
73 changes: 73 additions & 0 deletions lib/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
var fs = require("fs");
var path = require("path");
var findup = require("findup-sync");
var CONFIG_FILENAME = "tslint.json";
var DEFAULT_CONFIG = {
"rules": {
"curly": true,
"indent": [true, 4],
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-trailing-whitespace": true,
"no-unreachable": true,
"no-use-before-declare": true,
"quotemark": [true, "double"],
"semicolon": true
}
};
function findConfiguration(configFile, inputFileLocation) {
if (configFile) {
return JSON.parse(fs.readFileSync(configFile, "utf8"));
}
// first look for package.json from input file location
configFile = findup("package.json", { cwd: inputFileLocation, nocase: true });
if (configFile) {
var content = require(configFile);
if (content.tslintConfig) {
return content.tslintConfig;
}
}
// next look for tslint.json
var homeDir = getHomeDir();
if (!homeDir) {
return undefined;
}
var defaultPath = path.join(homeDir, CONFIG_FILENAME);
configFile = findup(CONFIG_FILENAME, { cwd: inputFileLocation, nocase: true }) || defaultPath;
if (fs.existsSync(configFile)) {
return JSON.parse(fs.readFileSync(configFile, "utf8"));
}
else {
return DEFAULT_CONFIG;
}
}
exports.findConfiguration = findConfiguration;
function getHomeDir() {
var environment = global.process.env;
var paths = [environment.USERPROFILE, environment.HOME, environment.HOMEPATH, environment.HOMEDRIVE + environment.HOMEPATH];
for (var homeIndex in paths) {
if (paths.hasOwnProperty(homeIndex)) {
var homePath = paths[homeIndex];
if (homePath && fs.existsSync(homePath)) {
return homePath;
}
}
}
}
9 changes: 9 additions & 0 deletions lib/enableDisableRules.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Lint from "./lint";
import * as ts from "typescript";
export declare class EnableDisableRulesWalker extends Lint.SkippableTokenAwareRuleWalker {
enableDisableRuleMap: {
[rulename: string]: Lint.IEnableDisablePosition[];
};
visitSourceFile(node: ts.SourceFile): void;
private handlePossibleTslintSwitch(commentText, startingPosition);
}
78 changes: 78 additions & 0 deletions lib/enableDisableRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/*
* Copyright 2014 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
var Lint = require("./lint");
var ts = require("typescript");
var EnableDisableRulesWalker = (function (_super) {
__extends(EnableDisableRulesWalker, _super);
function EnableDisableRulesWalker() {
_super.apply(this, arguments);
this.enableDisableRuleMap = {};
}
EnableDisableRulesWalker.prototype.visitSourceFile = function (node) {
var _this = this;
_super.prototype.visitSourceFile.call(this, node);
var scan = ts.createScanner(1 /* ES5 */, false, 0 /* Standard */, node.text);
Lint.scanAllTokens(scan, function (scanner) {
var 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.
scanner.setTextPos(_this.tokensToSkipStartEndMap[startPos]);
return;
}
if (scanner.getToken() === 3 /* MultiLineCommentTrivia */) {
var commentText = scanner.getTokenText();
var startPosition = scanner.getTokenPos();
_this.handlePossibleTslintSwitch(commentText, startPosition);
}
});
};
EnableDisableRulesWalker.prototype.handlePossibleTslintSwitch = function (commentText, startingPosition) {
var _this = this;
var 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(":");
// 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|$)/);
if (enableOrDisableMatch != null) {
var isEnabled = enableOrDisableMatch[1] === "enable";
var position = currentPosition;
var rulesList = ["all"];
if (commentTextParts.length > 2) {
rulesList = commentTextParts[2].split(/\s+/);
}
rulesList.forEach(function (ruleToAdd) {
if (!(ruleToAdd in _this.enableDisableRuleMap)) {
_this.enableDisableRuleMap[ruleToAdd] = [];
}
_this.enableDisableRuleMap[ruleToAdd].push({
isEnabled: isEnabled,
position: position
});
});
}
}
};
return EnableDisableRulesWalker;
})(Lint.SkippableTokenAwareRuleWalker);
exports.EnableDisableRulesWalker = EnableDisableRulesWalker;
1 change: 1 addition & 0 deletions lib/formatterLoader.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function findFormatter(name: string, formattersDirectory?: string): any;
64 changes: 64 additions & 0 deletions lib/formatterLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
var fs = require("fs");
var path = require("path");
var underscore_string_1 = require("underscore.string");
var moduleDirectory = path.dirname(module.filename);
var CORE_FORMATTERS_DIRECTORY = path.resolve(moduleDirectory, "..", "build", "formatters");
function findFormatter(name, formattersDirectory) {
if (typeof (name) === "function") {
return name;
}
var camelizedName = underscore_string_1.camelize(name + "Formatter");
// first check for core formatters
var Formatter = loadFormatter(CORE_FORMATTERS_DIRECTORY, camelizedName);
if (Formatter) {
return Formatter;
}
// then check for rules within the first level of rulesDirectory
if (formattersDirectory) {
Formatter = loadFormatter(formattersDirectory, camelizedName);
if (Formatter) {
return Formatter;
}
}
// else try to resolve as module
return loadFormatterModule(name);
}
exports.findFormatter = findFormatter;
function loadFormatter() {
var paths = [];
for (var _i = 0; _i < arguments.length; _i++) {
paths[_i - 0] = arguments[_i];
}
var formatterPath = paths.reduce(function (p, c) { return path.join(p, c); }, "");
var fullPath = path.resolve(moduleDirectory, formatterPath);
if (fs.existsSync(fullPath + ".js")) {
var formatterModule = require(fullPath);
return formatterModule.Formatter;
}
return undefined;
}
function loadFormatterModule(name) {
var src;
try {
src = require.resolve(name);
}
catch (e) {
return undefined;
}
return require(src).Formatter;
}
2 changes: 2 additions & 0 deletions lib/formatters.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./language/formatter/abstractFormatter";
export * from "./formatters/index";
5 changes: 5 additions & 0 deletions lib/formatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./language/formatter/abstractFormatter"));
__export(require("./formatters/index"));
4 changes: 4 additions & 0 deletions lib/formatters/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./jsonFormatter";
export * from "./pmdFormatter";
export * from "./proseFormatter";
export * from "./verboseFormatter";
7 changes: 7 additions & 0 deletions lib/formatters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./jsonFormatter"));
__export(require("./pmdFormatter"));
__export(require("./proseFormatter"));
__export(require("./verboseFormatter"));
4 changes: 4 additions & 0 deletions lib/formatters/jsonFormatter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as Lint from "../lint";
export declare class Formatter extends Lint.Formatters.AbstractFormatter {
format(failures: Lint.RuleFailure[]): string;
}
33 changes: 33 additions & 0 deletions lib/formatters/jsonFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
var Lint = require("../lint");
var Formatter = (function (_super) {
__extends(Formatter, _super);
function Formatter() {
_super.apply(this, arguments);
}
Formatter.prototype.format = function (failures) {
var failuresJSON = failures.map(function (failure) { return failure.toJson(); });
return JSON.stringify(failuresJSON);
};
return Formatter;
})(Lint.Formatters.AbstractFormatter);
exports.Formatter = Formatter;
4 changes: 4 additions & 0 deletions lib/formatters/pmdFormatter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as Lint from "../lint";
export declare class Formatter extends Lint.Formatters.AbstractFormatter {
format(failures: Lint.RuleFailure[]): string;
}
49 changes: 49 additions & 0 deletions lib/formatters/pmdFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
var Lint = require("../lint");
var Formatter = (function (_super) {
__extends(Formatter, _super);
function Formatter() {
_super.apply(this, arguments);
}
Formatter.prototype.format = function (failures) {
var output = "<pmd version=\"tslint\">";
for (var _i = 0; _i < failures.length; _i++) {
var failure = failures[_i];
var failureString = failure.getFailure()
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\'/g, "&#39;")
.replace(/\"/g, "&quot;");
var 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;
};
return Formatter;
})(Lint.Formatters.AbstractFormatter);
exports.Formatter = Formatter;
4 changes: 4 additions & 0 deletions lib/formatters/proseFormatter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as Lint from "../lint";
export declare class Formatter extends Lint.Formatters.AbstractFormatter {
format(failures: Lint.RuleFailure[]): string;
}
Loading

0 comments on commit 249c7d9

Please sign in to comment.