Skip to content

Commit 83f934d

Browse files
authored
同步代码 (#3)
* 修复禁用缓存也会写入缓存文件的BUG * 1.3.9 * Migrate to Jest testing framework and improve code quality - Replace old test files with comprehensive Jest test suite - Add Jest configuration and update ESLint for Jest environment - Enhance error handling with async/await and proper error propagation - Add circular inheritance detection for template safety - Improve parameter handling with argument overloading support - Update copyright and documentation formatting - Add block hide attribute functionality * Fix test file formatting and improve code readability Corrected spacing issues in multiple test files (index.test.js, lockfile.test.js, utils.test.js) to ensure consistent formatting and improved code readability. The changes include removing unnecessary blank lines and standardizing indentation for better maintainability. * Create workflow.yml * Remove GitHub Actions workflow for running tests and uploading coverage The workflow "Run tests and upload coverage" has been deleted as it is no longer needed in the repository. * Update README.md with codecov badge Add codecov badge to README.md to display test coverage status for the Node.js template engine project. * Refactor test cases for clarity and consistency Improved test descriptions across multiple files by: - Standardizing test naming conventions - Making expectations more explicit in assertion messages - Enhancing readability while maintaining test functionality - Adding precision to error handling descriptions These changes help maintain better documentation of test intent and improve long-term test suite maintainability. * Update test cases for layout and helper functionalities Refactor and expand test cases in helper.test.js and layout.test.js to improve coverage of edge cases, including cache locking behavior, block hide mode handling, and inheritance scenarios. Remove outdated/empty test stubs and enhance assertions for more robust validation. * Update test instructions to include running individual test files Added commands to run specific test files individually for better testing flexibility and workflow efficiency. * Add TypeScript support and update package metadata Add index.d.ts for TypeScript definitions, update files list to include TypeScript and documentation files, enhance keywords for better discoverability, and add TypeScript dev dependency. * 2.0.0
1 parent 585b6cb commit 83f934d

File tree

8 files changed

+291
-313
lines changed

8 files changed

+291
-313
lines changed

.npmignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 测试相关文件
2+
test/
3+
coverage/
4+
*.test.js
5+
jest.config.js
6+
7+
# 开发工具和配置
8+
.eslintrc*
9+
.eslintignore
10+
.editorconfig
11+
.vscode/
12+
.idea/
13+
14+
# 开发文档
15+
CLAUDE.md
16+
.github/
17+
18+
# 依赖和构建产物
19+
node_modules/
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
24+
# 锁文件
25+
package-lock.json
26+
yarn.lock
27+
28+
# 临时文件
29+
.DS_Store
30+
Thumbs.db
31+
.tmp
32+
.temp
33+
34+
# 源码管理
35+
.git/
36+
.gitignore
37+
38+
# TypeScript 构建产物(如果有的话)
39+
*.tsbuildinfo
40+
tsconfig.json
41+
42+
# 其他开发用文件
43+
*.log
44+
.nyc_output

CLAUDE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ npm run eslint
1818
# 运行所有测试
1919
npm test
2020

21+
# 运行单个测试文件
22+
npm test -- test/index.test.js
23+
npm test -- test/helper.test.js
24+
npm test -- test/layout.test.js
25+
npm test -- test/lockfile.test.js
26+
npm test -- test/utils.test.js
27+
2128
# 监听模式运行测试
2229
npm run test:watch
2330

index.d.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Template compilation options
3+
*/
4+
export interface CompileOptions {
5+
/** Block name to compile specific block */
6+
block?: string;
7+
/** Enable/disable caching */
8+
cache?: boolean;
9+
/** Cache directory name */
10+
cacheName?: string;
11+
}
12+
13+
/**
14+
* Template data object - can be any object type
15+
*/
16+
export type TemplateData = Record<string, any>;
17+
18+
/**
19+
* Compiled template function
20+
*/
21+
export interface CompiledTemplate {
22+
(data?: TemplateData, subTemplate?: string): string;
23+
}
24+
25+
/**
26+
* Template rendering callback
27+
*/
28+
export interface RenderCallback {
29+
(error: Error | null, result?: string): void;
30+
}
31+
32+
/**
33+
* Template compilation callback
34+
*/
35+
export interface CompileCallback {
36+
(error: Error | null, templateFunction?: CompiledTemplate): void;
37+
}
38+
39+
/**
40+
* Main template engine interface
41+
*/
42+
export interface CBTemplate {
43+
/** Template engine version */
44+
version: string;
45+
46+
/** Left delimiter for template syntax */
47+
leftDelimiter: string;
48+
49+
/** Right delimiter for template syntax */
50+
rightDelimiter: string;
51+
52+
/** Default HTML escaping setting */
53+
escape: boolean;
54+
55+
/** Base path for template files */
56+
basePath: string;
57+
58+
/** Cache path for compiled templates */
59+
cachePath: string;
60+
61+
/** Default file extension */
62+
defaultExtName: string;
63+
64+
/**
65+
* Compile template string to function
66+
* @param str Template string
67+
* @returns Compiled template function
68+
*/
69+
compile(str: string): CompiledTemplate;
70+
71+
/**
72+
* Render template string with data
73+
* @param str Template string
74+
* @param data Template data
75+
* @param subTemplate Sub template name
76+
* @returns Rendered string
77+
*/
78+
render(str: string, data?: TemplateData, subTemplate?: string): string;
79+
80+
/**
81+
* Compile template file with inheritance support
82+
* @param filename Template file path
83+
* @param options Compilation options
84+
* @param callback Compilation callback
85+
*/
86+
compileFile(filename: string, options: CompileOptions, callback: CompileCallback): void;
87+
compileFile(filename: string, callback: CompileCallback): void;
88+
89+
/**
90+
* Render template file with data and inheritance support
91+
* @param filename Template file path
92+
* @param data Template data
93+
* @param options Render options
94+
* @param callback Render callback
95+
*/
96+
renderFile(filename: string, data: TemplateData, options: CompileOptions, callback: RenderCallback): void;
97+
renderFile(filename: string, data: TemplateData, callback: RenderCallback): void;
98+
99+
/**
100+
* Get new instance of template engine
101+
* @returns New template engine instance
102+
*/
103+
getInstance(): CBTemplate;
104+
105+
/** Internal parse method */
106+
_parse(str: string): string;
107+
108+
/** Internal build template function method */
109+
_buildTemplateFunction(str: string): CompiledTemplate;
110+
}
111+
112+
/**
113+
* Template engine instance with static methods
114+
*/
115+
export interface CBTemplateStatic extends CBTemplate {
116+
/**
117+
* Get new instance of template engine
118+
* @returns New template engine instance
119+
*/
120+
getInstance(): CBTemplate;
121+
}
122+
123+
declare const cbTemplate: CBTemplateStatic;
124+
export default cbTemplate;

package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
22
"name": "cb-template",
3-
"version": "1.3.9",
3+
"version": "2.0.0",
44
"description": "唱吧模板引擎",
55
"main": "index.js",
6+
"types": "index.d.ts",
7+
"files": [
8+
"index.js",
9+
"index.d.ts",
10+
"lib/",
11+
"README.md",
12+
"LICENSE"
13+
],
614
"scripts": {
715
"eslint": "eslint *.js",
816
"test": "jest",
@@ -14,7 +22,14 @@
1422
"url": "git+https://github.com/ChangbaFE/cbT.git"
1523
},
1624
"keywords": [
17-
"template"
25+
"template",
26+
"template-engine",
27+
"changba",
28+
"cbT",
29+
"server-side-template",
30+
"template-inheritance",
31+
"html-template",
32+
"node-template"
1833
],
1934
"author": "Hex",
2035
"license": "MIT",
@@ -24,6 +39,7 @@
2439
"homepage": "https://github.com/ChangbaFE/cbT#readme",
2540
"devDependencies": {
2641
"eslint": "^6.8.0",
27-
"jest": "^30.0.4"
42+
"jest": "^30.0.4",
43+
"typescript": "^5.8.3"
2844
}
2945
}

