Skip to content

Commit 207c862

Browse files
Add check for Windows-forbidden characters in the filename (#353)
1 parent ee5eda7 commit 207c862

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com).
66

7+
## Unreleased
8+
9+
- Add rule `TLDR111` ([#353](https://github.com/tldr-pages/tldr-lint/pull/353))
10+
711
## [v0.0.15 - 2024-04-03](https://github.com/tldr-pages/tldr-lint/compare/v0.0.14...v0.0.15)
812

913
- Add rule `TLDR020` ([#308](https://github.com/tldr-pages/tldr-lint/pull/308))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ TLDR107 | File name should end with `.md` extension
9090
TLDR108 | File name should not contain whitespace
9191
TLDR109 | File name should be lowercase
9292
TLDR110 | Command example should not be empty
93+
TLDR111 | File name should not contain any Windows-forbidden character
9394

9495
[npm-url]: https://www.npmjs.com/package/tldr-lint
9596
[npm-image]: https://img.shields.io/npm/v/tldr-lint.svg

lib/tldr-lint.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports.ERRORS = parser.ERRORS = {
3939
'TLDR108': 'File name should not contain whitespace',
4040
'TLDR109': 'File name should be lowercase',
4141
'TLDR110': 'Command example should not be empty',
42+
'TLDR111': 'File name should not contain any Windows-forbidden character'
4243
};
4344

4445
(function(parser) {
@@ -212,6 +213,10 @@ linter.processFile = function(file, verbose, alsoFormat, ignoreErrors) {
212213
result.errors.push({ locinfo: { first_line: '0' }, code: 'TLDR109', description: this.ERRORS.TLDR109 });
213214
}
214215

216+
if (/[<>:"/\\|?*]/.test(path.basename(file))) {
217+
result.errors.push({ locinfo: { first_line: '0' }, code: 'TLDR111', description: this.ERRORS.TLDR111 });
218+
}
219+
215220
if (ignoreErrors) {
216221
ignoreErrors = ignoreErrors.split(',').map(function(val) {
217222
return val.trim();

specs/pages/failing/111.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# jar
2+
3+
> JAR (Java Archive) is a package file format.
4+
5+
- Unzip file to the current directory:
6+
7+
`jar -xvf *.jar`

specs/tldr-lint.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const linter = require('../lib/tldr-lint.js');
23
const { lintFile, containsErrors, containsOnlyErrors } = require('./tldr-lint-helper');
34

@@ -183,6 +184,21 @@ describe('Common TLDR formatting errors', function() {
183184
expect(containsOnlyErrors(errors, 'TLDR110')).toBeTruthy();
184185
expect(errors.length).toBe(1);
185186
});
187+
188+
const invalidCharacters = ['<', '>', ':', '"', '/', '\\', '|', '?', '*'];
189+
invalidCharacters.forEach((char) => {
190+
it('TLDR111\t' + linter.ERRORS.TLDR111 + '\t - ${char}', function() {
191+
const basenameSpy = jest.spyOn(path, 'basename').mockImplementation((filePath) => {
192+
return `111${char}`;
193+
});
194+
195+
let errors = lintFile(`pages/failing/111.md`).errors;
196+
expect(containsOnlyErrors(errors, 'TLDR111')).toBeTruthy();
197+
expect(errors.length).toBe(1);
198+
199+
basenameSpy.mockRestore();
200+
});
201+
});
186202
});
187203

188204
describe('TLDR pages that are simply correct', function() {

0 commit comments

Comments
 (0)