path-match: Rename Pattern node to PathPattern and eliminate redundant names array
MODIFY
packages/@romejs/path-match/index.ts
packages/@romejs/path-match/index.ts
* LICENSE file in the root directory of this source tree.
*/
-import {PatternNode} from './types';
+import {PathPatternNode} from './types';
import {parsePattern} from './parse';
import match from './match';
import {PathSegments, AbsoluteFilePath} from '@romejs/path';
-export type PathPatterns = Array<PatternNode>;
-export type PathPattern = PatternNode;
+export type PathPatterns = Array<PathPatternNode>;
+export type PathPattern = PathPatternNode;
export {parsePattern as parsePathPattern};
@@ -19,7 +19,7 @@ export {stringifyPathPattern} from './stringify';
export function matchPath(
path: AbsoluteFilePath,
- patternNode: PatternNode,
+ patternNode: PathPatternNode,
cwdSegs?: PathSegments,
): boolean {
const matches = match(path.getSegments(), patternNode, cwdSegs);

MODIFY
packages/@romejs/path-match/match.ts
packages/@romejs/path-match/match.ts
import {
PatternPartNode,
- PatternNode,
+ PathPatternNode,
PatternSegments,
PatternSegmentNode,
} from './types';
@@ -73,7 +73,7 @@ function matchSegment(path: string, patternSeg: PatternSegmentNode): boolean {
export default function match(
pathSegs: PathSegments,
- pattern: PatternNode,
+ pattern: PathPatternNode,
cwdSegs: undefined | PathSegments,
): boolean {
// Clone so we can freely mutate
@@ -86,8 +86,13 @@ export default function match(
}
// Quick optimization, check if the path contains all of the absolute names in the pattern
- for (const name of pattern.names) {
- if (!pathSegs.includes(name)) {
+ for (const seg of patternSegs) {
+ if (seg.type !== 'Segment' || seg.parts.length !== 1) {
+ continue;
+ }
+
+ const part = seg.parts[0];
+ if (part.type === 'Word' && !pathSegs.includes(part.value)) {
return false;
}
}

MODIFY
packages/@romejs/path-match/parse.ts
packages/@romejs/path-match/parse.ts
@@ -11,7 +11,7 @@ import {
PatternSegments,
PatternParts,
PatternSegmentNode,
- PatternNode,
+ PathPatternNode,
} from './types';
import {ParserOptions, createParser} from '@romejs/parser-core';
import {Number0, add, number0, get0, coerce0} from '@romejs/ob1';
@@ -220,7 +220,7 @@ const createPathMatchParser = createParser(
return normalized;
}
- parsePattern(): PatternNode {
+ parsePattern(): PathPatternNode {
const startPos = this.getPosition();
const segments: PatternSegments = [];
const negate = this.eatToken('Exclamation') !== undefined;
@@ -245,26 +245,12 @@ const createPathMatchParser = createParser(
root = firstSeg.type === 'Segment' && firstSeg.parts.length === 0;
}
- // Get a list of all segments that only have a single word
- const names: Array<string> = [];
- for (const seg of segments) {
- if (seg.type !== 'Segment' || seg.parts.length !== 1) {
- continue;
- }
-
- const part = seg.parts[0];
- if (part.type === 'Word') {
- names.push(part.value);
- }
- }
-
return {
- type: 'Pattern',
+ type: 'PathPattern',
loc: this.finishLoc(startPos),
root,
comment,
negate,
- names,
segments: this.normalizePatternSegments(segments),
};
}
@@ -308,7 +294,7 @@ const createPathMatchParser = createParser(
},
);
-export function parsePattern(opts: PathMatchParserOptions): PatternNode {
+export function parsePattern(opts: PathMatchParserOptions): PathPatternNode {
const parser = createPathMatchParser(opts, 'pattern');
return parser.parsePattern();
}

MODIFY
packages/@romejs/path-match/stringify.ts
packages/@romejs/path-match/stringify.ts
* LICENSE file in the root directory of this source tree.
*/
-import {PatternNode, PatternPartNode, PatternSegmentNode} from './types';
+import {PathPatternNode, PatternPartNode, PatternSegmentNode} from './types';
export function stringifyPathPattern(
- node: PatternNode | PatternPartNode | PatternSegmentNode,
+ node: PathPatternNode | PatternPartNode | PatternSegmentNode,
): string {
switch (node.type) {
case 'Pattern':

MODIFY
packages/@romejs/path-match/types.ts
packages/@romejs/path-match/types.ts
@@ -45,13 +45,12 @@ export type PatternSegmentNode =
| PatternWildcardSegmentNode;
export type PatternSegments = Array<PatternSegmentNode>;
-export type PatternNode = ComplexNode<
- 'Pattern',
+export type PathPatternNode = ComplexNode<
+ 'PathPattern',
{
comment: string;
negate: boolean;
root: boolean;
segments: PatternSegments;
- names: Array<string>;
}
>;

path-match: Rename Pattern node to PathPattern and eliminate redundant names array