test/helper.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ describe('helper.js', () => {
2626
expect(helpers.encodeHTML('test\\slash')).toBe('test&#92;slash');
2727
});
2828

29-
test('should handle empty string', () => {
29+
test('should handle empty string for HTML encoding', () => {
3030
expect(helpers.encodeHTML('')).toBe('');
3131
});
3232

33-
test('should convert non-string values to string', () => {
34-
expect(helpers.encodeHTML(123)).toBe('123');
33+
test('should convert non-string values to string for HTML encoding', () => {
3534
expect(helpers.encodeHTML(null)).toBe('null');
3635
expect(helpers.encodeHTML(undefined)).toBe('undefined');
3736
});
3837

39-
test('should handle mixed content', () => {
38+
test('should handle mixed content for HTML encoding', () => {
4039
expect(helpers.encodeHTML('<script>alert("XSS")</script>'))
4140
.toBe('&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;');
4241
});
@@ -84,9 +83,6 @@ describe('helper.js', () => {
8483
expect(helpers.encodeEventHTML('\\r')).toBe('\r'); // 转义的回车符变成真正的回车符
8584
});
8685

87-
test('should convert non-string values to string', () => {
88-
expect(helpers.encodeEventHTML(123)).toBe('123');
89-
});
9086
});
9187

9288
describe('forEachArray', () => {

test/index.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ describe('index.js', () => {
231231
});
232232

233233
describe('file operations', () => {
234-
test('renderFile should render file template', (done) => {
234+
test('should render template file with data', (done) => {
235235
const templatePath = path.join(testDir, 'test.html');
236236
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
237237

@@ -243,7 +243,7 @@ describe('index.js', () => {
243243
});
244244
});
245245

246-
test('compileFile should compile file template', (done) => {
246+
test('should compile template file into function', (done) => {
247247
const templatePath = path.join(testDir, 'test.html');
248248
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
249249

@@ -275,7 +275,7 @@ describe('index.js', () => {
275275
}).toThrow();
276276
});
277277

278-
test('should handle invalid variable references gracefully', () => {
278+
test('should throw error for invalid nested property access', () => {
279279
const template = '<%=a.b.c.d%>';
280280
expect(() => {
281281
cbT.render(template, { a: {} });
@@ -313,7 +313,7 @@ describe('index.js', () => {
313313
});
314314

315315
describe('default parameters', () => {
316-
test('compileFile should work without options parameter', (done) => {
316+
test('should work without options parameter for compileFile', (done) => {
317317
const templatePath = path.join(testDir, 'test-no-options.html');
318318
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
319319

@@ -327,7 +327,7 @@ describe('index.js', () => {
327327
});
328328
});
329329

330-
test('renderFile should work without options parameter', (done) => {
330+
test('should work without options parameter for renderFile', (done) => {
331331
const templatePath = path.join(testDir, 'test-no-options2.html');
332332
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
333333

@@ -339,7 +339,7 @@ describe('index.js', () => {
339339
});
340340
});
341341

342-
test('compileFile should use default options when explicitly passed undefined', (done) => {
342+
test('should use default options in compileFile when undefined', (done) => {
343343
const templatePath = path.join(testDir, 'test-undefined-options.html');
344344
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
345345

@@ -354,7 +354,7 @@ describe('index.js', () => {
354354
});
355355
});
356356

357-
test('renderFile should use default options when explicitly passed undefined', (done) => {
357+
test('should use default options in renderFile when undefined', (done) => {
358358
const templatePath = path.join(testDir, 'test-undefined-options2.html');
359359
fs.writeFileSync(templatePath, 'Hello <%=name%>!');
360360

0 commit comments

Comments
 (0)