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

Improve some more code #1892

Merged
merged 3 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 2 additions & 6 deletions src/lib/converter/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ export class Context {
if (!nodeType) {
if (node.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(node.symbol);
} else if (node.parent && node.parent.symbol) {
} else if (node.parent?.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(
node.parent.symbol
);
} else if (
node.parent &&
node.parent.parent &&
node.parent.parent.symbol
) {
} else if (node.parent?.parent?.symbol) {
nodeType = this.checker.getDeclaredTypeOfSymbol(
node.parent.parent.symbol
);
Expand Down
17 changes: 7 additions & 10 deletions src/lib/converter/plugins/SourceLinkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class SourceLinkPlugin extends ConverterComponent {
/**
* List of paths known to be not under git control.
*/
private ignoredPaths: string[] = [];
private ignoredPaths = new Set<string>();

@BindOption("gitRevision")
readonly gitRevision!: string;
Expand Down Expand Up @@ -252,15 +252,13 @@ export class SourceLinkPlugin extends ConverterComponent {
private getRepository(fileName: string): Repository | undefined {
// Check for known non-repositories
const dirName = Path.dirname(fileName);
for (let i = 0, c = this.ignoredPaths.length; i < c; i++) {
if (this.ignoredPaths[i] === dirName) {
return;
}
if (this.ignoredPaths.has(dirName)) {
return;
}

// Check for known repositories
for (const path of Object.keys(this.repositories)) {
if (fileName.substring(0, path.length).toLowerCase() === path) {
if (fileName.toLowerCase().startsWith(path)) {
return this.repositories[path];
}
}
Expand All @@ -279,7 +277,7 @@ export class SourceLinkPlugin extends ConverterComponent {
// No repository found, add path to ignored paths
const segments = dirName.split("/");
for (let i = segments.length; i > 0; i--) {
this.ignoredPaths.push(segments.slice(0, i).join("/"));
this.ignoredPaths.add(segments.slice(0, i).join("/"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without a set, this could add sub-paths multiple times.

@Gerrit0 My understanding of this is that this would always ignore the entire path if a single file is ignored. Eg. if src/components/generated is in the .gitignore, this would ignore everything in src. I suspect that we'd rather just add the directory by itself, and not the path leading up to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, we might want to move this loop to the top of the function — the dirName should probably be checked if any part of it is ignored.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would always ignore the entire path if a single file is ignored. Eg. if src/components/generated is in the .gitignore, this would ignore everything in src.

I read this the same way, and I agree, this definitely looks wrong. I'm surprised there haven't been any bug reports about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a fix for this issue.

}
}

Expand All @@ -298,11 +296,10 @@ export class SourceLinkPlugin extends ConverterComponent {
}
});

for (const key in project.reflections) {
const reflection = project.reflections[key];
for (const reflection of Object.values(project.reflections)) {
if (reflection.sources) {
reflection.sources.forEach((source: SourceReference) => {
if (source.file && source.file.url) {
if (source.file?.url) {
source.url =
source.file.url +
"#" +
Expand Down
7 changes: 4 additions & 3 deletions src/lib/models/reflections/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export function splitUnquotedString(
return [input];
} else {
const remainder = input.substring(closingQuoteIndex + 1);
const result = [input.substring(0, closingQuoteIndex + 1)];
result.push(...splitUnquotedString(remainder, delimiter));
return result;
return [
input.substring(0, closingQuoteIndex + 1),
...splitUnquotedString(remainder, delimiter),
];
}
} else {
return input.split(delimiter);
Expand Down
3 changes: 1 addition & 2 deletions src/lib/output/themes/default/DefaultTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ export class DefaultTheme extends Theme {
event.project.groups.forEach(DefaultTheme.applyGroupClasses);
}

for (const id in event.project.reflections) {
const reflection = event.project.reflections[id];
for (const reflection of Object.values(event.project)) {
fb55 marked this conversation as resolved.
Show resolved Hide resolved
if (reflection instanceof DeclarationReflection) {
DefaultTheme.applyReflectionClasses(reflection);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/output/themes/default/partials/type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getUniquePath(reflection: Reflection): Reflection[] {
function getNamespacedPath(reflection: Reflection): Reflection[] {
const path = [reflection];
let parent = reflection.parent;
while (parent && parent.kindOf(ReflectionKind.Namespace)) {
while (parent?.kindOf(ReflectionKind.Namespace)) {
path.unshift(parent);
parent = parent.parent;
}
Expand Down
19 changes: 9 additions & 10 deletions src/lib/output/themes/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export function stringify(data: unknown) {
export function wbr(str: string): (string | JSX.Element)[] {
// TODO surely there is a better way to do this, but I'm tired.
const ret: (string | JSX.Element)[] = [];
const re = /[\s\S]*?(?:([^_-][_-])(?=[^_-])|([^A-Z])(?=[A-Z][^A-Z]))/g;
const re = /[\s\S]*?(?:[^_-][_-](?=[^_-])|[^A-Z](?=[A-Z][^A-Z]))/g;
let match: RegExpExecArray | null;
let i = 0;
while ((match = re.exec(str))) {
ret.push(match[0]);
ret.push(<wbr />);
ret.push(match[0], <wbr />);
i += match[0].length;
}
ret.push(str.slice(i));
Expand Down Expand Up @@ -70,9 +69,8 @@ export function renderFlags(flags: ReflectionFlags, comment: Comment | undefined
}

export function classNames(names: Record<string, boolean | null | undefined>, extraCss?: string) {
const css = Object.entries(names)
.filter(([, include]) => include)
.map(([key]) => key)
const css = Object.keys(names)
.filter((key) => names[key])
.concat(extraCss || "")
.join(" ")
.trim()
Expand All @@ -83,10 +81,11 @@ export function classNames(names: Record<string, boolean | null | undefined>, ex
export function hasTypeParameters(
reflection: Reflection
): reflection is Reflection & { typeParameters: TypeParameterReflection[] } {
if (reflection instanceof DeclarationReflection || reflection instanceof SignatureReflection) {
return reflection.typeParameters != null && reflection.typeParameters.length > 0;
}
return false;
return (
Gerrit0 marked this conversation as resolved.
Show resolved Hide resolved
(reflection instanceof DeclarationReflection || reflection instanceof SignatureReflection) &&
reflection.typeParameters != null &&
reflection.typeParameters.length > 0
);
}

export function renderTypeParametersSignature(
Expand Down