Skip to content

Commit 4767caf

Browse files
refactor: convert pug-error to TypeScript (#3355)
1 parent a724446 commit 4767caf

File tree

11 files changed

+282
-89
lines changed

11 files changed

+282
-89
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ cov-pt*
1818
**/test/temp
1919
.release.json
2020
package-lock.json
21+
22+
scripts/tsconfig.json
23+
packages/pug-error/lib

README.md

Lines changed: 67 additions & 63 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"devDependencies": {
99
"coveralls": "3.0.9",
1010
"jest": "^26.0.1",
11-
"prettier": "1.19.1"
11+
"prettier": "1.19.1",
12+
"typescript": "^4.1.2",
13+
"wsrun": "^5.2.0"
1214
},
1315
"repository": {
1416
"type": "git",
@@ -17,7 +19,7 @@
1719
"scripts": {
1820
"prettier:check": "prettier --ignore-path .gitignore --list-different './**/*.js'",
1921
"format": "prettier --ignore-path .gitignore --write './**/*.js'",
20-
"build": "wsrun --stages --exclude-missing --fast-exit --collect-logs build",
22+
"build": "node scripts/prebuild && wsrun --stages --exclude-missing --fast-exit --collect-logs build && tsc --build scripts",
2123
"pretest": "yarn build",
2224
"test": "jest",
2325
"coverage": "jest --coverage",
@@ -32,8 +34,5 @@
3234
"./scripts/buffer-serializer.js"
3335
]
3436
},
35-
"license": "MIT",
36-
"dependencies": {
37-
"wsrun": "^5.2.0"
38-
}
37+
"license": "MIT"
3938
}

