Skip to content

Commit

Permalink
refactor: lime primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Dec 13, 2023
1 parent ab288bf commit a17b6dc
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 111 deletions.
8 changes: 7 additions & 1 deletion appserver/appserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { type Channel } from "./channel.ts";
import { type Module } from "./module.ts";

export class AppServer {
static default = Symbol("default");

runMode: runModes.RunMode;
events: typeof events;
di: typeof di;
Expand All @@ -34,6 +36,10 @@ export class AppServer {
this.channels.set(name, channel);
}

execute(_options: unknown) {
setAsDefaultAppServer() {
this.di.register(AppServer.default, this);
}

// execute(_options: unknown) {
// }
}
16 changes: 8 additions & 8 deletions appserver/version-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
import * as runtime from "../standards/runtime.ts";
import { semver } from "./deps.ts";

export function compareSemanticVersions(
export const compareSemanticVersions = (
currentVersion: semver.SemVer,
targetVersion: semver.SemVerRange | semver.SemVer,
) {
) => {
if (semver.isSemVerRange(targetVersion)) {
return semver.testRange(currentVersion, targetVersion);
}

return !semver.gte(currentVersion, targetVersion);
}
};

export function compareTextVersions(
export const compareTextVersions = (
currentVersion: string,
targetVersion: string,
) {
) => {
const currentSemanticVersion = semver.parse(currentVersion);
const targetSemanticVersion = semver.parseRange(targetVersion);

return compareSemanticVersions(currentSemanticVersion, targetSemanticVersion);
}
};

export function checkMinDenoVersion(minimumVersion: string) {
export const checkMinDenoVersion = (minimumVersion: string) => {
return compareTextVersions(runtime.version.runtime, minimumVersion);
}
};
4 changes: 2 additions & 2 deletions collector/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface CollectExportsOptions {
ignoreFilePattern?: RegExp;
}

export async function collectExports(options: CollectExportsOptions) {
export const collectExports = async (options: CollectExportsOptions) => {
const ignoreFilePattern = options.ignoreFilePattern ??
patterns.JS_TEST_FILE_PATTERN;

Expand Down Expand Up @@ -76,4 +76,4 @@ export async function collectExports(options: CollectExportsOptions) {
}

return exports;
}
};
29 changes: 17 additions & 12 deletions collector/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ const PLACEHOLDER_SUFFIX = "__//!!##";
/**
* Import specifiers must have forward slashes
*/
function toImportSpecifier(file: string) {
const toImportSpecifier = (file: string) => {
const specifier = posix.normalize(file).replace(/\\/g, "/");

if (!specifier.startsWith(".")) {
return `./${specifier}`;
}

return specifier;
}
};

// Create a valid JS identifier out of the project relative specifier.
// Note that we only need to deal with strings that _must_ have been
// valid file names in Windows, macOS and Linux and every identifier we
// create here will be prefixed with at least one "$". This greatly
// simplifies the invalid characters we have to account for.
export function specifierToIdentifier(specifier: string, used: Set<string>) {
export const specifierToIdentifier = (specifier: string, used: Set<string>) => {
// specifier = specifier.replace(/^(?:\.\/pkg)\//, "");
const ext = path.extname(specifier);
if (ext) {
Expand Down Expand Up @@ -62,29 +62,32 @@ export function specifierToIdentifier(specifier: string, used: Set<string>) {
if (used.has(ident)) {
let check = ident;
let i = 1;

while (used.has(check)) {
check = `${ident}_${i++}`;
}

ident = check;
}

used.add(ident);

return ident;
}
};

const getSortFn = function () {
const getSortFn = () => {
const naturalCollator = new Intl.Collator(undefined, { numeric: true });

return naturalCollator.compare;
};

const placeholder = function (text: string) {
const placeholder = (text: string) => {
return `${PLACEHOLDER_PREFIX}${text}${PLACEHOLDER_SUFFIX}`;
};

export async function writeManifestToString(
export const writeManifestToString = async (
collection: Array<[string, Array<[string, unknown]>]>,
) {
) => {
const sortFn = getSortFn();

const used = new Set<string>();
Expand All @@ -102,7 +105,7 @@ export async function writeManifestToString(
`import * as ${IMPORT_PREFIX}${identifier} from "${specifier}";`,
);

const ref = function (target: unknown) {
const ref = (target: unknown) => {
const name = (target as { name: string }).name;

return placeholder(`${IMPORT_PREFIX}${identifier}.${name}`);
Expand Down Expand Up @@ -132,9 +135,11 @@ export const manifest = ${manifestSerialized};
const manifestStr = await formatter.format(output);

return manifestStr;
}
};

export async function buildManifest(options: collector.CollectExportsOptions) {
export const buildManifest = async (
options: collector.CollectExportsOptions,
) => {
const collection = await collector.collectExports(options);

const manifestStr = await writeManifestToString(collection);
Expand All @@ -152,4 +157,4 @@ export async function buildManifest(options: collector.CollectExportsOptions) {
`%cThe manifest file has been generated for ${exportCount} exports in ${exportModules.length} modules.`,
"color: blue",
);
}
};
24 changes: 18 additions & 6 deletions collector/validator-identifier/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const nonASCIIidentifier = new RegExp(
// This has a complexity linear to the value of the code. The
// assumption is that looking up astral identifier characters is
// rare.
function isInAstralSet(code: number, set: readonly number[]): boolean {
const isInAstralSet = (code: number, set: readonly number[]) => {
let pos = 0x10000;

for (let i = 0, length = set.length; i < length; i += 2) {
Expand All @@ -39,7 +39,7 @@ function isInAstralSet(code: number, set: readonly number[]): boolean {
}

return false;
}
};

// These are a run-length and offset-encoded representation of the
// >0xffff code points that are a valid part of identifiers. The
Expand Down Expand Up @@ -902,53 +902,65 @@ const astralIdentifierCodes = [

// Test whether a given character code starts an identifier.

export function isIdentifierStart(code: number): boolean {
export const isIdentifierStart = (code: number) => {
if (code < charCodes.uppercaseA) {
return code === charCodes.dollarSign;
}

if (code <= charCodes.uppercaseZ) {
return true;
}

if (code < charCodes.lowercaseA) {
return code === charCodes.underscore;
}

if (code <= charCodes.lowercaseZ) {
return true;
}

if (code <= 0xffff) {
return (
code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code))
);
}

return isInAstralSet(code, astralIdentifierStartCodes);
}
};

// Test whether a given character is part of an identifier.

export function isIdentifierChar(code: number): boolean {
export const isIdentifierChar = (code: number) => {
if (code < charCodes.digit0) {
return code === charCodes.dollarSign;
}

if (code < charCodes.colon) {
return true;
}

if (code < charCodes.uppercaseA) {
return false;
}

if (code <= charCodes.uppercaseZ) {
return true;
}

if (code < charCodes.lowercaseA) {
return code === charCodes.underscore;
}

if (code <= charCodes.lowercaseZ) {
return true;
}

if (code <= 0xffff) {
return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
}

return (
isInAstralSet(code, astralIdentifierStartCodes) ||
isInAstralSet(code, astralIdentifierCodes)
);
}
};
12 changes: 6 additions & 6 deletions di/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import {
type ServiceValue,
} from "./primitives.ts";

function getFunctionParametersFromString(fnSerialized: string) {
const getFunctionParametersFromString = (fnSerialized: string) => {
const match = fnSerialized.match(/(?:function.*?\(|\()(.*?)(?:\)|=>)/);

if (match && match[1]) {
return match[1].split(",").map((p) => p.trim());
}

return [];
}
};

function getFunctionParameters(fn: AnonymousFunction) {
const getFunctionParameters = (fn: AnonymousFunction) => {
return getFunctionParametersFromString(fn.toString());
}
};

// function analyzeParameter(param: string) {
// const analyzeParameter = (param: string) => {
// const decoratorMatch = param.match(/@(\w+)\(([^)]+)\)/);
// const defaultValueMatch = param.match(/(.*?)=(.*)/);
// const isRestOrSpread = param.startsWith("...");
Expand Down Expand Up @@ -53,7 +53,7 @@ function getFunctionParameters(fn: AnonymousFunction) {
// decorators: decorators,
// };
// }
// }
// };

export const invoke = <
T extends AnonymousFunction,
Expand Down
2 changes: 1 addition & 1 deletion functions/fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bdd.describe("cool/functions/fn", () => {
const spyFn = mock.spy();

const fns = fn<Result<string>>(
function () {
() => {
spyFn();

return Ok("Testing");
Expand Down
4 changes: 2 additions & 2 deletions jsx-runtime.test/root.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ bdd.describe("cool/jsx-runtime", () => {
const spyFn = mock.spy();

// deno-lint-ignore no-explicit-any
function hook(vnode: any) {
const hook = (vnode: any) => {
if (vnode.props["lime-hack"]) {
spyFn();
}
}
};

// @ts-ignore typescript don't recognize this
jsxRuntimeInternal.options.tagHelperHook = hook;
Expand Down
12 changes: 6 additions & 6 deletions jsx-runtime.test/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

// @ts-nocheck: JSX.IntrinsicElements issue.

export function SubComponent() {
export const SubComponent = () => {
return <div>sub component</div>;
}
};

export interface ComponentProps {
foo: string;
}

export function Component(props: ComponentProps) {
export const Component = (props: ComponentProps) => {
return (
<div>
hello {props.foo}
<br />
<SubComponent />
</div>
);
}
};

export function Root() {
export const Root = () => {
return <Component foo="bar" lime-hack />;
}
};
21 changes: 16 additions & 5 deletions jsx-runtime/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
const ENCODED_ENTITIES = /["&<]/;

/** @param {string} str */
export function encodeEntities(str) {
export const encodeEntities = (str) => {
// Skip all work for strings with no entities needing encoding:
if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) return str;
if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) {
return str;
}

let last = 0,
i = 0,
Expand All @@ -33,12 +35,21 @@ export function encodeEntities(str) {
default:
continue;
}

// Append skipped/buffered characters and the encoded entity:
if (i !== last) out += str.slice(last, i);
if (i !== last) {
out += str.slice(last, i);
}

out += ch;

// Start the next seek/buffer after the entity's offset:
last = i + 1;
}
if (i !== last) out += str.slice(last, i);

if (i !== last) {
out += str.slice(last, i);
}

return out;
}
};
Loading

0 comments on commit a17b6dc

Please sign in to comment.