Skip to content

Commit

Permalink
fix: Add shortcuts to sourcemap API
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Feb 28, 2025
1 parent 8bdf4c8 commit 084e073
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function inlineConditions(
vars: Record<string, unknown>,
ifTag: IfTag,
) {
const {conditions} = this.settings;
const {conditions = true} = this.settings;

let ifCon = null;

Expand Down
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {applyCycles} from './cycles';
import {applyConditions} from './conditions';
import {codeUtils} from './utils';

export type {LiquidContext} from './types';

export {evaluate} from './syntax/evaluate';
export {SourceMap} from './sourcemap';
export {composeFrontMatter, extractFrontMatter} from './frontmatter';
Expand Down Expand Up @@ -81,16 +83,12 @@ export function liquidDocument(
return composedFrontmatter + liquidedResult;
}

export function createContext(logger: Logger, settings?: Partial<LiquidSettings>): LiquidContext {
export function createContext(
logger: Logger,
settings: Partial<LiquidSettings> = {},
): LiquidContext {
return {
logger,
settings: {
cycles: true,
conditions: true,
conditionsInCode: false,
substitutions: true,
keepNotVar: false,
...settings,
},
settings,
};
}
9 changes: 9 additions & 0 deletions src/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export class SourceMap {
return dump;
}

delete(start: number, content: string) {
const lines = this.lines(content);
const point = this.location(start, content.length, lines);

this.patch({
delete: [point],
});
}

patch(tx: Partial<SourceMapTx>) {
const flow = {
offset: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/substitutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function applySubstitutions(
vars: Record<string, unknown>,
) {
const {path} = this;
const {keepNotVar} = this.settings;
const {keepNotVar = false} = this.settings;

if (isSingleVariable(input)) {
const match = input.match(singleVariableRe);
Expand Down
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export type Logger = {
};

export type LiquidSettings = {
conditions: boolean | 'strict';
conditionsInCode: boolean;
cycles: boolean;
substitutions: boolean;
keepNotVar: boolean;
conditions?: boolean | 'strict';
conditionsInCode?: boolean;
cycles?: boolean;
substitutions?: boolean;
keepNotVar?: boolean;
};

export type LiquidContext = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getPreparedLeftContent({content, tagStartPos, tagContent}: Opts)
const fence = '```';

export function codeUtils(this: LiquidContext) {
const {substitutions} = this.settings;
const {substitutions = true} = this.settings;
const codes: string[] = [];

const find = (open: string, close: string, string: string, index: number) => {
Expand Down

0 comments on commit 084e073

Please sign in to comment.