Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
verytactical committed Feb 4, 2025
1 parent 6e41992 commit 66354f8
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/grammar/next/grammar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,88 @@ describe("parse imports", () => {
],
});
});

it("should parse absolute stdlib imports", () => {
expect(parse('import "@stdlib/foo";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["foo.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse relative stdlib imports", () => {
expect(parse('import "@stdlib/foo/../bar";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["bar.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse stdlib tact imports with extension", () => {
expect(parse('import "@stdlib/foo.tact";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["foo.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse stdlib func imports with extension", () => {
expect(parse('import "@stdlib/foo.fc";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "func",
path: {
segments: ["foo.fc"],
stepsUp: 0,
},
},
},
],
});
});

it("should reject stdlib root import", () => {
expect(() => parse('import "@stdlib";')).toThrow();
});

it("should reject stdlib root import as folder", () => {
expect(() => parse('import "@stdlib/";')).toThrow();
});

it("should reject stdlib folder import", () => {
expect(() => parse('import "@stdlib/foo/";')).toThrow();
});

it("should reject stdlib import up from stdlib root", () => {
expect(() => parse('import "@stdlib/../foo";')).toThrow();
});
});
8 changes: 7 additions & 1 deletion src/grammar/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,14 @@ const parseImportString =
const { guessedPath, language } = guessExtension(importText);

if (guessedPath.startsWith(stdlibPrefix)) {
const path = fromString(guessedPath.substring(stdlibPrefix.length));

if (path.stepsUp !== 0) {
ctx.err.importWithBackslash()(loc);
}

return {
path: fromString(guessedPath.substring(stdlibPrefix.length)),
path,
type: "stdlib",
language,
};
Expand Down
5 changes: 5 additions & 0 deletions src/grammar/parser-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export const syntaxErrorSchema = <T, U>(
invalidImport: () => {
return handle(sub`Import must start with ./, ../ or @stdlib/`);
},
escapingImport: () => {
return handle(
sub`Standard library imports should be inside its root`,
);
},
};
};

Expand Down
84 changes: 84 additions & 0 deletions src/grammar/prev/grammar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,88 @@ describe("parse imports", () => {
],
});
});

it("should parse absolute stdlib imports", () => {
expect(parse('import "@stdlib/foo";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["foo.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse relative stdlib imports", () => {
expect(parse('import "@stdlib/foo/../bar";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["bar.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse stdlib tact imports with extension", () => {
expect(parse('import "@stdlib/foo.tact";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "tact",
path: {
segments: ["foo.tact"],
stepsUp: 0,
},
},
},
],
});
});

it("should parse stdlib func imports with extension", () => {
expect(parse('import "@stdlib/foo.fc";')).toMatchObject({
imports: [
{
importPath: {
type: "stdlib",
language: "func",
path: {
segments: ["foo.fc"],
stepsUp: 0,
},
},
},
],
});
});

it("should reject stdlib root import", () => {
expect(() => parse('import "@stdlib";')).toThrow();
});

it("should reject stdlib root import as folder", () => {
expect(() => parse('import "@stdlib/";')).toThrow();
});

it("should reject stdlib folder import", () => {
expect(() => parse('import "@stdlib/foo/";')).toThrow();
});

it("should reject stdlib import up from stdlib root", () => {
expect(() => parse('import "@stdlib/../foo";')).toThrow();
});
});
3 changes: 3 additions & 0 deletions src/grammar/prev/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ function parseImportString(importText: string, loc: SrcInfo): A.ImportPath {

if (guessedPath.startsWith(stdlibPrefix)) {
const path = fromString(guessedPath.substring(stdlibPrefix.length));
if (path.stepsUp !== 0) {
err().importWithBackslash()(loc);
}
return { path, type: "stdlib", language };
} else if (guessedPath.startsWith("./") || guessedPath.startsWith("../")) {
return { path: fromString(guessedPath), type: "relative", language };
Expand Down

0 comments on commit 66354f8

Please sign in to comment.