Skip to content
Open
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
34 changes: 34 additions & 0 deletions src/ignore-markers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export type IgnoreCommentKind = "ignore" | "ignore-start" | "ignore-end";
export type IgnoreCommentWrapper = "blade" | "html";

export function getIgnoreCommentKindFromPayload(payload: string): IgnoreCommentKind | null {
switch (payload.trim().toLowerCase()) {
case "format-ignore":
case "prettier-ignore":
return "ignore";
case "format-ignore-start":
case "prettier-ignore-start":
return "ignore-start";
case "format-ignore-end":
case "prettier-ignore-end":
return "ignore-end";
default:
return null;
}
}

export function getIgnoreCommentKindFromCommentText(
text: string,
wrapper: IgnoreCommentWrapper,
): IgnoreCommentKind | null {
const payload =
wrapper === "blade"
? (text.match(/^\{\{--\s*([\s\S]*?)\s*--\}\}$/s)?.[1] ?? null)
: (text.match(/^<!--\s*([\s\S]*?)\s*-->$/s)?.[1] ?? null);

if (payload === null) {
return null;
}

return getIgnoreCommentKindFromPayload(payload);
}
145 changes: 145 additions & 0 deletions src/lexer/ignore-ranges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { getIgnoreCommentKindFromCommentText } from "../ignore-markers.js";
import { Directives } from "./directives.js";
import { Lexer, type LexerRawBlockConfig } from "./lexer.js";
import { State, type IgnoreRangeRegion, type IgnoreRangeResumeState } from "./types.js";

function isTagIgnoreState(state: State): boolean {
switch (state) {
case State.TagName:
case State.BeforeAttrName:
case State.AttrName:
case State.AfterAttrName:
case State.BeforeAttrValue:
case State.AttrValueUnquoted:
return true;
default:
return false;
}
}

function isIgnoreMarkerAllowed(wrapper: "blade" | "html", originState: State): boolean {
if (originState === State.AttrValueQuoted) {
return false;
}

if (wrapper === "html") {
return originState !== State.RawText;
}

return true;
}

class IgnoreRangeCollectorImpl {
private ranges: IgnoreRangeRegion[] = [];
private depth = 0;
private outerStart = -1;

constructor(private readonly source: string) {}

handleBladeComment(
start: number,
end: number,
originState: State,
tagStart: number | null,
resume: IgnoreRangeResumeState,
): void {
this.handleComment("blade", start, end, originState, tagStart, resume);
}

handleHtmlComment(
start: number,
end: number,
originState: State,
tagStart: number | null,
resume: IgnoreRangeResumeState,
): void {
this.handleComment("html", start, end, originState, tagStart, resume);
}

finish(resume: IgnoreRangeResumeState, eof: number): IgnoreRangeRegion[] {
if (this.depth > 0 && this.outerStart >= 0) {
this.ranges.push({
start: this.outerStart,
end: eof,
resume,
});
this.depth = 0;
this.outerStart = -1;
}

return this.ranges;
}

private handleComment(
wrapper: "blade" | "html",
start: number,
end: number,
originState: State,
tagStart: number | null,
resume: IgnoreRangeResumeState,
): void {
if (!isIgnoreMarkerAllowed(wrapper, originState)) {
return;
}

const kind = getIgnoreCommentKindFromCommentText(this.source.slice(start, end), wrapper);
if (!kind) {
return;
}

if (kind === "ignore-start") {
if (this.depth === 0) {
this.outerStart =
isTagIgnoreState(originState) && tagStart !== null && tagStart >= 0 ? tagStart : start;
}
this.depth++;
return;
}

if (this.depth === 0) {
return;
}

this.depth--;
if (this.depth !== 0) {
return;
}

this.ranges.push({
start: this.outerStart >= 0 ? this.outerStart : start,
end,
resume,
});
this.outerStart = -1;
}
}

export function collectIgnoreRanges(
source: string,
directives?: Directives,
rawBlockConfig?: LexerRawBlockConfig,
): IgnoreRangeRegion[] {
const collector = new IgnoreRangeCollectorImpl(source);
new Lexer(source, directives, {
...rawBlockConfig,
ignoreRangeCollector: collector,
}).tokenize();

return collector.finish(
{
state: State.Data,
returnState: State.Data,
rawtextTagName: "",
currentTagName: "",
isClosingTag: false,
continuedTagName: false,
inXmlDeclaration: false,
verbatim: false,
verbatimReturnState: null,
phpBlock: false,
phpTag: false,
attrPhpDirectiveDepth: 0,
},
source.length,
);
}
1 change: 1 addition & 0 deletions src/lexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
tokenContent,
reconstructFromTokens,
} from "./lexer.js";
export { collectIgnoreRanges } from "./ignore-ranges.js";
export {
Directives,
getDirectivePhpWrapperKind,
Expand Down
Loading
Loading