Skip to content
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
4 changes: 2 additions & 2 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Addon Stack <addonbonedev@gmail.com> <191148085+addon-stack@users.noreply.github.com>
Addon Stack <addonbonedev@gmail.com> <addon-stack@users.noreply.github.com>
Addon Stack <191148085+addon-stack@users.noreply.github.com> <addonbonedev@gmail.com>
Addon Stack <addon-stack@users.noreply.github.com> <addonbonedev@gmail.com>
2 changes: 1 addition & 1 deletion .release-it.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function getContributors() {
}

const range = fromTag ? `${fromTag}..HEAD` : "";
const cmd = `git shortlog -sne ${range}`.trim();
const cmd = `git shortlog --use-mailmap -sne ${range}`.trim();
const out = execSync(cmd, {encoding: "utf8"}).trim();

if (!out) {
Expand Down
860 changes: 470 additions & 390 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@
},
"dependencies": {
"@addon-core/browser": "^0.2.1",
"@addon-core/inject-script": "^0.3.0",
"@addon-core/inject-script": "^0.3.1",
"@addon-core/storage": "^0.4.0",
"@rsdoctor/rspack-plugin": "~1.1.11",
"@rspack/cli": "^1.4.11",
"@rspack/core": "^1.4.11",
"@svgr/webpack": "^8.1.0",
"await-lock": "^3.0.0",
"c12": "^3.0.3",
"cac": "^6.7.14",
"consola": "^3.4.2",
Expand Down
1 change: 1 addition & 0 deletions src/cli/bundler/utils/optimization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const getEntryDirs = (entry: string | string[]): string[] => {
return entries.flatMap(v => [
path.join("node_modules", PackageName, "dist", "entry", v),
path.join("addonbone", "dist", "entry", v), // TODO: Only for test. Remove this
path.join("addon-bone", "dist", "entry", v), // TODO: Only for test. Remove this
]);
};

Expand Down
2 changes: 1 addition & 1 deletion src/cli/entrypoint/file/ExpressionFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
jest.mock("./resolvers", () => require("./resolvers/tests/resolvers.mock"));
jest.mock("./resolvers", () => jest.requireActual("./resolvers/tests/resolvers.mock"));
import ExpressionFile from "./ExpressionFile";

const fixtures = path.resolve(__dirname, "tests", "fixtures", "expression");
Expand Down
54 changes: 31 additions & 23 deletions src/cli/entrypoint/file/ExpressionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class ExpressionFile extends SourceFile {
private readonly objectParser: ObjectParser;
private readonly classParser: ClassParser;
private readonly jsDocParser: JSDocParser;

constructor(filePath: string) {
super(filePath);

Expand Down Expand Up @@ -105,6 +106,8 @@ export default class ExpressionFile extends SourceFile {
// Get the return type of the method
const returnType = this.getTypeFromMethodReturn(prop);
if (returnType) return returnType;
} else if (ts.isShorthandPropertyAssignment(prop)) {
return this.resolveTypeFromShorthand(prop.name.text);
}
}
}
Expand All @@ -127,27 +130,7 @@ export default class ExpressionFile extends SourceFile {

return this.signatureBuilder.buildFunctionType(sig);
} else if (ts.isShorthandPropertyAssignment(prop)) {
const name = prop.name.text;
const variable = this.getVariables().get(name);

if (variable) {
const val = variable.value;
switch (typeof val) {
case "string":
return "string";
case "number":
return "number";
case "boolean":
return "boolean";
case "object":
if (val === null) return "null";
if (Array.isArray(val)) return "array";
return "object";
default:
return "any";
}
}
return "any";
return this.resolveTypeFromShorthand(prop.name.text);
}
}
return undefined;
Expand Down Expand Up @@ -298,15 +281,40 @@ export default class ExpressionFile extends SourceFile {
return this.signatureBuilder.formatMembers(members);
}

// For literals or other expressions, infer a simple type
if (ts.isLiteralExpression(node) || ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node)) {
return this.objectParser.inferTypeFromExpression(node as ts.Expression);
}

// Fallback: undefined
return undefined;
}

private resolveTypeFromShorthand(name: string): string | undefined {
const varDecl = this.nodeFinder.findVariableDeclaration(name);
if (varDecl && varDecl.initializer) {
const t = this.getTypeFromInitializer(varDecl.initializer);
if (t) return t;
}
const variable = this.getVariables().get(name);
if (variable) {
const val = variable.value;
switch (typeof val) {
case "string":
return "string";
case "number":
return "number";
case "boolean":
return "boolean";
case "object":
if (val === null) return "null";
if (Array.isArray(val)) return "array";
return "object";
default:
return "any";
}
}
return "any";
}

