Upgrade linguist dependency

This version changes how it counts python heredoc. All heredoc is
counted as code.
This commit is contained in:
Andrew Eisenberg 2021-08-25 10:45:44 -07:00
parent a44b61d961
commit b29bf7b05a
32 changed files with 410 additions and 339 deletions

View file

@ -3,8 +3,6 @@
*/
import * as fs from 'fs-extra';
import * as Path from 'path';
// @ts-ignore
import slash from 'slash2';
import { Languages, Regexes } from './languages';
@ -40,6 +38,7 @@ const DefaultFileInfo: FileInfo = {
*/
export class LocFile {
public path: string;
private rawPath: string;
private languages = new Languages();
@ -59,18 +58,16 @@ export class LocFile {
const total = codeLength;
let inMultiLineComment = false;
lines.forEach((line) => {
lines.forEach((origLine) => {
let lineType = 'code';
line = line.trim();
const line = origLine.trim();
if (inMultiLineComment) {
let noCode = true;
if (regexes.multiLineCommentClose.test(line)) {
if (regexes.multiLineCommentClose?.test(line)) {
// line contains the end of a multi-line comment
inMultiLineComment = false;
if (!regexes.multiLineCommentCloseEnd.test(line)) {
if (!regexes.multiLineCommentCloseEnd?.test(line)) {
// the multiline comment does not end this line.
// there is real code on it.
noCode = false;
@ -82,33 +79,29 @@ export class LocFile {
commentLength += 1;
codeLength -= 1;
}
} else if (line) {
// non-empty line
if (regexes.multiLineCommentOpen.test(line)) {
if (regexes.multiLineCommentOpen?.test(line)) {
// line contains the start of a multi-line comment
// might contain some real code, but we'll let that slide
if (!regexes.multiLineCommentOpenAndClose.test(line)) {
if (!regexes.multiLineCommentOpenAndClose?.test(line)) {
// comment is not also closed on this line
inMultiLineComment = true;
}
if (regexes.multiLineCommentOpenStart.test(line)) {
if (regexes.multiLineCommentOpenStart?.test(line)) {
// The comment starts the line. There is no other code on this line
commentLength += 1;
codeLength -= 1;
lineType = 'comm';
}
} else if (regexes.singleLineComment.test(line)) {
} else if (regexes.singleLineComment?.test(line)) {
// line contains only a single line comment
commentLength += 1;
codeLength -= 1;
lineType = 'comm';
}
} else {
// empty line
codeLength -= 1;
@ -116,7 +109,7 @@ export class LocFile {
}
if (this.debug) {
console.log(lineType, line)
console.log(lineType, line);
}
});
@ -137,14 +130,16 @@ export class LocFile {
}
let newData = data;
const info: FileInfo = Object.assign({}, DefaultFileInfo);
const name = this.path.split(Path.sep).pop() || '';
const info: FileInfo = { ...DefaultFileInfo };
// note: do not use Path.sep here since we have already
// translated the path to a posix path using slash(...).
const name = this.path.split('/').pop() || '';
try {
const stat = await fs.stat(this.path);
if (!stat.isFile()) {
return info;
}
newData = data || await fs.readFile(this.path, 'utf-8');
newData = data || (await fs.readFile(this.path, 'utf-8'));
info.name = name;
info.size = (stat && stat.size) || 0;
info.languages = this.languages.getType(this.path);
@ -162,7 +157,7 @@ export class LocFile {
}
public getFileInfoByContent(name: string, data: string): FileInfo {
const info: FileInfo = Object.assign({}, DefaultFileInfo);
const info: FileInfo = { ...DefaultFileInfo };
info.name = name;
info.languages = this.languages.getType(name);
info.lines = this.filterData(data, this.languages.getRegexes(info.languages));