• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ota-meshi / jsonc-eslint-parser / 9441882216

10 Jun 2024 03:08AM CUT coverage: 88.791%. Remained the same
9441882216

Pull #167

github

web-flow
Merge 98884f0b5 into 1fe0d2e4e
Pull Request #167: chore(deps): update dependency typescript to ~5.4.0

308 of 365 branches covered (84.38%)

Branch coverage included in aggregate %.

603 of 661 relevant lines covered (91.23%)

64.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/src/parser/modules/require-utils.ts
1
import path from "path";
1✔
2
import { lte } from "semver";
1✔
3
import type ModuleClass from "module";
4

5
/**
6
 * createRequire
7
 */
8
export function createRequire(
1✔
9
  filename: string,
10
): ReturnType<typeof ModuleClass.createRequire> {
11
  // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention -- special require
12
  const Module = require("module");
6✔
13
  const fn: (
14
    fileName: string,
15
  ) => // eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
16
  any =
17
    // Added in v12.2.0
18
    Module.createRequire ||
6!
19
    // Added in v10.12.0, but deprecated in v12.2.0.
20
    Module.createRequireFromPath ||
21
    // Polyfill - This is not executed on the tests on node@>=10.
22
    /* istanbul ignore next */
23
    ((filename2: string) => {
24
      const mod = new Module(filename2);
25

26
      mod.filename = filename2;
27
      mod.paths = Module._nodeModulePaths(path.dirname(filename2));
28
      mod._compile("module.exports = require;", filename2);
29
      return mod.exports;
30
    });
31
  return fn(filename);
6✔
32
}
33

34
/**
35
 * Checks if the given string is a linter path.
36
 */
37
function isLinterPath(p: string) {
38
  return (
2,072✔
39
    p.includes(`eslint${path.sep}lib${path.sep}linter${path.sep}linter.js`) ||
4,142✔
40
    p.includes(`eslint${path.sep}lib${path.sep}linter.js`)
41
  );
42
}
43

44
/**
45
 * Get NodeRequire from Linter
46
 */
47
export function getRequireFromLinter(): NodeRequire | null {
1✔
48
  // Lookup the loaded eslint
49
  const linterPath = Object.keys(require.cache).find(isLinterPath);
3✔
50
  if (linterPath) {
3✔
51
    try {
2✔
52
      return createRequire(linterPath);
2✔
53
    } catch {
54
      // ignore
55
    }
56
  }
57
  return null;
1✔
58
}
59

60
/**
61
 * Get NodeRequire from Cwd
62
 */
63
export function getRequireFromCwd(): NodeRequire | null {
1✔
64
  try {
4✔
65
    const cwd = process.cwd();
4✔
66
    const relativeTo = path.join(cwd, "__placeholder__.js");
4✔
67
    return createRequire(relativeTo);
4✔
68
  } catch {
69
    // ignore
70
  }
71
  return null;
×
72
}
73

74
/**
75
 * Get module from Linter
76
 */
77
export function requireFromLinter<T>(module: string): T | null {
1✔
78
  try {
3✔
79
    return getRequireFromLinter()?.(module);
3✔
80
  } catch {
81
    // ignore
82
  }
83
  return null;
×
84
}
85

86
/**
87
 * Get module from Cwd
88
 */
89
export function requireFromCwd<T>(module: string): T | null {
1✔
90
  try {
4✔
91
    return getRequireFromCwd()?.(module);
4!
92
  } catch {
93
    // ignore
94
  }
95
  return null;
×
96
}
97

98
/**
99
 * Get the newest `espree` kind from the loaded ESLint or dependency.
100
 */
101
export function loadNewest<T>(
1✔
102
  items: { getPkg: () => { version: string } | null; get: () => T | null }[],
103
): T {
104
  let target: { version: string; get: () => T | null } | null = null;
4✔
105
  for (const item of items) {
4✔
106
    const pkg = item.getPkg();
12✔
107
    if (pkg != null && (!target || lte(target.version, pkg.version))) {
12✔
108
      target = { version: pkg.version, get: item.get };
11✔
109
    }
110
  }
111
  return target!.get()!;
4✔
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc