-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: creating discover module for importer feature
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,3 @@ | ||
export default class Dependency { | ||
constructor(public readonly id: string, public readonly alias: string) {} | ||
} |
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,57 @@ | ||
import { Lexer } from "@/lexer"; | ||
import Discover from "./discover"; | ||
import Dependency from "./dependency"; | ||
|
||
describe("Discover test suite", () => { | ||
test("Program that imports only one dependency", () => { | ||
const program = ` | ||
from "github.com/guiferpa/testing" as testing | ||
`; | ||
|
||
const expected = [new Dependency("github.com/guiferpa/testing", "testing")]; | ||
|
||
const lexer = new Lexer(Buffer.from(program, "utf-8")); | ||
const discover = new Discover(lexer); | ||
const got = discover.run(); | ||
expect(got).toStrictEqual(expected); | ||
}); | ||
|
||
test("Program that imports some dependencies", () => { | ||
const program = ` | ||
from "github.com/guiferpa/testing" as testing | ||
from "github.com/guiferpa/tester" as tester | ||
from "github.com/guiferpa/test" as test | ||
`; | ||
|
||
const expected = [ | ||
new Dependency("github.com/guiferpa/testing", "testing"), | ||
new Dependency("github.com/guiferpa/tester", "tester"), | ||
new Dependency("github.com/guiferpa/test", "test"), | ||
]; | ||
|
||
const lexer = new Lexer(Buffer.from(program, "utf-8")); | ||
const discover = new Discover(lexer); | ||
const got = discover.run(); | ||
expect(got).toStrictEqual(expected); | ||
}); | ||
|
||
test("Program that imports some duplicated dependencies", () => { | ||
const program = ` | ||
from "github.com/guiferpa/testing" as testing | ||
from "github.com/guiferpa/testing" as testing | ||
from "github.com/guiferpa/tester" as tester | ||
from "github.com/guiferpa/test" as test | ||
`; | ||
|
||
const expected = [ | ||
new Dependency("github.com/guiferpa/testing", "testing"), | ||
new Dependency("github.com/guiferpa/tester", "tester"), | ||
new Dependency("github.com/guiferpa/test", "test"), | ||
]; | ||
|
||
const lexer = new Lexer(Buffer.from(program, "utf-8")); | ||
const discover = new Discover(lexer); | ||
const got = discover.run(); | ||
expect(got).toStrictEqual(expected); | ||
}); | ||
}); |
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,54 @@ | ||
import { Lexer } from "@/lexer"; | ||
import { TokenTag } from "@/lexer/tokens/tag"; | ||
import { Token } from "@/lexer/tokens/token"; | ||
|
||
import Dependency from "./dependency"; | ||
|
||
export default class Discover { | ||
private _lookahead: Token | null = null; | ||
|
||
constructor(private readonly _lexer: Lexer) {} | ||
|
||
private _discovery(): Dependency[] { | ||
const deps = new Map<string, Dependency>(); | ||
|
||
this._lookahead = this._lexer.getNextToken(); | ||
|
||
while (this._lookahead?.tag === TokenTag.FROM) { | ||
this._lookahead = this._lexer.getNextToken(); | ||
if (this._lookahead.tag !== TokenTag.STR) { | ||
throw new Error( | ||
`Missing dependency identifier at line ${this._lookahead.line}` | ||
); | ||
} | ||
|
||
const id = this._lookahead.value; | ||
|
||
this._lookahead = this._lexer.getNextToken(); | ||
|
||
if (this._lookahead.tag !== TokenTag.AS) { | ||
throw new SyntaxError( | ||
`Unexpected token ${this._lookahead.tag} at line ${this._lookahead.line}` | ||
); | ||
} | ||
|
||
this._lookahead = this._lexer.getNextToken(); | ||
|
||
if (this._lookahead.tag !== TokenTag.IDENT) { | ||
throw new Error(`Invalid alias at line ${this._lookahead.line}`); | ||
} | ||
|
||
const alias = this._lookahead.value; | ||
|
||
deps.set(id, new Dependency(id, alias)); | ||
|
||
this._lookahead = this._lexer.getNextToken(); | ||
} | ||
|
||
return Array.from(deps.values()); | ||
} | ||
|
||
public run(): Dependency[] { | ||
return this._discovery(); | ||
} | ||
} |
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