Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(valid-repository-directory): use repository root for more accurate linting #498

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"pnpm-lock.yaml"
],
"words": [
"altano",
"autofixable",
"Codecov",
"codespace",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"*": "prettier --ignore-unknown --write"
},
"dependencies": {
"@altano/repository-tools": "^0.1.1",
altano marked this conversation as resolved.
Show resolved Hide resolved
"detect-indent": "6.1.0",
"detect-newline": "3.1.0",
"package-json-validator": "^0.6.5",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 95 additions & 16 deletions src/rules/valid-repository-directory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import type { AST as JsonAST } from "jsonc-eslint-parser";

import { findRootSync } from "@altano/repository-tools/findRootSync.cjs";
import * as ESTree from "estree";
import * as path from "node:path";

import { createRule } from "../createRule.js";
import { findPropertyWithKeyValue } from "../utils/findPropertyWithKeyValue.js";

/**
* Checks if the child path appears at the end of the parent path. e.g.
*
* '/a/b/c', 'c' => true
* '/a/b/c', 'b/c' => true
* '/a/b/c', 'b' => false
* '/a/b/c', 'd' => false
*/
function pathEndsWith(parent: string, child: string): boolean {
const segments = parent.split(path.sep);

if (parent === child) {
// the directory specified was the full, absolute path to the
// package.json
return true;
}

// work backwards from the end, adding another path segment to each check
let pathToCheck = "";
return segments.reverse().some((segment) => {
pathToCheck = path.join(segment, pathToCheck);
if (pathToCheck === child) {
return true;
}
});
}

export const rule = createRule({
create(context) {
return {
Expand All @@ -26,24 +54,75 @@ export const rule = createRule({
}

const directoryValue = directoryProperty.value.value;
const expected = path.normalize(path.dirname(context.filename));
const fileDirectory = path.normalize(
path.dirname(context.filename),
);
const repositoryRoot = findRootSync(fileDirectory);

if (repositoryRoot == null) {
// Since we couldn't determine the repository root, we can't
// rigorously determine the validity of the directory value.
// But, we can at least make sure the directory value
// appears at the end of the package.json file path. For
// example:
//
// if /a/b/c/package.json has the value: directory: "b/c"
// it will validate
//
// if /a/b/c/package.json has the value: directory: "b/d"
// it will NOT validate
//
// This ensures that the directory value is at least
// partially correct.
if (
!pathEndsWith(
fileDirectory,
path.normalize(directoryValue),
)
) {
context.report({
messageId: "mismatched",
node: directoryProperty.value as unknown as ESTree.Node,
});
}
} else {
// Get the relative path from repository root to
// package.json. For example:
//
// Simple case (root package.json):
// repositoryRoot: '~/src/project'
// filename: '~/src/project/package.json',
// fileDirectory: '~/src/project',
// expected: '' (directory should be undefined or empty)
//
// Monorepo case (nested package.json):
// repositoryRoot: '~/src/project'
// filename: '~/src/project/packages/packageA/package.json',
// fileDirectory: '~/src/project/packages/packageA',
// expected: 'packages/packageA'
//
const expected = path.relative(
repositoryRoot,
fileDirectory,
);

if (path.normalize(directoryValue) !== expected) {
context.report({
messageId: "mismatched",
node: directoryProperty.value as unknown as ESTree.Node,
suggest: [
{
fix(fixer) {
return fixer.replaceText(
directoryProperty.value as unknown as ESTree.Node,
`"${expected}"`,
);
if (expected !== directoryValue) {
context.report({
messageId: "mismatched",
node: directoryProperty.value as unknown as ESTree.Node,
suggest: [
{
fix(fixer) {
return fixer.replaceText(
directoryProperty.value as unknown as ESTree.Node,
`"${expected}"`,
);
},
messageId: "replace",
},
messageId: "replace",
},
],
});
],
});
}
}
},
};
Expand Down
Loading
Loading