Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function getLintConfig(
expectedConfigPath: string,
tsconfigPath: string,
minVersion: TsVersion,
maxVersion: TsVersion,
tsLocal: string | undefined,
): Promise {
const configExists = await pathExists(expectedConfigPath);
const configPath = configExists ? expectedConfigPath : joinPaths(__dirname, "..", "dtslint.json");
// Second param to `findConfiguration` doesn't matter, since config path is provided.
const config = Configuration.findConfiguration(configPath, "").results;
if (!config) {
throw new Error(`Could not load config at ${configPath}`);
}
const expectRule = config.rules.get("expect");
if (!expectRule || expectRule.ruleSeverity !== "error") {
throw new Error("'expect' rule should be enabled, else compile errors are ignored");
}
if (expectRule) {
const versionsToTest =
range(minVersion, maxVersion).map(versionName => ({ versionName, path: typeScriptPath(versionName, tsLocal) }));
const expectOptions: ExpectOptions = { tsconfigPath, versionsToTest };
expectRule.ruleArguments = [expectOptions];
}
return config;
}
TSLint.prototype.processString = function(content, relativePath) {
var linter = new Linter(this.options);
var configLoad = Configuration.findConfiguration(this.tslintConfigPath, relativePath);
linter.lint(relativePath, content, configLoad.results);
var result = linter.getResult();
this.totalFiles++;
var passed = result.errorCount === 0;
var errors = [];
if (!passed) {
// error is seen
this.errorCount += result.errorCount;
errors = result.failures.map((failure) => {
var position = failure.startPosition.lineAndCharacter;
var line = position.line + 1;
var character = position.character + 1;
return `${failure.ruleSeverity.toUpperCase()}: ${failure.fileName}[${line}, ${character}]: ${failure.failure} (${failure.ruleName})`;
export function run(filename: string, config: string) {
const fileContents = fs.readFileSync(filename, 'utf8');
const linter = new Linter({
fix: false,
formatter: 'json',
});
const configuration = Configuration.findConfiguration(config, filename)
.results;
linter.lint(filename, fileContents, configuration);
return linter.getResult();
}
const fs = require("fs");
const { run } = require("tslint/lib/runner");
const { Configuration } = require("tslint");
const { junitReportPath } = require("./utils");
let format = "codeFrame";
let out;
let outputStream = process.stdout;
if (process.env.JUNIT_REPORT_PATH != null) {
format = "junit";
out = junitReportPath("tslint");
console.info(`TSLint report will appear in ${out}`);
outputStream = fs.createWriteStream(out, { flags: "w+" });
}
const configFile = Configuration.findConfiguration(null, __dirname).path;
run({
config: configFile,
exclude: [],
files: ["{src,test}/**/*.tsx"],
fix: process.argv.includes("--fix"),
format,
out,
}, {
error(m) {
process.stderr.write(m);
},
log(m) {
outputStream.write(m);
},
})
export const lintPlugin = useSpinner('Linting', async ({ fix }) => {
let tsLintConfigPath = path.resolve(process.cwd(), 'tslint.json');
if (!fs.existsSync(tsLintConfigPath)) {
tsLintConfigPath = path.resolve(__dirname, '../../config/tslint.plugin.json');
}
const options = {
fix: fix === true,
formatter: 'json',
};
const configuration = Configuration.findConfiguration(tsLintConfigPath).results;
const sourcesToLint = getTypescriptSources();
const lintResults = sourcesToLint
.map(fileName => {
const linter = new Linter(options);
const fileContents = fs.readFileSync(fileName, 'utf8');
linter.lint(fileName, fileContents, configuration);
return linter.getResult();
})
.filter(result => {
return result.errorCount > 0 || result.warningCount > 0;
});
if (lintResults.length > 0) {
console.log('\n');
const failures = lintResults.reduce((failures, result) => {
files.forEach(file => {
const fileContents = program.getSourceFile(file).getFullText();
const configuration = Configuration.findConfiguration("tslint.json", file)
.results;
linter.lint(file, fileContents, configuration);
});
files.forEach(({ filename, content }) => {
const config = Configuration.findConfiguration(configurationFilePath, filename).results;
linter.lint(filename, content, config);
});
function lint(dir: string, configFileName?: string, fixtureFileName?: string): LintResult {
const fixturesDir = path.resolve("./fixtures");
const fixtureDir = `${fixturesDir}/${dir}`;
const fileName = `${fixtureDir}/${fixtureFileName || "fixture.ts"}`;
const content = fs.readFileSync(fileName, "utf8");
const program = Linter.createProgram(`${fixtureDir}/tsconfig.json`);
const linter = new Linter({ fix: false }, program);
const configuration = Configuration.findConfiguration(
configFileName ?
`${fixtureDir}/${configFileName}` :
`${fixturesDir}/tslint.json`,
fileName
).results;
linter.lint(fileName, content, configuration);
return linter.getResult();
}
});
private lintFilesAsync() {
const program = Linter.createProgram(this.tsconfig);
const linter = new Linter({ fix: this.autofix, formatter: Formatter }, program);
const files = Linter.getFileNames(program);
const configuration = Configuration.findConfiguration(this.tslintCfg).results;
const lintInEventLoop = (remainingFiles: string[]) => {
const file = remainingFiles.shift()!;
const fileContents = program.getSourceFile(file)!.getFullText();
linter.lint(file, fileContents, configuration);
if (remainingFiles.length && !this.terminated) {
setImmediate(lintInEventLoop, remainingFiles);
} else {
this.running = false;
if (this.terminated) {
debugLog("Tslint: Aborted", this.tsconfig);
} else {
this.lastResult = linter.getResult().output;
debugLog("Tslint: Done", this.tsconfig);