Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async lint(document: TextDocument, settings: PhpcsSettings): Promise {
// Process linting paths.
let filePath = Files.uriToFilePath(document.uri);
// Make sure we capitalize the drive letter in paths on Windows.
if (filePath !== undefined && /^win/.test(process.platform)) {
let pathRoot: string = path.parse(filePath).root;
let noDrivePath = filePath.slice(Math.max(pathRoot.length - 1, 0));
filePath = path.join(pathRoot.toUpperCase(), noDrivePath);
}
let fileText = document.getText();
// Return empty on empty text.
if (fileText === '') {
return [];
}
// Process linting arguments.
connection.onInitialize(async ({ workspaceFolders }) => {
const completionTriggerCharacters = [' ', ',', '{', '[', '=', ':', '/', '!', "'", '"', '.', '@']
const completionCommitCharacters = [...completionTriggerCharacters, '}', ']']
if (workspaceFolders) {
workspaceFolder = workspaceFolders[0]
workspaceFolderPath = Files.uriToFilePath(workspaceFolder.uri) as string
connection.console.info(`workspaceFolderPath = ${workspaceFolderPath}`)
dotPath = path.join(workspaceFolderPath as string, '.datapack')
cachePath = path.join(dotPath, 'cache.json')
dataPath = path.join(workspaceFolderPath as string, 'data')
connection.console.info(`dotPath = ${dotPath}`)
connection.console.info(`cachePath = ${cachePath}`)
connection.console.info(`dataPath = ${dataPath}`)
if (!fs.pathExistsSync(dotPath)) {
fs.mkdirpSync(dotPath)
}
if (fs.existsSync(cachePath)) {
cacheFile = await fs.readJson(cachePath, { encoding: 'utf8' })
if (cacheFile.version !== LatestCacheFileVersion) {
cacheFile = { cache: {}, files: {}, version: LatestCacheFileVersion }
export default function validateDocument(document: TextDocument): Diagnostic[] {
const diagnostics: Diagnostic[] = [],
contents = document.getText(),
filePath = Files.uriToFilePath(document.uri),
cliEngine = new wist.CLIEngine(),
report: Report = cliEngine.executeOnText(contents, filePath);
if (report && report.results && Array.isArray(report.results) && report.results.length > 0) {
const docReport = report.results[0];
if (docReport.messages && Array.isArray(docReport.messages)) {
docReport.messages.forEach((problem) => {
if (problem) {
const diagnostic = makeDiagnostic(problem);
diagnostics.push(diagnostic);
}
});
}
}
private getViewImportsFiles(): string[] {
let currentDir = Files.uriToFilePath(this._document.uri);
if (!currentDir) return [];
let files: string[] = [];
while (currentDir !== this._rootDir) {
currentDir = path.dirname(currentDir);
fs.readdirSync(currentDir).forEach(f => {
if (f.includes('_ViewImports.cshtml')) files.push(currentDir + path.sep + f);
});
}
return files;
}
private getRootPath() {
let currentDir = Files.uriToFilePath(this._document.uri);
if (!currentDir) return
while (currentDir !== this._rootDir) {
currentDir = path.dirname(currentDir);
fs.readdirSync(currentDir).forEach(f => {
if (f.includes('project.json') || f.includes('csproj')) {
if (currentDir) this._rootDir = currentDir;
return;
}
});
}
}
private getRootPath() {
let currentDir = Files.uriToFilePath(this._document.uri);
while (currentDir !== this._rootDir) {
currentDir = path.dirname(currentDir);
fs.readdirSync(currentDir).forEach(f => {
if (f.includes('project.json') || f.includes('csproj')) {
this._rootDir = currentDir;
return;
}
});
}
}
function doValidate(document: TextDocument): any {
const uri = document.uri;
const content: string = document.getText();
const diagnostics = [];
const lang: string = document.languageId;
const syntax = getSyntax(lang);
if (editorSettings.ignoreFiles.length) {
let fsPath: string = Files.uriToFilePath(uri);
if (workspaceFolder) {
fsPath = path.relative(workspaceFolder, fsPath);
}
const match = micromatch([fsPath], editorSettings.ignoreFiles);
if (editorSettings.ignoreFiles && match.length !== 0) {
return diagnostics;
}
}
const linterOptions = {
browsers: editorSettings.browsers,
ignore: editorSettings.ignore,
onFeatureUsage: (usageInfo) => diagnostics.push(makeDiagnostic(usageInfo))
};
public getCurrentController(): string {
let folderNameRegExp = new RegExp('.*\\' + path.sep + '([a-zA-Z]+)$');
let name = folderNameRegExp.exec(path.dirname(Files.uriToFilePath(this._document.uri)));
if (name) return name[1]
return '';
}
function getRelFromUri(uri: string) {
const abs = Files.uriToFilePath(uri) as string
if (workspaceFolderPath) {
return path.relative(workspaceFolderPath, abs)
}
return abs
}