/**
* Extracts the type of the return value of a method, taking into account JSDoc annotations.
*
Expand Down
96 changes: 1 addition & 95 deletions src/cli/entrypoint/file/OptionFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1 @@
import path from "path";
jest.mock("./resolvers", () => require("./resolvers/tests/resolvers.mock"));
import OptionFile from "./OptionFile";

const fixtures = path.resolve(__dirname, "tests", "fixtures");

describe("background", () => {
test("background with definition function", () => {
const filename = path.join(fixtures, "background", "definition-only.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
includeBrowser: ["firefox"],
});
});

test("background with default function and export properties", () => {
const filename = path.join(fixtures, "background", "default-function.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "excludeApps"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["edge"],
excludeApps: ["my_test_app"],
});
});

test("background with combined definition and export properties", () => {
const filename = path.join(fixtures, "background", "definition-with-named-exports.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["edge"],
includeBrowser: ["firefox"],
});
});

test("background with default object and export properties", () => {
const filename = path.join(fixtures, "background", "default-object.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["opera"],
includeBrowser: ["chrome"],
});
});

test("background with default type name", () => {
const filename = path.join(fixtures, "background", "default-object-assertion.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["safari"],
includeBrowser: ["chromium"],
});
});

test("background with default satisfies", () => {
const filename = path.join(fixtures, "background", "default-object-satisfies.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
includeBrowser: ["chromium"],
});
});
});
import "./tests/background";
11 changes: 9 additions & 2 deletions src/cli/entrypoint/file/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class EntryFile {
case ts.SyntaxKind.ObjectLiteralExpression:
return (node as ts.ObjectLiteralExpression).properties
.filter(
(property): property is ts.PropertyAssignment =>
(property): property is ts.PropertyAssignment | ts.ShorthandPropertyAssignment =>
ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)
)
.reduce(
Expand All @@ -264,8 +264,15 @@ export default class EntryFile {
}

const key = (property.name as ts.Identifier | ts.StringLiteral).text;
let value = this.parseNode(property.initializer);

// Determine the initializer node: for shorthand, use the identifier itself
const initNode = ts.isShorthandPropertyAssignment(property)
? (property.name as ts.Identifier)
: (property.initializer as ts.Expression | undefined);

let value = this.parseNode(initNode);

// If value is still just an identifier name, resolve from collected variables
if (typeof value === "string" && this.variables?.has(value)) {
value = this.variables?.get(value)?.value;
}
Expand Down
11 changes: 10 additions & 1 deletion src/cli/entrypoint/file/injectors/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Command, Mode, PackageName} from "@typing/app";
import {Browser} from "@typing/browser";
import {RelayMethod} from "@typing/relay";
import {ContentScriptAppend, ContentScriptDeclarative} from "@typing/content";
import {ContentScriptAppend, ContentScriptDeclarative, ContentScriptMarker} from "@typing/content";
import {OffscreenReason} from "@typing/offscreen";

import {Injector} from "../types";
Expand Down Expand Up @@ -54,6 +54,15 @@ export default (): Injector[] => {
});
});

Object.entries(ContentScriptMarker).forEach(([key, value]) => {
resolvers.push({
from: PackageName,
target: "ContentScriptMarker",
name: key,
value,
});
});

Object.entries(RelayMethod).forEach(([key, value]) => {
resolvers.push({
from: PackageName,
Expand Down
113 changes: 113 additions & 0 deletions src/cli/entrypoint/file/tests/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import path from "path";
import OptionFile from "../OptionFile";

jest.mock("../resolvers", () => jest.requireActual("../resolvers/tests/resolvers.mock"));

const fixtures = path.resolve(__dirname, "fixtures");

describe("background", () => {
test("background with definition function", () => {
const filename = path.join(fixtures, "background", "definition-only.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
includeBrowser: ["firefox"],
});
});

test("background with default function and export properties", () => {
const filename = path.join(fixtures, "background", "default-function.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "excludeApps"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["edge"],
excludeApps: ["my_test_app"],
});
});

test("background with combined definition and export properties", () => {
const filename = path.join(fixtures, "background", "definition-with-named-exports.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["edge"],
includeBrowser: ["firefox"],
});
});

test("background with default object and export properties", () => {
const filename = path.join(fixtures, "background", "default-object.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["opera"],
includeBrowser: ["chrome"],
});
});

test("background with default type name", () => {
const filename = path.join(fixtures, "background", "default-object-assertion.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
excludeBrowser: ["safari"],
includeBrowser: ["chromium"],
});
});

test("background with default satisfies", () => {
const filename = path.join(fixtures, "background", "default-object-satisfies.ts");

const options = OptionFile.make(filename)
.setProperties(["persistent", "excludeBrowser", "includeBrowser"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
persistent: true,
includeBrowser: ["chromium"],
});
});

test("background with definition shorthand", () => {
const filename = path.join(fixtures, "background", "definition-shorthand.ts");

const options = OptionFile.make(filename)
.setProperties(["permissions", "persistent", "excludeBrowser", "includeBrowser", "excludeApp"])
.setDefinition("defineBackground")
.getOptions();

expect(options).toEqual({
permissions: ["storage", "tabs"],
persistent: true,
excludeBrowser: ["edge"],
includeBrowser: ["firefox"],
excludeApp: ["my_test_app"],
});
});
});
Loading