-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:regex to import names with dash properly (#302)
- Loading branch information
1 parent
7b3b1f6
commit 56aeaef
Showing
3 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { describe, expect, test } from "bun:test" | ||
import { TSCI_PACKAGE_PATTERN } from "../constants" | ||
|
||
describe("TSCI_PACKAGE_PATTERN", () => { | ||
// Test valid patterns | ||
const validPackages = [ | ||
"@tsci/basic", | ||
"@tsci/package-name", | ||
"@tsci/multiple-dashes-here", | ||
"@tsci/package.subpackage", | ||
"@tsci/complex-package.complex-subpackage", | ||
"@tsci/a.b.c", | ||
"@tsci/package-with-numbers123", | ||
"@tsci/package123-with-numbers", | ||
"@tsci/core.utils-helper", | ||
"@tsci/double--dash", | ||
] | ||
|
||
validPackages.forEach((pkg) => { | ||
test(`should match valid package: ${pkg}`, () => { | ||
const matches = Array.from(pkg.matchAll(TSCI_PACKAGE_PATTERN)) | ||
expect(matches).toHaveLength(1) | ||
expect(matches[0][0]).toBe(pkg) | ||
}) | ||
}) | ||
|
||
// Test invalid patterns | ||
const invalidPackages = [ | ||
"@tsci/", // Empty | ||
"@tsci/123start-with-number", // Starts with number | ||
"@tsci/-start-with-dash", // Starts with dash | ||
"@tsci/.start-with-dot", // Starts with dot | ||
|
||
"@tsci/double..dot", // Double dot | ||
"@tsci/end-with-dash-", // Ends with dash | ||
"@tsci/end-with-dot.", // Ends with dot | ||
"@tsci/invalid@chars", // Invalid characters | ||
"@tsci/dot.dash.-mixed", // Invalid dot-dash combination | ||
"@tsci/dash-.dot-mixed", // Invalid dash-dot combination | ||
"@tsci/space in name", // Contains space | ||
"@tsci/!", // Invalid character | ||
"@tsci/pkg#123", // Invalid character | ||
"@tsci/@invalid", // Invalid character | ||
] | ||
|
||
invalidPackages.forEach((pkg) => { | ||
test(`should not match invalid package: ${pkg}`, () => { | ||
const matches = Array.from(pkg.matchAll(TSCI_PACKAGE_PATTERN)) | ||
console.log(Array.from(pkg.matchAll(TSCI_PACKAGE_PATTERN))) | ||
expect(matches).toHaveLength(0) | ||
}) | ||
}) | ||
|
||
// Test import statements | ||
test("should match package names in import statements", () => { | ||
const importStatement = | ||
'import { something } from "@tsci/valid-package" import { other } from "@tsci/other-package.sub"' | ||
const matches = Array.from(importStatement.matchAll(TSCI_PACKAGE_PATTERN)) | ||
expect(matches).toHaveLength(2) | ||
expect(matches.map((m) => m[0])).toEqual([ | ||
"@tsci/valid-package", | ||
"@tsci/other-package.sub", | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Regular expression pattern for matching @tsci package imports | ||
* Rules: | ||
* - Must start with @tsci/ | ||
* - First character after @tsci/ must be a letter | ||
* - Can contain letters, numbers, single dashes between alphanumeric characters | ||
* - Can have subpackages separated by single dots | ||
* - Cannot end with dots or dashes | ||
* - Cannot have consecutive dots or dashes | ||
*/ | ||
export const TSCI_PACKAGE_PATTERN = | ||
/@tsci\/[a-zA-Z][a-zA-Z0-9]*(?:--?[a-zA-Z0-9]+)*(?:\.[a-zA-Z][a-zA-Z0-9]*(?:--?[a-zA-Z0-9]+)*)*$/g |