-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#153): fixed the issue with @ctix-declaration statement generation
- fixed the issue with @ctix-declaration statement generation - fixed the problem where the fileExt option was not working
- Loading branch information
Showing
4 changed files
with
155 additions
and
29 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
112 changes: 112 additions & 0 deletions
112
src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts
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,112 @@ | ||
import { CE_INLINE_COMMENT_KEYWORD } from '#/comments/const-enum/CE_INLINE_COMMENT_KEYWORD'; | ||
import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; | ||
import { getInlineDeclarationRenderData } from '#/templates/modules/getInlineDeclarationRenderData'; | ||
import pathe from 'pathe'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
describe('getInlineDeclarationRenderData', () => { | ||
it('successfully generate render data, with output directory', () => { | ||
const declarations: ReturnType<typeof getInlineCommentedFiles> = [ | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), | ||
commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, | ||
tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, | ||
pos: { | ||
line: 0, | ||
column: 0, | ||
start: 0, | ||
}, | ||
workspaces: undefined, | ||
}, | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), | ||
commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, | ||
tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, | ||
pos: { | ||
line: 0, | ||
column: 0, | ||
start: 0, | ||
}, | ||
workspaces: undefined, | ||
}, | ||
]; | ||
|
||
const data = getInlineDeclarationRenderData(declarations, { | ||
output: pathe.join(process.cwd(), '/a/b/e'), | ||
fileExt: 'to-js', | ||
}); | ||
|
||
expect(data).toMatchObject([ | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), | ||
commentCode: '// @ctix-declaration', | ||
tag: '@ctix-declaration', | ||
pos: { line: 0, column: 0, start: 0 }, | ||
workspaces: undefined, | ||
relativePath: '../c/d/a.ts', | ||
extname: { origin: '.ts', render: '.js' }, | ||
}, | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), | ||
commentCode: '// @ctix-declaration', | ||
tag: '@ctix-declaration', | ||
pos: { line: 0, column: 0, start: 0 }, | ||
workspaces: undefined, | ||
relativePath: '../c/e/b.ts', | ||
extname: { origin: '.ts', render: '.js' }, | ||
}, | ||
]); | ||
}); | ||
|
||
it('successfully generate render data, without output directory', () => { | ||
const declarations: ReturnType<typeof getInlineCommentedFiles> = [ | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), | ||
commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, | ||
tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, | ||
pos: { | ||
line: 0, | ||
column: 0, | ||
start: 0, | ||
}, | ||
workspaces: undefined, | ||
}, | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), | ||
commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, | ||
tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, | ||
pos: { | ||
line: 0, | ||
column: 0, | ||
start: 0, | ||
}, | ||
workspaces: undefined, | ||
}, | ||
]; | ||
|
||
const data = getInlineDeclarationRenderData(declarations, { | ||
fileExt: 'to-js', | ||
}); | ||
|
||
expect(data).toMatchObject([ | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), | ||
commentCode: '// @ctix-declaration', | ||
tag: '@ctix-declaration', | ||
pos: { line: 0, column: 0, start: 0 }, | ||
workspaces: undefined, | ||
relativePath: `./${pathe.join(process.cwd(), '/a/b/c/d/a.ts')}`, | ||
extname: { origin: '.ts', render: '.js' }, | ||
}, | ||
{ | ||
filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), | ||
commentCode: '// @ctix-declaration', | ||
tag: '@ctix-declaration', | ||
pos: { line: 0, column: 0, start: 0 }, | ||
workspaces: undefined, | ||
relativePath: `./${pathe.join(process.cwd(), '/a/b/c/e/b.ts')}`, | ||
extname: { origin: '.ts', render: '.js' }, | ||
}, | ||
]); | ||
}); | ||
}); |
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,40 @@ | ||
import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; | ||
import type { TBundleOptions } from '#/configs/interfaces/TBundleOptions'; | ||
import { addCurrentDirPrefix } from '#/modules/path/addCurrentDirPrefix'; | ||
import { getExtname } from '#/modules/path/getExtname'; | ||
import { getImportStatementExtname } from '#/modules/path/getImportStatementExtname'; | ||
import { posixRelative } from '#/modules/path/modules/posixRelative'; | ||
import { replaceSepToPosix } from 'my-node-fp'; | ||
import path from 'path'; | ||
import pathe from 'pathe'; | ||
import type { SetOptional } from 'type-fest'; | ||
|
||
export function getInlineDeclarationRenderData( | ||
declarations: ReturnType<typeof getInlineCommentedFiles>, | ||
options: SetOptional<Pick<TBundleOptions, 'output' | 'fileExt'>, 'output'>, | ||
) { | ||
const renderDatas = declarations.map((declaration) => { | ||
const extname = getExtname(declaration.filePath); | ||
const renderExtname = getImportStatementExtname(options.fileExt, extname); | ||
const dirname = pathe.dirname(declaration.filePath); | ||
const basename = pathe.basename(declaration.filePath, extname); | ||
|
||
const relativePath = | ||
options.output != null | ||
? addCurrentDirPrefix( | ||
posixRelative(options.output, pathe.join(dirname, `${basename}${extname}`)), | ||
) | ||
: replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, `${basename}${extname}`)}`); | ||
|
||
return { | ||
...declaration, | ||
relativePath, | ||
extname: { | ||
origin: extname, | ||
render: renderExtname, | ||
}, | ||
}; | ||
}); | ||
|
||
return renderDatas; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export const declarationFileTemplate = ` | ||
<%- it.declarations.forEach((declaration) => { -%> | ||
import <%-= it.options.quote %><%= declaration.relativePath %><%= it.options.quote -%> | ||
import <%-= it.options.quote %><%= declaration.relativePath %><%= declaration.extname.render %><%= it.options.quote -%> | ||
<%- }) %> | ||
`; |