-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): improve error message when encountering unsupported syntax
- Add NotFoundExportedKind error class - Add link for GitHub issue creation in CLI error message
- Loading branch information
Showing
9 changed files
with
301 additions
and
42 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
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
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,53 @@ | ||
import type { IReason } from '#/compilers/interfaces/IReason'; | ||
import type * as tsm from 'ts-morph'; | ||
|
||
export class NotFoundExportedKind extends Error { | ||
#pos: { line: number; column: number }; | ||
|
||
#filePath: string; | ||
|
||
#node: tsm.Node; | ||
|
||
get pos() { | ||
return this.#pos; | ||
} | ||
|
||
get filePath() { | ||
return this.#filePath; | ||
} | ||
|
||
get node() { | ||
return this.#node; | ||
} | ||
|
||
constructor( | ||
pos: NotFoundExportedKind['pos'], | ||
filePath: string, | ||
node: tsm.Node, | ||
message?: string, | ||
) { | ||
super(message); | ||
|
||
this.#pos = pos; | ||
this.#node = node; | ||
this.#filePath = filePath; | ||
} | ||
|
||
createReason() { | ||
const message = | ||
`Cannot support export statement: (${this.#node.getKind()}) ${this.#node.getText()}`.trim(); | ||
|
||
const reason: IReason = { | ||
type: 'error', | ||
lineAndCharacter: { | ||
line: this.#pos.line, | ||
character: this.#pos.column, | ||
}, | ||
nodes: [this.#node], | ||
filePath: this.#filePath, | ||
message, | ||
}; | ||
|
||
return reason; | ||
} | ||
} |
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,124 @@ | ||
import { NotFoundExportedKind } from '#/errors/NotFoundExportedKind'; | ||
import { randomUUID } from 'crypto'; | ||
import { atOrThrow } from 'my-easy-fp'; | ||
import pathe from 'pathe'; | ||
import * as tsm from 'ts-morph'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
const tsconfigDirPath = pathe.join(process.cwd(), 'examples'); | ||
const tsconfigFilePath = pathe.join(tsconfigDirPath, 'tsconfig.example.json'); | ||
const context = { | ||
tsconfig: tsconfigFilePath, | ||
project: new tsm.Project({ | ||
tsConfigFilePath: tsconfigFilePath, | ||
}), | ||
}; | ||
|
||
describe('NotFoundExportedKind', () => { | ||
it('constructor', () => { | ||
const uuid = randomUUID(); | ||
const filename = `${uuid}.ts`; | ||
const source = ` | ||
/** @ctix-generation-style default-alias-named-destructive */ | ||
import path from 'node:path'; | ||
/** | ||
* Some Detail Comment | ||
*/ | ||
/** @ctix-exclude-next */ | ||
const hero = 'ironman'; | ||
export default hero; | ||
export function iamfunction() { | ||
return 'function'; | ||
}`.trim(); | ||
|
||
const sourceFile = context.project.createSourceFile(filename, source.trim()); | ||
const exportedDeclarationsMap = sourceFile.getExportedDeclarations(); | ||
const exportedDeclaration = atOrThrow(exportedDeclarationsMap.get('default') ?? [], 0); | ||
|
||
const err = new NotFoundExportedKind( | ||
{ line: 1, column: 1 }, | ||
filename, | ||
exportedDeclaration, | ||
'test message', | ||
); | ||
|
||
expect(err).toBeTruthy(); | ||
}); | ||
|
||
it('getter and setter', () => { | ||
const uuid = randomUUID(); | ||
const filename = `${uuid}.ts`; | ||
const source = ` | ||
/** @ctix-generation-style default-alias-named-destructive */ | ||
import path from 'node:path'; | ||
/** | ||
* Some Detail Comment | ||
*/ | ||
/** @ctix-exclude-next */ | ||
const hero = 'ironman'; | ||
export default hero; | ||
export function iamfunction() { | ||
return 'function'; | ||
}`.trim(); | ||
|
||
const sourceFile = context.project.createSourceFile(filename, source.trim()); | ||
const exportedDeclarationsMap = sourceFile.getExportedDeclarations(); | ||
const exportedDeclaration = atOrThrow(exportedDeclarationsMap.get('default') ?? [], 0); | ||
|
||
const err = new NotFoundExportedKind( | ||
{ line: 1, column: 1 }, | ||
filename, | ||
exportedDeclaration, | ||
'test message', | ||
); | ||
|
||
expect(err.filePath).toEqual(filename); | ||
expect(err.pos).toEqual({ line: 1, column: 1 }); | ||
expect(err.node).toBeTruthy(); | ||
}); | ||
|
||
it('create reason', () => { | ||
const uuid = randomUUID(); | ||
const filename = `${uuid}.ts`; | ||
const source = ` | ||
/** @ctix-generation-style default-alias-named-destructive */ | ||
import path from 'node:path'; | ||
/** | ||
* Some Detail Comment | ||
*/ | ||
/** @ctix-exclude-next */ | ||
const hero = 'ironman'; | ||
export default hero; | ||
export function iamfunction() { | ||
return 'function'; | ||
}`.trim(); | ||
|
||
const sourceFile = context.project.createSourceFile(filename, source.trim()); | ||
const exportedDeclarationsMap = sourceFile.getExportedDeclarations(); | ||
const exportedDeclaration = atOrThrow(exportedDeclarationsMap.get('default') ?? [], 0); | ||
|
||
const err = new NotFoundExportedKind( | ||
{ line: 1, column: 1 }, | ||
filename, | ||
exportedDeclaration, | ||
'test message', | ||
); | ||
|
||
const reason = err.createReason(); | ||
expect(reason).toMatchObject({ | ||
type: 'error', | ||
lineAndCharacter: { line: 1, character: 1 }, | ||
filePath: filename, | ||
message: "Cannot support export statement: (260) hero = 'ironman'", | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.