MCPcopy Create free account
hub / github.com/Effect-TS/effect / compileDocker

Function compileDocker

packages/effect/src/IgnoreFile.ts:833–916  ·  view source on GitHub ↗
(pattern: string)

Source from the content-addressed store, hash-verified

831// `Pattern.compile` from moby/patternmatcher, with the generated regular
832// expression replaced by tokens and the reference's string fast paths kept.
833const compileDocker = (pattern: string): ((path: string) => boolean) | string => {
834 const chars = Array.from(pattern)
835 if (!isGoMatchSyntax(chars)) {
836 return "syntax error in pattern"
837 }
838 let kind: "exact" | "prefix" | "suffix" | "tokens" = "exact"
839 const tokens: Array<DockerToken> = []
840 // `text/scanner` skips a leading BOM; the string tests keep it
841 for (let i = chars[0] === "\uFEFF" ? 1 : 0, iteration = 0; i < chars.length; iteration++) {
842 const char = chars[i++]
843 if (char === "*") {
844 if (chars[i] === "*") {
845 i++
846 if (chars[i] === "/") {
847 i++
848 }
849 if (i === chars.length) {
850 if (kind === "exact") {
851 kind = "prefix"
852 } else {
853 tokens.push({ _tag: "StarAll" })
854 kind = "tokens"
855 }
856 } else {
857 tokens.push({ _tag: "GroupStart" }, { _tag: "GroupBody" })
858 kind = "tokens"
859 }
860 if (iteration === 0) {
861 kind = "suffix"
862 }
863 } else {
864 tokens.push({ _tag: "Star" })
865 kind = "tokens"
866 }
867 } else if (char === "?") {
868 tokens.push({ _tag: "Any" })
869 kind = "tokens"
870 } else if (char === "\\") {
871 const next = chars[i++]
872 if (next === undefined) {
873 // the generated expression would end in a lone backslash
874 return `a trailing backslash is regular expression syntax in Docker; ` +
875 `escape it as "\\\\" to match a literal backslash`
876 }
877 const error = escapeError(next)
878 if (error !== undefined) {
879 return error
880 }
881 tokens.push({ _tag: "Char", code: codePoint(next) })
882 kind = "tokens"
883 } else if (char === "[") {
884 const parsed = parseDockerClass(chars, i - 1)
885 if (typeof parsed === "string") {
886 return parsed
887 }
888 tokens.push(parsed.value)
889 i = parsed.end + 1
890 kind = "tokens"

Callers 1

parseDockerignoreFunction · 0.85

Calls 8

isGoMatchSyntaxFunction · 0.85
escapeErrorFunction · 0.85
codePointFunction · 0.85
parseDockerClassFunction · 0.85
matchTokensFunction · 0.85
fromMethod · 0.80
sliceMethod · 0.80
pushMethod · 0.65

Tested by

no test coverage detected