Skip to content

Commit

Permalink
fix: Fix issues with ESlint running outside of root and missing basel…
Browse files Browse the repository at this point in the history
…ine file
  • Loading branch information
Maciek Pekala committed Jul 18, 2022
1 parent e5380b0 commit 55fd310
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/dependency-cruiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ let tsConfig: unknown;
* we do it once here for the first run (when we know ESLint settings) and then
* memoise it for the consecutive runs.
*/
export function getConfigs(settings: unknown) {
export function getConfigs(settings: unknown, cwd: string) {
// We only check for DC config, since it's guaranteed to be present after we load it
// the first time. The other configs are optional.
if (!depcruiseConfig) {
depcruiseConfig = loadDependencyCruiserConfig(settings);
tsConfig = loadTsConfig(depcruiseConfig.options);
knownViolations = loadKnownViolations(settings);
depcruiseConfig = loadDependencyCruiserConfig(settings, cwd);
tsConfig = loadTsConfig(depcruiseConfig.options, cwd);
knownViolations = loadKnownViolations(settings, cwd);
}

return {
Expand All @@ -46,13 +46,13 @@ export function getConfigs(settings: unknown) {
* the user via ESLint shared global configuration (`'dependency-cruiser'.config`)
* @see https://github.com/sverweij/dependency-cruiser/blob/develop/doc/api.md#utility-functions
*/
function loadDependencyCruiserConfig(settings: unknown) {
function loadDependencyCruiserConfig(settings: unknown, cwd: string) {
let configLocation = dependencyCruiserDefaults.DEFAULT_CONFIG_FILE_NAME;
if (hasKey("config", settings) && typeof settings.config === "string") {
configLocation = settings.config;
}
const depcruiseConfig: IConfiguration = extractDepcruiseConfig(
`./${configLocation}`
path.join(cwd, configLocation)
);
invariant(
!!depcruiseConfig,
Expand All @@ -67,15 +67,15 @@ function loadDependencyCruiserConfig(settings: unknown) {
* DC doesn't specify any types for this configuration, but we don't access it and can treat
* as opaque
*/
function loadTsConfig(options?: ICruiseOptions) {
function loadTsConfig(options: ICruiseOptions | undefined, cwd: string) {
// DC's types here are inconsistent with documentation and implementation.
// `tsConfig` field from "options" part of config is used for loading TS config
// but it's not present on the `ICruiseOptions` interface
const tsConfigFileName = (options as any)?.tsConfig?.fileName;

if (tsConfigFileName) {
try {
return extractTSConfig(`${tsConfigFileName}`) as unknown;
return extractTSConfig(path.join(cwd, tsConfigFileName)) as unknown;
} catch (error) {
return undefined;
}
Expand All @@ -90,7 +90,7 @@ function loadTsConfig(options?: ICruiseOptions) {
* do it here instead (using json5 package).
* @see https://github.com/sverweij/dependency-cruiser/blob/develop/doc/cli.md#--ignore-known-ignore-known-violations
*/
function loadKnownViolations(settings: unknown) {
function loadKnownViolations(settings: unknown, cwd: string) {
let baselineLocation = dependencyCruiserDefaults.DEFAULT_BASELINE_FILE_NAME;
if (
hasKey("knownViolationsFile", settings) &&
Expand All @@ -101,7 +101,7 @@ function loadKnownViolations(settings: unknown) {

const absoluteFilePath = !path.isAbsolute(baselineLocation)
? baselineLocation
: path.join(process.cwd(), `./${baselineLocation}`);
: path.join(cwd, baselineLocation);

try {
return json5.parse(
Expand Down
7 changes: 4 additions & 3 deletions src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export function getDependencyCruiserRule(
const settings: unknown = context.settings["dependency-cruiser"];

const { depcruiseConfig, knownViolations, tsConfig } = getConfigs(
settings
settings,
context.getCwd()
);
const ruleSet = filterRules(depcruiseConfig, severity);

const currentFileLocation = path.relative(
process.cwd(),
context.getCwd(),
context.getPhysicalFilename()
);

Expand All @@ -46,7 +47,7 @@ export function getDependencyCruiserRule(
...ruleSet.options,
maxDepth: 1,
validate: true,
knownViolations,
...(knownViolations ? { knownViolations } : {}),
},
// Webpack configuration is not supported currently, contributions welcome!
undefined,
Expand Down

0 comments on commit 55fd310

Please sign in to comment.