packages/pug-error/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"name": "pug-error",
33
"version": "1.3.3",
44
"description": "Standard error objects for pug",
5+
"main": "./lib/index.js",
6+
"types": "./lib/index.d.ts",
57
"keywords": [
68
"pug"
79
],
810
"files": [
9-
"index.js"
11+
"lib/"
1012
],
1113
"repository": {
1214
"type": "git",
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
'use strict';
2-
3-
module.exports = makeError;
4-
function makeError(code, message, options) {
5-
var line = options.line;
6-
var column = options.column;
7-
var filename = options.filename;
8-
var src = options.src;
9-
var fullMessage;
10-
var location = line + (column ? ':' + column : '');
1+
export default function makeError(
2+
code: string,
3+
message: string,
4+
options: {line: number; column: number; filename?: string; src?: string},
5+
) {
6+
const line = options.line;
7+
const column = options.column;
8+
const filename = options.filename;
9+
const src = options.src;
10+
let fullMessage;
11+
const location = line + (column ? ':' + column : '');
1112
if (src && line >= 1 && line <= src.split('\n').length) {
12-
var lines = src.split('\n');
13-
var start = Math.max(line - 3, 0);
14-
var end = Math.min(lines.length, line + 3);
13+
const lines = src.split('\n');
14+
const start = Math.max(line - 3, 0);
15+
const end = Math.min(lines.length, line + 3);
1516
// Error context
16-
var context = lines
17+
const context = lines
1718
.slice(start, end)
1819
.map(function(text, i) {
19-
var curr = i + start + 1;
20-
var preamble = (curr == line ? ' > ' : ' ') + curr + '| ';
21-
var out = preamble + text;
20+
const curr = i + start + 1;
21+
const preamble = (curr == line ? ' > ' : ' ') + curr + '| ';
22+
let out = preamble + text;
2223
if (curr === line && column > 0) {
2324
out += '\n';
2425
out += Array(preamble.length + column).join('-') + '^';
@@ -31,7 +32,7 @@ function makeError(code, message, options) {
3132
} else {
3233
fullMessage = (filename || 'Pug') + ':' + location + '\n\n' + message;
3334
}
34-
var err = new Error(fullMessage);
35+
const err: any = new Error(fullMessage);
3536
err.code = 'PUG:' + code;
3637
err.msg = message;
3738
err.line = line;
@@ -49,3 +50,7 @@ function makeError(code, message, options) {
4950
};
5051
return err;
5152
}
53+
54+
// Make this easier to use from CommonJS
55+
module.exports = makeError;
56+
module.exports.default = makeError;

packages/pug-error/test/index.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var assert = require('assert');
43
var error = require('../');
54

65
describe('with a source', function() {

packages/pug-error/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"rootDir": "src",
6+
"outDir": "lib",
7+
"tsBuildInfoFile": "lib/tsconfig.tsbuildinfo",
8+
},
9+
"references": [],
10+
}

packages/pug-strip-comments/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Forbes Lindesay
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

scripts/prebuild.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
const {
2+
readdirSync,
3+
writeFileSync,
4+
statSync,
5+
readFileSync,
6+
existsSync,
7+
} = require('fs');
8+
9+
const LICENSE = `Copyright (c) ${new Date().getFullYear()} Forbes Lindesay
10+
11+
Permission is hereby granted, free of charge, to any person obtaining a copy
12+
of this software and associated documentation files (the "Software"), to deal
13+
in the Software without restriction, including without limitation the rights
14+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
copies of the Software, and to permit persons to whom the Software is
16+
furnished to do so, subject to the following conditions:
17+
18+
The above copyright notice and this permission notice shall be included in
19+
all copies or substantial portions of the Software.
20+
21+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
THE SOFTWARE.`;
28+
29+
const packageNames = [];
30+
const typeScriptPackages = [];
31+
const packageDirectories = readdirSync(__dirname + '/../packages')
32+
.filter(directory =>
33+
statSync(__dirname + '/../packages/' + directory).isDirectory()
34+
)
35+
.sort();
36+
packageDirectories.forEach(directory => {
37+
if (!existsSync(__dirname + '/../packages/' + directory + '/LICENSE')) {
38+
writeFileSync(
39+
__dirname + '/../packages/' + directory + '/LICENSE',
40+
LICENSE
41+
);
42+
}
43+
let pkg = {};
44+
try {
45+
pkg = JSON.parse(
46+
readFileSync(
47+
__dirname + '/../packages/' + directory + '/package.json',
48+
'utf8'
49+
)
50+
);
51+
} catch (ex) {
52+
if (ex.code !== 'ENOENT') {
53+
throw ex;
54+
}
55+
}
56+
const before = JSON.stringify(pkg);
57+
if (!pkg.name) {
58+
pkg.name = directory;
59+
}
60+
packageNames.push(pkg.name);
61+
const after = JSON.stringify(pkg);
62+
if (before !== after) {
63+
writeFileSync(
64+
__dirname + '/../packages/' + directory + '/package.json',
65+
JSON.stringify(pkg, null, ' ') + '\n'
66+
);
67+
}
68+
if (existsSync(__dirname + '/../packages/' + directory + '/tsconfig.json')) {
69+
typeScriptPackages.push(directory);
70+
const deps = [
71+
...Object.keys(pkg.dependencies || {}),
72+
...Object.keys(pkg.devDependencies || {}),
73+
]
74+
.filter(dep =>
75+
existsSync(__dirname + '/../packages/' + dep + '/tsconfig.json')
76+
)
77+
.map(
78+
dep =>
79+
`\n {"path": ${JSON.stringify(
80+
`../${dep.substr(`@databases/`.length)}`
81+
)}},`
82+
)
83+
.join(``);
84+
writeFileSync(
85+
__dirname + '/../packages/' + directory + '/tsconfig.json',
86+
`{
87+
"extends": "../../tsconfig.json",
88+
"compilerOptions": {
89+
"composite": true,
90+
"rootDir": "src",
91+
"outDir": "lib",
92+
"tsBuildInfoFile": "lib/tsconfig.tsbuildinfo",
93+
},
94+
"references": ${deps.length ? `[${deps}\n ],` : `[],`}
95+
}
96+
`
97+
);
98+
}
99+
});
100+
101+
writeFileSync(
102+
`scripts/tsconfig.json`,
103+
`{
104+
"extends": "../tsconfig.json",
105+
"references": [${typeScriptPackages
106+
.map(n => `\n {"path": ${JSON.stringify(`../packages/${n}`)}},`)
107+
.join(``)}
108+
],
109+
}`
110+
);
111+
const [README_HEADER, _table, README_FOOTER] = readFileSync(
112+
__dirname + '/../README.md',
113+
'utf8'
114+
).split('<!-- VERSION_TABLE -->');
115+
116+
const versionsTable = `
117+
Package Name | Version
118+
-------------|--------
119+
${packageNames
120+
.sort()
121+
.map(
122+
name =>
123+
`${name} | [![NPM version](https://img.shields.io/npm/v/${name}?style=for-the-badge)](https://www.npmjs.com/package/${name})`
124+
)
125+
.join('\n')}
126+
`;
127+
writeFileSync(
128+
__dirname + '/../README.md',
129+
[README_HEADER, versionsTable, README_FOOTER || ''].join(
130+
'<!-- VERSION_TABLE -->'
131+
)
132+
);

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"sourceMap": true,
7+
"strict": true,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": false,
10+
"noImplicitReturns": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"lib": ["es2016", "dom"],
13+
"pretty": true
14+
}
15+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,6 +4039,11 @@ typedarray-to-buffer@^3.1.5:
40394039
dependencies:
40404040
is-typedarray "^1.0.0"
40414041

4042+
typescript@^4.1.2:
4043+
version "4.5.4"
4044+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8"
4045+
integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg==
4046+
40424047
uc.micro@^1.0.1, uc.micro@^1.0.5:
40434048
version "1.0.6"
40444049
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"

0 commit comments

Comments
 (0)