Add parsePathPatternsFile method for parsing .gitignore
MODIFY
packages/@romejs/path-match/index.ts
packages/@romejs/path-match/index.ts
*/
import {PathPatternNode} from './types';
-import {parsePattern} from './parse';
+import {parsePattern, parsePatternsFile} from './parse';
import match from './match';
import {AbsoluteFilePath, PathSegments} from '@romejs/path';
@@ -14,7 +14,10 @@ export type PathPatterns = Array<PathPatternNode>;
export type PathPattern = PathPatternNode;
-export {parsePattern as parsePathPattern};
+export {
+ parsePattern as parsePathPattern,
+ parsePatternsFile as parsePathPatternsFile,
+};
export {stringifyPathPattern} from './stringify';

MODIFY
packages/@romejs/path-match/match.ts
packages/@romejs/path-match/match.ts
@@ -51,7 +51,6 @@ function matchSegment(path: string, patternSeg: PatternSegmentNode): boolean {
}
// We consumed the whole buffer and nothing matched
-
return false;
}
@@ -152,7 +151,7 @@ export default function match(
// When given a wildcard segment, keep popping off all the path segments until we find one that matches the next pattern segment
if (patternSeg.type === 'WildcardSegment') {
const nextPattern = patternSegs[i + 1];
- while (!matchSegment(pathSegs[0], nextPattern)) {
+ while (pathSegs.length > 0 && !matchSegment(pathSegs[0], nextPattern)) {
pathSegs.shift();
}
continue;

MODIFY
packages/@romejs/path-match/parse.ts
packages/@romejs/path-match/parse.ts
@@ -206,11 +206,8 @@ const createPathMatchParser = createParser((ParserCore) =>
}
// TODO Remove duplicate wildcard segments
-
// - Multiple WildcardSegment
-
// - Wildcard next to a WildcardSegment
-
// Remove all wildcard-only segments from end
while (this.isWildcardOnlySegment(normalized[normalized.length - 1])) {
normalized.pop();
@@ -219,6 +216,14 @@ const createPathMatchParser = createParser((ParserCore) =>
return normalized;
}
+ parsePatternsFile(): Array<PathPatternNode> {
+ const patterns: Array<PathPatternNode> = [];
+ while (!this.matchToken('EOF')) {
+ patterns.push(this.parsePattern());
+ }
+ return patterns;
+ }
+
parsePattern(): PathPatternNode {
const startPos = this.getPosition();
const segments: PatternSegments = [];
@@ -230,7 +235,6 @@ const createPathMatchParser = createParser((ParserCore) =>
}
// Get a trailing comment
-
let comment = '';
if (this.eatToken('Hash')) {
comment = this.getRawInput(
@@ -297,3 +301,10 @@ export function parsePattern(opts: PathMatchParserOptions): PathPatternNode {
const parser = createPathMatchParser(opts, 'pattern');
return parser.parsePattern();
}
+
+export function parsePatternsFile(
+ opts: PathMatchParserOptions,
+): Array<PathPatternNode> {
+ const parser = createPathMatchParser(opts, 'pattern');
+ return parser.parsePatternsFile();
+}

MODIFY
packages/@romejs/project/load.ts
packages/@romejs/project/load.ts
@@ -18,7 +18,7 @@ import {
ProjectConfigObjects,
ProjectConfigTarget,
} from './types';
-import {parsePathPattern} from '@romejs/path-match';
+import {parsePathPatternsFile} from '@romejs/path-match';
import {
arrayOfPatterns,
arrayOfStrings,
@@ -28,10 +28,8 @@ import {
} from './utils';
import {ConsumeJSONResult, consumeJSONExtra} from '@romejs/codec-json';
import {AbsoluteFilePath, AbsoluteFilePathSet} from '@romejs/path';
-import {ob1Add, ob1Coerce1, ob1Inc, ob1Number0} from '@romejs/ob1';
import {existsSync, lstatSync, readFileTextSync, readdirSync} from '@romejs/fs';
import crypto = require('crypto');
-
import {ROME_CONFIG_PACKAGE_JSON_FIELD} from './constants';
import {parseSemverRange} from '@romejs/codec-semver';
import {descriptions} from '@romejs/diagnostics';
@@ -92,28 +90,11 @@ export function loadCompleteProjectConfig(
if (existsSync(possiblePath)) {
const file = readFileTextSync(possiblePath);
- const lines: Array<string> = file.split('\n');
-
- let index = ob1Number0;
consumer.handleThrownDiagnostics(() => {
- const patterns = lines.map((line, i) => {
- const pattern = parsePathPattern({
- input: line,
+ const patterns = parsePathPatternsFile({
+ input: file,
path: possiblePath,
- offsetPosition: {
- index,
- line: ob1Coerce1(i),
- column: ob1Number0,
- },
- });
-
- index = ob1Add(index, line.length);
-
- // Newline char
- index = ob1Inc(index);
-
- return pattern;
});
// TODO: Maybe these are useful in other places?

Add parsePathPatternsFile method for parsing .gitignore