Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed May 4, 2021
2 parents fd81895 + c6632cb commit 617c2af
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 84 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ updates:
- package-ecosystem: 'npm' # See documentation for possible values
directory: '/' # Location of package manifests
schedule:
interval: 'daily'
target-branch: 'staging'
interval: 'weekly'
target-branch: 'master'
2 changes: 1 addition & 1 deletion docs/codebase/CORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The values of the `directives` object are `DirectiveData`, which contain propert

```ts
interface DirectiveData {
compute: (state: UnknownKV, event?: Event) => any;
compute: (state: KV<unknown>, event?: Event) => any;
value: string;
deps: string[];
}
Expand Down
14 changes: 7 additions & 7 deletions src/component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* istanbul ignore file */

import { COMPONENT_FLAG } from './models/generics';
import compile from './core/compile';
import { directives } from './core/directive';
import reactive from './core/reactive';
import render from './core/render';
import { setElementCustomProp } from './core/utils/elementCustomProp';
import { ASTNode, DirectiveProps, Directives, State, Watchers } from './models/structs';
import { COMPONENT_FLAG } from '@models/generics';
import compile from '@core/compile';
import { directives } from '@core/directive';
import reactive from '@core/reactive';
import render from '@core/render';
import { setElementCustomProp } from '@core/utils/elementCustomProp';
import { ASTNode, DirectiveProps, Directives, State, Watchers } from '@models/structs';

export class Component {
public state: State;
Expand Down
4 changes: 2 additions & 2 deletions src/core/__test__/reactive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringKV } from '../../models/generics';
import { KV } from '../../models/generics';
import { reactive } from '../reactive';

describe('.reactive', () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('.reactive', () => {
const callback = (deps: string[]) => (result = deps[0]);
const proxy = reactive(state, callback);

(proxy.foo as StringKV).bar = '1';
(proxy.foo as KV<string>).bar = '1';

expect(result).toEqual('bar');
});
Expand Down
12 changes: 6 additions & 6 deletions src/core/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
DIRECTIVE_PREFIX,
DIRECTIVE_SHORTHANDS,
FOR_TEMPLATE_FLAG,
} from '../models/generics';
import { ASTNode, ASTNodeType, DirectiveKV, Refs, State } from '../models/structs';
import compute from './utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from './utils/elementCustomProp';
import { eventDirectivePrefixRE, expressionPropRE, hasDirectiveRE } from './utils/patterns';
import removeDupesFromArray from './utils/removeDupesFromArray';
} from '@models/generics';
import { ASTNode, ASTNodeType, DirectiveKV, Refs, State } from '@models/structs';
import compute from '@utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from '@utils/elementCustomProp';
import { eventDirectivePrefixRE, expressionPropRE, hasDirectiveRE } from '@utils/patterns';
import removeDupesFromArray from '@utils/removeDupesFromArray';

export const isListRenderScope = (el: HTMLElement): boolean => {
return el.hasAttribute(`${DIRECTIVE_PREFIX}for`);
Expand Down
16 changes: 8 additions & 8 deletions src/core/directive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DirectiveProps, Directives } from '../models/structs';
import { bindDirective } from './directives/bind';
import { forDirective } from './directives/for';
import { htmlDirective } from './directives/html';
import { modelDirective } from './directives/model';
import { onDirective } from './directives/on';
import { showDirective } from './directives/show';
import { textDirective } from './directives/text';
import { DirectiveProps, Directives } from '@models/structs';
import { bindDirective } from '@directives/bind';
import { forDirective } from '@directives/for';
import { htmlDirective } from '@directives/html';
import { modelDirective } from '@directives/model';
import { onDirective } from '@directives/on';
import { showDirective } from '@directives/show';
import { textDirective } from '@directives/text';

export const directives: Directives = {
BIND: bindDirective,
Expand Down
2 changes: 1 addition & 1 deletion src/core/directives/bind.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DirectiveProps } from '../../models/structs';
import { DirectiveProps } from '@models/structs';

export const formatAcceptableWhitespace = (expression: string): string => {
const whitespaceRE = /\s+/gim;
Expand Down
18 changes: 9 additions & 9 deletions src/core/directives/for.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* istanbul ignore file */

import compile from '../../core/compile';
import { directives } from '../../core/directive';
import render from '../../core/render';
import { COMPONENT_FLAG, DIRECTIVE_PREFIX, FOR_TEMPLATE_FLAG } from '../../models/generics';
import { ASTNode, DirectiveProps } from '../../models/structs';
import adjustDeps from '../utils/adjustDeps';
import computeExpression from '../utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from '../utils/elementCustomProp';
import { expressionPropRE, parenthesisWrapReplaceRE } from '../utils/patterns';
import compile from '@core/compile';
import { directives } from '@core/directive';
import render from '@core/render';
import { COMPONENT_FLAG, DIRECTIVE_PREFIX, FOR_TEMPLATE_FLAG } from '@models/generics';
import { ASTNode, DirectiveProps } from '@models/structs';
import adjustDeps from '@utils/adjustDeps';
import computeExpression from '@utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from '@utils/elementCustomProp';
import { expressionPropRE, parenthesisWrapReplaceRE } from '@utils/patterns';

// This directive is size-based, not content-based, since everything is compiled and rerendered
export const forDirective = ({ el, data, state, node }: DirectiveProps): void => {
Expand Down
16 changes: 8 additions & 8 deletions src/core/directives/html.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import compile from '../../core/compile';
import { directives } from '../../core/directive';
import render from '../../core/render';
import { COMPONENT_FLAG } from '../../models/generics';
import { ASTNode, DirectiveProps } from '../../models/structs';
import adjustDeps from '../utils/adjustDeps';
import { getElementCustomProp, setElementCustomProp } from '../utils/elementCustomProp';
import { hasDirectiveRE } from '../utils/patterns';
import compile from '@core/compile';
import { directives } from '@core/directive';
import render from '@core/render';
import { COMPONENT_FLAG } from '@models/generics';
import { ASTNode, DirectiveProps } from '@models/structs';
import adjustDeps from '@utils/adjustDeps';
import { getElementCustomProp, setElementCustomProp } from '@utils/elementCustomProp';
import { hasDirectiveRE } from '@utils/patterns';

export const htmlDirective = ({ el, data, state, node }: DirectiveProps): void => {
node = node!;
Expand Down
8 changes: 4 additions & 4 deletions src/core/directives/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MODEL_REGISTERED_FLAG } from '../../models/generics';
import { DirectiveData, DirectiveProps, State } from '../../models/structs';
import computeExpression from '../utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from '../utils/elementCustomProp';
import { MODEL_REGISTERED_FLAG } from '@models/generics';
import { DirectiveData, DirectiveProps, State } from '@models/structs';
import computeExpression from '@utils/computeExpression';
import { getElementCustomProp, setElementCustomProp } from '@utils/elementCustomProp';

export const inputCallback = (
el: HTMLInputElement,
Expand Down
4 changes: 2 additions & 2 deletions src/core/directives/on.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DirectiveProps } from '../../models/structs';
import { getElementCustomProp, setElementCustomProp } from '../utils/elementCustomProp';
import { DirectiveProps } from '@models/structs';
import { getElementCustomProp, setElementCustomProp } from '@utils/elementCustomProp';

export const onDirective = ({ el, parts, data, state }: DirectiveProps): void => {
const options: Record<string, boolean> = {};
Expand Down
2 changes: 1 addition & 1 deletion src/core/directives/show.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DirectiveProps } from '../../models/structs';
import { DirectiveProps } from '@models/structs';

export const showDirective = ({ el, data, state }: DirectiveProps): void => {
const ret = data.compute(state);
Expand Down
2 changes: 1 addition & 1 deletion src/core/directives/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DirectiveProps } from '../../models/structs';
import { DirectiveProps } from '@models/structs';

export const textDirective = ({ el, data, state }: DirectiveProps): void => {
const ret = data.compute(state) ?? data.value;
Expand Down
8 changes: 4 additions & 4 deletions src/core/reactive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnknownKV } from '../models/generics';
import { State, Watchers } from '../models/structs';
import { KV } from '@models/generics';
import { State, Watchers } from '@models/structs';

export const arrayEquals = (firstArray: unknown[], secondArray: unknown[]): boolean => {
// Deep Array equality check
Expand All @@ -17,7 +17,7 @@ export const reactive = (
watchers: Watchers = {}
): State => {
const handler = {
get(target: UnknownKV, key: string): unknown {
get(target: KV<unknown>, key: string): unknown {
const ret = target[key];

if (typeof ret === 'object' && ret !== null) {
Expand All @@ -27,7 +27,7 @@ export const reactive = (
return ret;
}
},
set(target: UnknownKV, key: string, value: unknown): boolean {
set(target: KV<unknown>, key: string, value: unknown): boolean {
// Currently double renderes - bad perf
const hasArrayMutationKey = !isNaN(Number(key)) || key === 'length';
const props = hasArrayMutationKey ? [] : [key];
Expand Down
10 changes: 5 additions & 5 deletions src/core/render.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DIRECTIVE_PREFIX, UnknownKV } from '../models/generics';
import { ASTNode, ASTNodeType, Directives } from '../models/structs';
import { DIRECTIVE_PREFIX, KV } from '@models/generics';
import { ASTNode, ASTNodeType, Directives } from '@models/structs';
import { renderDirective } from './directive';
import lazy from './utils/lazy';
import { rawDirectiveSplitRE } from './utils/patterns';
import lazy from '@utils/lazy';
import { rawDirectiveSplitRE } from '@utils/patterns';

const render = (
ast: ASTNode[],
directives: Directives,
state: UnknownKV = {},
state: KV<unknown> = {},
changedProps: string[] = []
): void => {
const legalDirectiveNames = Object.keys(directives);
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/adjustDeps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASTNode } from '../../models/structs';
import { ASTNode } from '@models/structs';
import removeDupesFromArray from './removeDupesFromArray';

export const adjustDeps = (
Expand Down
8 changes: 4 additions & 4 deletions src/core/utils/computeExpression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnknownKV } from '../../models/generics';
import { Refs } from '../../models/structs';
import { KV } from '@models/generics';
import { Refs } from '@models/structs';
import { expressionPropRE } from './patterns';

export const resolveStateInExpression = (
Expand All @@ -22,7 +22,7 @@ export const computeExpression = (
refs: Refs = {},
deps?: string[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): ((state: UnknownKV, event?: Event) => any) => {
): ((state: KV<unknown>, event?: Event) => any) => {
const formattedExpression = `${returnable ? `return ${expression}` : expression}`;
const resolvedExpression = resolveStateInExpression(formattedExpression, deps);
const specialPropertiesNames = ['$state', '$el', '$emit', '$event', '$refs'];
Expand All @@ -39,7 +39,7 @@ export const computeExpression = (
target.dispatchEvent(event);
};

return (state: UnknownKV, event?: Event) => {
return (state: KV<unknown>, event?: Event) => {
try {
const value = state[expression];
if (value) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/patterns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIRECTIVE_PREFIX, DIRECTIVE_SHORTHANDS } from '../../models/generics';
import { DIRECTIVE_PREFIX, DIRECTIVE_SHORTHANDS } from '@models/generics';

// Split directive:modifier.property
export const rawDirectiveSplitRE = (): RegExp => /:|\./gim;
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Exports wrapped in Lucia namespace
import component from './component';
import compile from './core/compile';
import { directives } from './core/directive';
import reactive from './core/reactive';
import render from './core/render';
import computeExpression from './core/utils/computeExpression';
import { getElementCustomProp } from './core/utils/elementCustomProp';
import { COMPONENT_FLAG, DIRECTIVE_PREFIX } from './models/generics';
import compile from '@core/compile';
import { directives } from '@core/directive';
import reactive from '@core/reactive';
import render from '@core/render';
import computeExpression from '@core/utils/computeExpression';
import { getElementCustomProp } from '@core/utils/elementCustomProp';
import { COMPONENT_FLAG, DIRECTIVE_PREFIX } from '@models/generics';

export { component, compile, render, reactive, directives, computeExpression };

Expand Down
4 changes: 1 addition & 3 deletions src/models/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ export enum DIRECTIVE_SHORTHANDS {
'@' = 'on',
':' = 'bind',
}

export type UnknownKV = Record<string, unknown>;
export type StringKV = Record<string, string>;
export type KV<T> = Record<string, T>;
14 changes: 7 additions & 7 deletions src/models/structs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { UnknownKV } from './generics';
import { KV } from './generics';

export type Directives = Record<string, (props: DirectiveProps) => void>;
export type Watchers = Record<string, () => void>;
export type Refs = Record<string, HTMLElement>;
export type State = UnknownKV;
export type Directives = KV<(props: DirectiveProps) => void>;
export type Watchers = KV<() => void>;
export type Refs = KV<HTMLElement>;
export type State = KV<unknown>;

export type DirectiveKV = Record<string, DirectiveData>;
export type DirectiveKV = KV<DirectiveData>;

export interface DirectiveData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
compute: (state: UnknownKV, event?: Event) => any;
compute: (state: KV<unknown>, event?: Event) => any;
value: string;
deps: string[];
}
Expand Down

0 comments on commit 617c2af

Please sign in to comment.