dtsgenerator 3.14.0 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,6 +29,7 @@ TypeScript d.ts file generator from JSON Schema file or OpenAPI(Swagger) spec fi
29
29
  ## Usage
30
30
 
31
31
  ### CLI
32
+
32
33
  ```sh
33
34
  $ dtsgen --help
34
35
  Usage: dtsgenerator [options] <file ... | file patterns using node-glob>
@@ -58,6 +59,7 @@ Examples:
58
59
  For the configuration file, please refer to the file in the [config_sample](https://github.com/horiuchi/dtsgenerator/tree/master/config_sample/) directory.
59
60
 
60
61
  ### NodeJS API
62
+
61
63
  ```js
62
64
  const { default: dtsgenerator, parseSchema } = require('dtsgenerator');
63
65
 
@@ -88,21 +90,20 @@ The dtsgenerator v3 has made the following braking changes from v2.
88
90
 
89
91
  - Support Plug-in feature. See the Plug-in section for more information.
90
92
  - Change the command line options.
91
- - Remove the `--namespace` option. Use the `@dtsgenerator/replace-namespace` plug-in instead.
92
- - Add the `--config` option. Mainly for setting up the Plug-in.
93
- - And add more options.
93
+ - Remove the `--namespace` option. Use the `@dtsgenerator/replace-namespace` plug-in instead.
94
+ - Add the `--config` option. Mainly for setting up the Plug-in.
95
+ - And add more options.
94
96
  - TypeScript AST is now used internally to generate type definitions.
95
97
 
96
-
97
98
  ## Plug-in
98
99
 
99
100
  ### How to find plug-in
100
101
 
101
- - Search by npm: https://www.npmjs.com/search?q=dtsgenerator%20plugin
102
- - Find by the @dtsgenerator repositories: https://github.com/dtsgenerator
103
- - `@dtsgenerator/replace-namespace` : This plug-in is instead the `--namespace` option on old version.
104
- - `@dtsgenerator/decorate-typename` : This plug-in can decorate the output type name.
105
- - `@dtsgenerator/single-quote` : This plug-in replace the quote mark to single.
102
+ - Search by npm: <https://www.npmjs.com/search?q=dtsgenerator%20plugin>
103
+ - Find by the @dtsgenerator repositories: <https://github.com/dtsgenerator>
104
+ - `@dtsgenerator/replace-namespace` : This plug-in is instead the `--namespace` option on old version.
105
+ - `@dtsgenerator/decorate-typename` : This plug-in can decorate the output type name.
106
+ - `@dtsgenerator/single-quote` : This plug-in replace the quote mark to single.
106
107
 
107
108
  ### How to create plug-in
108
109
 
@@ -122,8 +123,7 @@ The dtsgenerator v3 has made the following braking changes from v2.
122
123
 
123
124
  Output debug message by [debug](https://www.npmjs.com/package/debug) library.
124
125
 
125
- $ DEBUG=dtsgen dtsgen schema/news.json
126
-
126
+ DEBUG=dtsgen dtsgen schema/news.json
127
127
 
128
128
  ### Links about JSON Schema and Swagger
129
129
 
@@ -143,7 +143,22 @@ Output debug message by [debug](https://www.npmjs.com/package/debug) library.
143
143
 
144
144
  ## ChangeLog
145
145
 
146
- ### [v3.14.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.13.2) (2022-01-27)
146
+ ### [v3.16.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.16.0) (2022-06-10)
147
+
148
+ - features:
149
+ - Support 'image/*' media types for #539. Thank you @Geloosa :+1:
150
+
151
+ ### [v3.15.1](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.15.1) (2022-03-10)
152
+
153
+ - fixed:
154
+ - Fix handling of stdin option with config file for #534. Thank you @arnestaphorsius :+1:
155
+
156
+ ### [v3.15.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.15.0) (2022-02-06)
157
+
158
+ - features:
159
+ - Support mobile wallet media types(`application/jwt` and `application/vnd.apple.pkpass`) for #530. Thank you @eostrom :+1:
160
+
161
+ ### [v3.14.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.14.0) (2022-01-27)
147
162
 
148
163
  - features:
149
164
  - Support the nested `allOf` and `oneOf` schema for #513. Thank you for your report @Cry0nicS :+1:
@@ -112,5 +112,8 @@ The base JSON Schema version is Draft-07
112
112
  - `application/x-www-form-urlencoded`
113
113
  - `application/*+json`
114
114
  - `application/octet-stream`
115
+ - `application/jwt`
116
+ - `application/vnd.apple.pkpass`
115
117
  - `multipart/form-data`
116
118
  - `text/*`
119
+ - `image/*`
package/dist/cli.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- const fs = (0, tslib_1.__importStar)(require("fs"));
4
+ const fs = tslib_1.__importStar(require("fs"));
5
5
  const path_1 = require("path");
6
- const commandOptions_1 = (0, tslib_1.__importStar)(require("./commandOptions"));
7
- const core_1 = (0, tslib_1.__importStar)(require("./core"));
8
- const config_1 = (0, tslib_1.__importStar)(require("./core/config"));
6
+ const commandOptions_1 = tslib_1.__importStar(require("./commandOptions"));
7
+ const core_1 = tslib_1.__importStar(require("./core"));
8
+ const config_1 = tslib_1.__importStar(require("./core/config"));
9
9
  const utils_1 = require("./utils");
10
10
  async function loadContents() {
11
11
  let contents = [];
@@ -9,7 +9,6 @@ export declare class CommandOptions {
9
9
  target?: string;
10
10
  info?: boolean;
11
11
  outputAST?: boolean;
12
- isReadFromStdin(): boolean;
13
12
  }
14
13
  declare const opts: CommandOptions;
15
14
  export default opts;
@@ -2,16 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.clear = exports.initialize = exports.CommandOptions = exports.defaultConfigFile = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const commander = (0, tslib_1.__importStar)(require("commander"));
5
+ const commander = tslib_1.__importStar(require("commander"));
6
6
  exports.defaultConfigFile = 'dtsgen.json';
7
7
  class CommandOptions {
8
8
  constructor() {
9
9
  this.files = [];
10
10
  this.urls = [];
11
11
  }
12
- isReadFromStdin() {
13
- return (this.stdin || (this.files.length === 0 && this.urls.length === 0));
14
- }
15
12
  }
16
13
  exports.CommandOptions = CommandOptions;
17
14
  const opts = new CommandOptions();
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addOptionalInformation = exports.addComment = exports.buildTypeReferenceNode = exports.buildTupleTypeNode = exports.buildIntersectionTypeNode = exports.buildUnionTypeNode = exports.buildFreeFormObjectTypeLiteralNode = exports.buildTypeLiteralNode = exports.buildIndexSignatureNode = exports.buildPropertySignature = exports.buildTypeAliasNode = exports.buildInterfaceNode = exports.buildNamespaceNode = exports.buildBooleanLiteralTypeNode = exports.buildNumericLiteralTypeNode = exports.buildStringLiteralTypeNode = exports.buildNullKeyword = exports.buildSimpleArrayNode = exports.buildStringKeyword = exports.buildVoidKeyword = exports.buildUnknownKeyword = exports.buildNeverKeyword = exports.buildAnyKeyword = exports.buildKeyword = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const ts = (0, tslib_1.__importStar)(require("typescript"));
6
- const config_1 = (0, tslib_1.__importDefault)(require("./config"));
5
+ const ts = tslib_1.__importStar(require("typescript"));
6
+ const config_1 = tslib_1.__importDefault(require("./config"));
7
7
  const validateIdentifier_1 = require("./validateIdentifier");
8
8
  function buildTypeNameIdentifier(name) {
9
9
  return ts.factory.createIdentifier((0, validateIdentifier_1.toValidIdentifier)(name, config_1.default.target));
@@ -87,7 +87,7 @@ function buildTypeAliasNode(id, type, root) {
87
87
  exports.buildTypeAliasNode = buildTypeAliasNode;
88
88
  function buildPropertySignature(_schema, propertyName, valueType, required, isPattern) {
89
89
  const modifiers = undefined;
90
- const questionToken = required == null || required.indexOf(propertyName) < 0
90
+ const questionToken = required == null || !required.includes(propertyName)
91
91
  ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
92
92
  : undefined;
93
93
  if (isPattern) {
@@ -147,7 +147,8 @@ function buildTupleTypeNode(types, minItems, maxItems, additionalItems) {
147
147
  for (let i = 0; i < itemCount; i++) {
148
148
  let node = typesIsArray(types)
149
149
  ? i < types.length
150
- ? types[i]
150
+ ?
151
+ types[i]
151
152
  : additionalItems !== undefined
152
153
  ? additionalItems === false
153
154
  ? buildNeverKeyword()
@@ -172,12 +173,17 @@ function buildTupleTypeNode(types, minItems, maxItems, additionalItems) {
172
173
  exports.buildTupleTypeNode = buildTupleTypeNode;
173
174
  function buildTypeReferenceNode(schema, currentSchema) {
174
175
  const typeName = getTypename(schema.id, currentSchema);
175
- if (typeName.length === 0) {
176
- throw new Error('TypeName array must not be empty.');
176
+ let node;
177
+ for (const name of typeName) {
178
+ if (node == null) {
179
+ node = buildTypeNameIdentifier(name);
180
+ }
181
+ else {
182
+ node = ts.factory.createQualifiedName(node, buildTypeNameIdentifier(name));
183
+ }
177
184
  }
178
- let node = buildTypeNameIdentifier(typeName[0]);
179
- for (let i = 1; i < typeName.length; i++) {
180
- node = ts.factory.createQualifiedName(node, buildTypeNameIdentifier(typeName[i]));
185
+ if (node == null) {
186
+ throw new Error('TypeName array must not be empty.');
181
187
  }
182
188
  return ts.factory.createTypeReferenceNode(node, undefined);
183
189
  }
@@ -185,29 +191,28 @@ exports.buildTypeReferenceNode = buildTypeReferenceNode;
185
191
  function getTypename(id, baseSchema) {
186
192
  const result = id.toNames();
187
193
  const baseId = baseSchema.id;
188
- if (baseId) {
189
- const baseTypes = baseId.toNames().slice(0, -1);
190
- for (const type of baseTypes) {
191
- if (result.length === 1) {
192
- break;
193
- }
194
- if (result[0] === type) {
195
- result.shift();
196
- }
197
- else {
198
- break;
199
- }
194
+ const baseTypes = baseId.toNames().slice(0, -1);
195
+ for (const type of baseTypes) {
196
+ if (result.length === 1) {
197
+ break;
198
+ }
199
+ if (result[0] === type) {
200
+ result.shift();
201
+ }
202
+ else {
203
+ break;
200
204
  }
201
205
  }
202
206
  return result;
203
207
  }
204
208
  function addComment(node, schema, terminate) {
209
+ var _a;
205
210
  const comments = getComment(schema);
206
211
  if (comments.length === 0) {
207
212
  return node;
208
213
  }
209
214
  else if (!terminate && comments.length === 1) {
210
- return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, ` ${comments[0]} `, false);
215
+ return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, ` ${(_a = comments[0]) !== null && _a !== void 0 ? _a : ''} `, false);
211
216
  }
212
217
  else {
213
218
  let result = '*\n';
@@ -276,11 +281,7 @@ function addOptionalInformation(node, schema, terminate) {
276
281
  }
277
282
  exports.addOptionalInformation = addOptionalInformation;
278
283
  function getLastTypeName(id) {
284
+ var _a;
279
285
  const names = id.toNames();
280
- if (names.length > 0) {
281
- return names[names.length - 1];
282
- }
283
- else {
284
- return '';
285
- }
286
+ return (_a = names[names.length - 1]) !== null && _a !== void 0 ? _a : '';
286
287
  }
@@ -1,11 +1,7 @@
1
1
  import { ScriptTarget } from 'typescript';
2
2
  export interface Config {
3
3
  configFile?: string;
4
- input: {
5
- files: string[];
6
- urls: string[];
7
- stdin: boolean;
8
- };
4
+ input: InputConfig;
9
5
  outputFile?: string;
10
6
  target: ScriptTarget;
11
7
  outputAST: boolean;
@@ -13,8 +9,16 @@ export interface Config {
13
9
  [pluginName: string]: boolean | Record<string, unknown>;
14
10
  };
15
11
  }
12
+ interface InputConfig {
13
+ files: string[];
14
+ urls: string[];
15
+ stdin: boolean;
16
+ }
17
+ export declare type PartialConfig = Omit<Partial<Config>, 'input'> & {
18
+ input?: Partial<InputConfig>;
19
+ };
16
20
  declare let config: Config;
17
- export declare function setConfig(input: Partial<Config>): void;
21
+ export declare function setConfig(input: PartialConfig): void;
18
22
  export declare function clearToDefault(): void;
19
23
  export declare function showConfig(version: string, c: Config): Promise<void>;
20
24
  export default config;
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
5
- const ts = (0, tslib_1.__importStar)(require("typescript"));
4
+ const debug_1 = tslib_1.__importDefault(require("debug"));
5
+ const ts = tslib_1.__importStar(require("typescript"));
6
6
  const jsonPointer_1 = require("../jsonPointer");
7
- const ast = (0, tslib_1.__importStar)(require("./astBuilder"));
8
- const config_1 = (0, tslib_1.__importDefault)(require("./config"));
7
+ const ast = tslib_1.__importStar(require("./astBuilder"));
8
+ const config_1 = tslib_1.__importDefault(require("./config"));
9
9
  const jsonSchema_1 = require("./jsonSchema");
10
- const referenceResolver_1 = (0, tslib_1.__importDefault)(require("./referenceResolver"));
10
+ const referenceResolver_1 = tslib_1.__importDefault(require("./referenceResolver"));
11
11
  const type_1 = require("./type");
12
12
  const typeTree_1 = require("./typeTree");
13
- const utils = (0, tslib_1.__importStar)(require("./utils"));
13
+ const utils = tslib_1.__importStar(require("./utils"));
14
14
  const debug = (0, debug_1.default)('dtsgen');
15
15
  class DtsGenerator {
16
16
  constructor(contents) {
@@ -243,9 +243,6 @@ class DtsGenerator {
243
243
  const content = schema.content;
244
244
  if (content.$ref) {
245
245
  const ref = this.resolver.dereference(content.$ref);
246
- if (ref.id == null) {
247
- throw new Error('target referenced id is nothing: ' + content.$ref);
248
- }
249
246
  const refSchema = this.normalizeContent(ref);
250
247
  const node = ast.addOptionalInformation(ast.addComment(ast.buildTypeReferenceNode(refSchema, this.currentSchema), refSchema, false), refSchema, false);
251
248
  return node;
@@ -280,6 +277,7 @@ class DtsGenerator {
280
277
  }
281
278
  }
282
279
  checkExistOtherType(content, base) {
280
+ var _a;
283
281
  const schema = Object.assign({}, base, { content });
284
282
  const result = this.generateLiteralTypeProperty(schema, false);
285
283
  if (result.kind === ts.SyntaxKind.AnyKeyword) {
@@ -288,7 +286,7 @@ class DtsGenerator {
288
286
  else if (result.kind === ts.SyntaxKind.TypeLiteral) {
289
287
  const node = result;
290
288
  if (node.members.length === 1 &&
291
- node.members[0].kind === ts.SyntaxKind.IndexSignature) {
289
+ ((_a = node.members[0]) === null || _a === void 0 ? void 0 : _a.kind) === ts.SyntaxKind.IndexSignature) {
292
290
  return undefined;
293
291
  }
294
292
  }
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SchemaId = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const config_1 = require("./config");
6
- const dtsGenerator_1 = (0, tslib_1.__importDefault)(require("./dtsGenerator"));
6
+ const dtsGenerator_1 = tslib_1.__importDefault(require("./dtsGenerator"));
7
7
  var schemaId_1 = require("./schemaId");
8
- Object.defineProperty(exports, "SchemaId", { enumerable: true, get: function () { return (0, tslib_1.__importDefault)(schemaId_1).default; } });
9
- (0, tslib_1.__exportStar)(require("./type"), exports);
8
+ Object.defineProperty(exports, "SchemaId", { enumerable: true, get: function () { return tslib_1.__importDefault(schemaId_1).default; } });
9
+ tslib_1.__exportStar(require("./type"), exports);
10
10
  async function dtsGenerator(options) {
11
11
  if (options.config) {
12
12
  (0, config_1.setConfig)(options.config);
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.selectSchemaType = exports.searchAllSubSchema = exports.setId = exports.getId = exports.getSubSchema = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const JsonPointer = (0, tslib_1.__importStar)(require("../jsonPointer"));
6
- const schemaId_1 = (0, tslib_1.__importDefault)(require("./schemaId"));
5
+ const JsonPointer = tslib_1.__importStar(require("../jsonPointer"));
6
+ const schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
7
7
  const type_1 = require("./type");
8
8
  function getSubSchema(rootSchema, pointer, id) {
9
9
  const content = JsonPointer.get(rootSchema.content, JsonPointer.parse(pointer));
@@ -144,7 +144,9 @@ function searchAllSubSchema(schema, onFoundSchema, onFoundReference) {
144
144
  }
145
145
  Object.keys(obj).forEach((key) => {
146
146
  const item = obj[key];
147
- f(item, keys.concat(convertKeyToTypeName(key)));
147
+ if (item != null) {
148
+ f(item, keys.concat(convertKeyToTypeName(key)));
149
+ }
148
150
  });
149
151
  }
150
152
  function setSubIdToParameterObject(obj, keys) {
@@ -256,9 +258,11 @@ function searchAllSubSchema(schema, onFoundSchema, onFoundReference) {
256
258
  return;
257
259
  }
258
260
  for (const mime of Object.keys(types)) {
259
- if (/^text\/|^(?:application\/x-www-form-urlencoded$|^application\/([a-z0-9-_.]+\+)?json)$|^application\/octet-stream$|^multipart\/form-data$/.test(mime)) {
261
+ if (/^text\/|^(?:application\/x-www-form-urlencoded$|^application\/([a-z0-9-_.]+\+)?json)$|^application\/octet-stream$|^application\/jwt$|^application\/vnd.apple.pkpass$|^multipart\/form-data$|^image\//.test(mime)) {
260
262
  const mt = types[mime];
261
- setSubId(mt.schema, keys);
263
+ if (mt != null) {
264
+ setSubId(mt.schema, keys);
265
+ }
262
266
  }
263
267
  }
264
268
  }
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  require("cross-fetch/polyfill");
5
- const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
5
+ const debug_1 = tslib_1.__importDefault(require("debug"));
6
6
  const jsonSchema_1 = require("./jsonSchema");
7
- const schemaId_1 = (0, tslib_1.__importDefault)(require("./schemaId"));
7
+ const schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
8
8
  const type_1 = require("./type");
9
9
  const debug = (0, debug_1.default)('dtsgen');
10
10
  class ReferenceResolver {
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- const url = (0, tslib_1.__importStar)(require("url"));
4
+ const url = tslib_1.__importStar(require("url"));
5
5
  const validateIdentifier_1 = require("./validateIdentifier");
6
6
  class SchemaId {
7
7
  constructor(inputId, parentIds) {
8
- var _a;
9
8
  this.inputId = inputId;
10
9
  let absoluteId = url.resolve('', inputId);
11
10
  if (parentIds) {
@@ -15,16 +14,16 @@ class SchemaId {
15
14
  }
16
15
  });
17
16
  }
18
- if (absoluteId.indexOf('#') < 0) {
17
+ if (!absoluteId.includes('#')) {
19
18
  absoluteId += '#';
20
19
  }
21
- if (absoluteId.indexOf('://') < 0 &&
22
- absoluteId[0] !== '/' &&
23
- absoluteId[0] !== '#') {
20
+ if (!absoluteId.includes('://') &&
21
+ !absoluteId.startsWith('/') &&
22
+ !absoluteId.startsWith('#')) {
24
23
  absoluteId = '/' + absoluteId;
25
24
  }
26
25
  this.id = url.parse(absoluteId);
27
- this.absoluteId = (_a = this.id.href) !== null && _a !== void 0 ? _a : '';
26
+ this.absoluteId = this.id.href;
28
27
  }
29
28
  getAbsoluteId() {
30
29
  return this.absoluteId;
@@ -39,11 +38,11 @@ class SchemaId {
39
38
  return this.absoluteId.replace(/#.*$/, '#');
40
39
  }
41
40
  existsJsonPointerHash() {
42
- return this.absoluteId === '#' || /#\//.test(this.absoluteId);
41
+ return this.absoluteId === '#' || this.absoluteId.includes('#/');
43
42
  }
44
43
  getJsonPointerHash() {
45
44
  const m = /(#\/.*)$/.exec(this.absoluteId);
46
- if (m == null) {
45
+ if ((m === null || m === void 0 ? void 0 : m[1]) == null) {
47
46
  return '#';
48
47
  }
49
48
  return decodeURIComponent(m[1]);
package/dist/core/type.js CHANGED
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loadPlugin = exports.parseFileContent = exports.readSchemaFromUrl = exports.readSchemasFromFile = exports.readSchemaFromStdin = exports.parseSchema = exports.isJsonSchemaDraft04 = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const fs = (0, tslib_1.__importStar)(require("fs"));
5
+ const fs = tslib_1.__importStar(require("fs"));
6
6
  const path_1 = require("path");
7
7
  const js_yaml_1 = require("js-yaml");
8
8
  const utils_1 = require("../utils");
9
9
  const jsonSchema_1 = require("./jsonSchema");
10
- const schemaId_1 = (0, tslib_1.__importDefault)(require("./schemaId"));
10
+ const schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
11
11
  exports.ts = require("typescript");
12
12
  function isJsonSchemaDraft04(_content, type) {
13
13
  return type === 'Draft04';
@@ -81,8 +81,8 @@ async function loadPlugin(name, option) {
81
81
  if (!option) {
82
82
  return undefined;
83
83
  }
84
- const mod = (await Promise.resolve().then(() => (0, tslib_1.__importStar)(require(name))));
85
- if (!(mod === null || mod === void 0 ? void 0 : mod.default)) {
84
+ const mod = (await Promise.resolve().then(() => tslib_1.__importStar(require(name))));
85
+ if (!mod.default) {
86
86
  console.warn(`The plugin (${name}) is invalid module. That is not default export format.`);
87
87
  return undefined;
88
88
  }
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeSchema = exports.reduceTypes = exports.toTSType = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
6
- const ts = (0, tslib_1.__importStar)(require("typescript"));
5
+ const debug_1 = tslib_1.__importDefault(require("debug"));
6
+ const ts = tslib_1.__importStar(require("typescript"));
7
7
  const debug = (0, debug_1.default)('dtsgen');
8
8
  function toTSType(type, debugSource) {
9
9
  switch (type) {
@@ -43,7 +43,7 @@ function set(obj, path, value) {
43
43
  }
44
44
  exports.set = set;
45
45
  function parse(s) {
46
- if (/^#/.test(s)) {
46
+ if (s.startsWith('#')) {
47
47
  s = s.substring(1);
48
48
  }
49
49
  const path = s.split('/');
package/dist/utils.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import glob from 'glob';
3
4
  import { CommandOptions } from './commandOptions';
4
- import { Config } from './core/config';
5
+ import { PartialConfig } from './core/config';
5
6
  export declare function readStream(stream: NodeJS.ReadableStream, encoding?: BufferEncoding): Promise<string>;
6
7
  export declare function readUrl(url: string): Promise<string>;
7
8
  export declare function buildProxyOptions(url: string): RequestInit | undefined;
8
9
  export declare function globFiles(pattern: string, options?: glob.IOptions): Promise<string[]>;
9
- export declare function readConfig(options: CommandOptions): Partial<Config>;
10
+ export declare function readConfig(options: CommandOptions): PartialConfig;
package/dist/utils.js CHANGED
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readConfig = exports.globFiles = exports.buildProxyOptions = exports.readUrl = exports.readStream = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const fs = (0, tslib_1.__importStar)(require("fs"));
5
+ const fs = tslib_1.__importStar(require("fs"));
6
6
  const url_1 = require("url");
7
- const glob_1 = (0, tslib_1.__importDefault)(require("glob"));
8
- const https_proxy_agent_1 = (0, tslib_1.__importDefault)(require("https-proxy-agent"));
7
+ const glob_1 = tslib_1.__importDefault(require("glob"));
8
+ const https_proxy_agent_1 = tslib_1.__importDefault(require("https-proxy-agent"));
9
9
  const typescript_1 = require("typescript");
10
10
  const commandOptions_1 = require("./commandOptions");
11
11
  function readStream(stream, encoding = 'utf8') {
@@ -107,7 +107,14 @@ function readConfig(options) {
107
107
  else if (pc.input.urls == null) {
108
108
  pc.input.urls = [];
109
109
  }
110
- pc.input.stdin = options.isReadFromStdin();
110
+ if (options.stdin != null) {
111
+ pc.input.stdin = options.stdin;
112
+ }
113
+ else {
114
+ pc.input.stdin =
115
+ pc.input.stdin ||
116
+ (pc.input.files.length === 0 && pc.input.urls.length === 0);
117
+ }
111
118
  if (options.out != null) {
112
119
  pc.outputFile = options.out;
113
120
  }
package/package.json CHANGED
@@ -1,85 +1,85 @@
1
1
  {
2
- "name": "dtsgenerator",
3
- "version": "3.14.0",
4
- "description": "TypeScript d.ts file generator for JSON Schema file",
5
- "bin": {
6
- "dtsgen": "bin/dtsgen"
7
- },
8
- "main": "dist/core/",
9
- "typings": "dist/core/index.d.ts",
10
- "scripts": {
11
- "build": "tsc -p ./tsconfig-build.json",
12
- "clean": "rimraf dist/ build/ {src,test}/**/*.{js,js.map} *.tsbuildinfo",
13
- "compile": "tsc -p .",
14
- "lint": "eslint --fix --ext ts src/ test/",
15
- "format": "prettier --write 'src/**/*.ts' 'test/*.ts' 'test/core/*.ts'",
16
- "precompile": "npm run lint && npm run format",
17
- "prepublishOnly": "npm run build",
18
- "pretest": "npm run compile",
19
- "do-test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --require source-map-support/register --colors test/*_test.ts test/**/*_test.ts",
20
- "test": "nyc npm run do-test",
21
- "test:update-snapshot": "UPDATE_SNAPSHOT=1 npm run test",
22
- "coverage": "nyc report --reporter=lcov",
23
- "prepare": "husky install"
24
- },
25
- "directories": {
26
- "test": "test/"
27
- },
28
- "keywords": [
29
- "TypeScript",
30
- "JSON Schema",
31
- "OpenAPI"
32
- ],
33
- "author": "horiuchi",
34
- "license": "MIT",
35
- "homepage": "https://github.com/horiuchi/dtsgenerator",
36
- "repository": {
37
- "type": "git",
38
- "url": "git@github.com:horiuchi/dtsgenerator.git"
39
- },
40
- "bugs": {
41
- "url": "https://github.com/horiuchi/dtsgenerator/issues"
42
- },
43
- "engines": {
44
- "node": ">= 10.0"
45
- },
46
- "dependencies": {
47
- "commander": "^8.3.0",
48
- "cross-fetch": "^3.1.5",
49
- "debug": "^4.3.3",
50
- "glob": "^7.2.0",
51
- "https-proxy-agent": "^5.0.0",
52
- "js-yaml": "^4.1.0",
53
- "tslib": "^2.3.1",
54
- "typescript": "^4.5.5"
55
- },
56
- "devDependencies": {
57
- "@dtsgenerator/do-nothing": "^2.5.1",
58
- "@dtsgenerator/eslint-config": "^0.5.0",
59
- "@dtsgenerator/replace-namespace": "^1.5.1",
60
- "@dtsgenerator/single-quote": "^1.6.1",
61
- "@istanbuljs/nyc-config-typescript": "^1.0.2",
62
- "@types/debug": "^4.1.7",
63
- "@types/glob": "^7.2.0",
64
- "@types/js-yaml": "^4.0.5",
65
- "@types/mkdirp": "^1.0.2",
66
- "@types/mocha": "^9.1.0",
67
- "@types/node": "^16.11.21",
68
- "cross-env": "^7.0.3",
69
- "eslint": "^8.7.0",
70
- "husky": "^7.0.4",
71
- "lint-staged": "^12.3.2",
72
- "mocha": "^9.2.0",
73
- "nyc": "^15.1.0",
74
- "prettier": "^2.5.1",
75
- "rimraf": "^3.0.2",
76
- "source-map-support": "^0.5.21",
77
- "ts-node": "^10.4.0"
78
- },
79
- "lint-staged": {
80
- "**/*.ts": [
81
- "prettier --write",
82
- "eslint --fix"
83
- ]
84
- }
2
+ "name": "dtsgenerator",
3
+ "version": "3.16.0",
4
+ "description": "TypeScript d.ts file generator for JSON Schema file",
5
+ "bin": {
6
+ "dtsgen": "bin/dtsgen"
7
+ },
8
+ "main": "dist/core/",
9
+ "typings": "dist/core/index.d.ts",
10
+ "scripts": {
11
+ "build": "tsc -p ./tsconfig-build.json",
12
+ "clean": "rimraf dist/ build/ {src,test}/**/*.{js,js.map} *.tsbuildinfo",
13
+ "compile": "tsc -p .",
14
+ "lint": "eslint --fix --ext ts src/ test/",
15
+ "format": "prettier --write 'src/**/*.ts' 'test/*.ts' 'test/core/*.ts'",
16
+ "precompile": "npm run lint && npm run format",
17
+ "prepublishOnly": "npm run build",
18
+ "pretest": "npm run compile",
19
+ "do-test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --require source-map-support/register --colors test/*_test.ts test/**/*_test.ts",
20
+ "test": "nyc npm run do-test",
21
+ "test:update-snapshot": "UPDATE_SNAPSHOT=1 npm run test",
22
+ "coverage": "nyc report --reporter=lcov",
23
+ "prepare": "husky install"
24
+ },
25
+ "directories": {
26
+ "test": "test/"
27
+ },
28
+ "keywords": [
29
+ "TypeScript",
30
+ "JSON Schema",
31
+ "OpenAPI"
32
+ ],
33
+ "author": "horiuchi",
34
+ "license": "MIT",
35
+ "homepage": "https://github.com/horiuchi/dtsgenerator",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git@github.com:horiuchi/dtsgenerator.git"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/horiuchi/dtsgenerator/issues"
42
+ },
43
+ "engines": {
44
+ "node": ">= 10.0"
45
+ },
46
+ "dependencies": {
47
+ "commander": "^9.3.0",
48
+ "cross-fetch": "^3.1.5",
49
+ "debug": "^4.3.4",
50
+ "glob": "^8.0.3",
51
+ "https-proxy-agent": "^5.0.1",
52
+ "js-yaml": "^4.1.0",
53
+ "tslib": "^2.4.0",
54
+ "typescript": "^4.7.3"
55
+ },
56
+ "devDependencies": {
57
+ "@dtsgenerator/do-nothing": "^2.5.2",
58
+ "@dtsgenerator/eslint-config": "^0.6.1",
59
+ "@dtsgenerator/replace-namespace": "^1.5.2",
60
+ "@dtsgenerator/single-quote": "^1.6.2",
61
+ "@istanbuljs/nyc-config-typescript": "^1.0.2",
62
+ "@types/debug": "^4.1.7",
63
+ "@types/glob": "^7.2.0",
64
+ "@types/js-yaml": "^4.0.5",
65
+ "@types/mkdirp": "^1.0.2",
66
+ "@types/mocha": "^9.1.1",
67
+ "@types/node": "^16.11.39",
68
+ "cross-env": "^7.0.3",
69
+ "eslint": "^8.17.0",
70
+ "husky": "^8.0.1",
71
+ "lint-staged": "^13.0.1",
72
+ "mocha": "^10.0.0",
73
+ "nyc": "^15.1.0",
74
+ "prettier": "^2.6.2",
75
+ "rimraf": "^3.0.2",
76
+ "source-map-support": "^0.5.21",
77
+ "ts-node": "^10.8.1"
78
+ },
79
+ "lint-staged": {
80
+ "**/*.ts": [
81
+ "prettier --write",
82
+ "eslint --fix"
83
+ ]
84
+ }
85
85
  }
@@ -11,12 +11,6 @@ export class CommandOptions {
11
11
  public target?: string;
12
12
  public info?: boolean;
13
13
  public outputAST?: boolean;
14
-
15
- public isReadFromStdin(): boolean {
16
- return (
17
- this.stdin || (this.files.length === 0 && this.urls.length === 0)
18
- );
19
- }
20
14
  }
21
15
 
22
16
  const opts = new CommandOptions();
@@ -46,8 +40,8 @@ function parse(options: CommandOptions, argv: string[]): commander.Command {
46
40
  return memo;
47
41
  }
48
42
 
49
- // eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment
50
- const pkg: Record<string, string> = require('../package.json');
43
+ // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
44
+ const pkg: { name: string; version: string } = require('../package.json');
51
45
 
52
46
  // <hoge> is required, [hoge] is optional
53
47
  program
@@ -121,7 +121,7 @@ export function buildPropertySignature(
121
121
  ): ts.PropertySignature | ts.IndexSignatureDeclaration {
122
122
  const modifiers = undefined;
123
123
  const questionToken =
124
- required == null || required.indexOf(propertyName) < 0
124
+ required == null || !required.includes(propertyName)
125
125
  ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
126
126
  : undefined;
127
127
  if (isPattern) {
@@ -230,7 +230,8 @@ export function buildTupleTypeNode(
230
230
  for (let i = 0; i < itemCount; i++) {
231
231
  let node = typesIsArray(types)
232
232
  ? i < types.length
233
- ? types[i]
233
+ ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
234
+ types[i]!
234
235
  : additionalItems !== undefined
235
236
  ? additionalItems === false
236
237
  ? buildNeverKeyword()
@@ -266,32 +267,34 @@ export function buildTypeReferenceNode(
266
267
  currentSchema: Schema
267
268
  ): ts.TypeReferenceNode {
268
269
  const typeName = getTypename(schema.id, currentSchema);
269
- if (typeName.length === 0) {
270
- throw new Error('TypeName array must not be empty.');
270
+ let node: ts.EntityName | undefined;
271
+ for (const name of typeName) {
272
+ if (node == null) {
273
+ node = buildTypeNameIdentifier(name);
274
+ } else {
275
+ node = ts.factory.createQualifiedName(
276
+ node,
277
+ buildTypeNameIdentifier(name)
278
+ );
279
+ }
271
280
  }
272
- let node: ts.EntityName = buildTypeNameIdentifier(typeName[0]);
273
- for (let i = 1; i < typeName.length; i++) {
274
- node = ts.factory.createQualifiedName(
275
- node,
276
- buildTypeNameIdentifier(typeName[i])
277
- );
281
+ if (node == null) {
282
+ throw new Error('TypeName array must not be empty.');
278
283
  }
279
284
  return ts.factory.createTypeReferenceNode(node, undefined);
280
285
  }
281
286
  function getTypename(id: SchemaId, baseSchema: Schema): string[] {
282
287
  const result = id.toNames();
283
288
  const baseId = baseSchema.id;
284
- if (baseId) {
285
- const baseTypes = baseId.toNames().slice(0, -1);
286
- for (const type of baseTypes) {
287
- if (result.length === 1) {
288
- break;
289
- }
290
- if (result[0] === type) {
291
- result.shift();
292
- } else {
293
- break;
294
- }
289
+ const baseTypes = baseId.toNames().slice(0, -1);
290
+ for (const type of baseTypes) {
291
+ if (result.length === 1) {
292
+ break;
293
+ }
294
+ if (result[0] === type) {
295
+ result.shift();
296
+ } else {
297
+ break;
295
298
  }
296
299
  }
297
300
  return result;
@@ -309,7 +312,7 @@ export function addComment<T extends ts.Node>(
309
312
  return ts.addSyntheticLeadingComment(
310
313
  node,
311
314
  ts.SyntaxKind.MultiLineCommentTrivia,
312
- ` ${comments[0]} `,
315
+ ` ${comments[0] ?? ''} `,
313
316
  false
314
317
  );
315
318
  } else {
@@ -394,9 +397,5 @@ export function addOptionalInformation<T extends ts.Node>(
394
397
 
395
398
  function getLastTypeName(id: SchemaId): string {
396
399
  const names = id.toNames();
397
- if (names.length > 0) {
398
- return names[names.length - 1];
399
- } else {
400
- return '';
401
- }
400
+ return names[names.length - 1] ?? '';
402
401
  }
@@ -3,11 +3,7 @@ import { Plugin, loadPlugin } from './type';
3
3
 
4
4
  export interface Config {
5
5
  configFile?: string;
6
- input: {
7
- files: string[];
8
- urls: string[];
9
- stdin: boolean;
10
- };
6
+ input: InputConfig;
11
7
  outputFile?: string;
12
8
  target: ScriptTarget;
13
9
  outputAST: boolean;
@@ -17,6 +13,16 @@ export interface Config {
17
13
  };
18
14
  }
19
15
 
16
+ interface InputConfig {
17
+ files: string[];
18
+ urls: string[];
19
+ stdin: boolean;
20
+ }
21
+
22
+ export type PartialConfig = Omit<Partial<Config>, 'input'> & {
23
+ input?: Partial<InputConfig>;
24
+ };
25
+
20
26
  const defaultConfig: Config = {
21
27
  configFile: undefined,
22
28
  input: {
@@ -32,7 +38,7 @@ const defaultConfig: Config = {
32
38
 
33
39
  let config: Config = Object.assign({}, defaultConfig);
34
40
 
35
- export function setConfig(input: Partial<Config>): void {
41
+ export function setConfig(input: PartialConfig): void {
36
42
  config = Object.assign(config, input);
37
43
  }
38
44
 
@@ -57,7 +57,8 @@ export default class DtsGenerator {
57
57
 
58
58
  const postProcess = await this.getPostProcess(plugins.post);
59
59
  const result = ts.transform(file, postProcess);
60
- const transformedNodes = result.transformed[0];
60
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61
+ const transformedNodes = result.transformed[0]!;
61
62
 
62
63
  const transformedContent = config.outputAST
63
64
  ? JSON.stringify(transformedNodes, null, 2)
@@ -374,11 +375,6 @@ export default class DtsGenerator {
374
375
  const content = schema.content;
375
376
  if (content.$ref) {
376
377
  const ref = this.resolver.dereference(content.$ref);
377
- if (ref.id == null) {
378
- throw new Error(
379
- 'target referenced id is nothing: ' + content.$ref
380
- );
381
- }
382
378
  const refSchema = this.normalizeContent(ref);
383
379
  const node = ast.addOptionalInformation(
384
380
  ast.addComment(
@@ -448,7 +444,7 @@ export default class DtsGenerator {
448
444
  const node = result as ts.TypeLiteralNode;
449
445
  if (
450
446
  node.members.length === 1 &&
451
- node.members[0].kind === ts.SyntaxKind.IndexSignature
447
+ node.members[0]?.kind === ts.SyntaxKind.IndexSignature
452
448
  ) {
453
449
  return undefined;
454
450
  }
@@ -209,7 +209,9 @@ export function searchAllSubSchema(
209
209
  }
210
210
  Object.keys(obj).forEach((key) => {
211
211
  const item = obj[key];
212
- f(item, keys.concat(convertKeyToTypeName(key)));
212
+ if (item != null) {
213
+ f(item, keys.concat(convertKeyToTypeName(key)));
214
+ }
213
215
  });
214
216
  }
215
217
 
@@ -272,6 +274,7 @@ export function searchAllSubSchema(
272
274
  if ('$ref' in item) {
273
275
  setSubId(item, keys.concat(key));
274
276
  }
277
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
275
278
  if ('type' in item && item.in !== undefined) {
276
279
  setSubId(
277
280
  item as JsonSchemaDraft04.Schema,
@@ -343,7 +346,8 @@ export function searchAllSubSchema(
343
346
  }
344
347
  const operationId = ops.operationId;
345
348
  if (operationId) {
346
- keys = [keys[0], convertKeyToTypeName(operationId)];
349
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
350
+ keys = [keys[0]!, convertKeyToTypeName(operationId)];
347
351
  }
348
352
  setSubIdToParameters(ops.parameters, keys.concat('parameters'));
349
353
  setSubIdToResponsesV2(ops.responses, keys.concat('responses'));
@@ -379,12 +383,14 @@ export function searchAllSubSchema(
379
383
  }
380
384
  for (const mime of Object.keys(types)) {
381
385
  if (
382
- /^text\/|^(?:application\/x-www-form-urlencoded$|^application\/([a-z0-9-_.]+\+)?json)$|^application\/octet-stream$|^multipart\/form-data$/.test(
386
+ /^text\/|^(?:application\/x-www-form-urlencoded$|^application\/([a-z0-9-_.]+\+)?json)$|^application\/octet-stream$|^application\/jwt$|^application\/vnd.apple.pkpass$|^multipart\/form-data$|^image\//.test(
383
387
  mime
384
388
  )
385
389
  ) {
386
390
  const mt = types[mime];
387
- setSubId(mt.schema, keys);
391
+ if (mt != null) {
392
+ setSubId(mt.schema, keys);
393
+ }
388
394
  }
389
395
  }
390
396
  }
@@ -443,7 +449,8 @@ export function searchAllSubSchema(
443
449
  }
444
450
  const operationId = ops.operationId;
445
451
  if (operationId) {
446
- keys = [keys[0], convertKeyToTypeName(operationId)];
452
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
453
+ keys = [keys[0]!, convertKeyToTypeName(operationId)];
447
454
  }
448
455
  setSubIdToParameters(ops.parameters, keys.concat('parameters'));
449
456
  setSubIdToRequestBody(ops.requestBody, keys.concat('requestBody'));
@@ -579,6 +586,7 @@ export function selectSchemaType(content: JsonSchema | OpenApiSchema): {
579
586
  }
580
587
  }
581
588
  }
589
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
582
590
  if ('swagger' in content && content.swagger === '2.0') {
583
591
  return {
584
592
  type: 'Draft04',
@@ -15,18 +15,18 @@ export default class SchemaId {
15
15
  }
16
16
  });
17
17
  }
18
- if (absoluteId.indexOf('#') < 0) {
18
+ if (!absoluteId.includes('#')) {
19
19
  absoluteId += '#';
20
20
  }
21
21
  if (
22
- absoluteId.indexOf('://') < 0 &&
23
- absoluteId[0] !== '/' &&
24
- absoluteId[0] !== '#'
22
+ !absoluteId.includes('://') &&
23
+ !absoluteId.startsWith('/') &&
24
+ !absoluteId.startsWith('#')
25
25
  ) {
26
26
  absoluteId = '/' + absoluteId;
27
27
  }
28
28
  this.id = url.parse(absoluteId);
29
- this.absoluteId = this.id.href ?? '';
29
+ this.absoluteId = this.id.href;
30
30
  }
31
31
 
32
32
  public getAbsoluteId(): string {
@@ -42,11 +42,11 @@ export default class SchemaId {
42
42
  return this.absoluteId.replace(/#.*$/, '#');
43
43
  }
44
44
  public existsJsonPointerHash(): boolean {
45
- return this.absoluteId === '#' || /#\//.test(this.absoluteId);
45
+ return this.absoluteId === '#' || this.absoluteId.includes('#/');
46
46
  }
47
47
  public getJsonPointerHash(): string {
48
48
  const m = /(#\/.*)$/.exec(this.absoluteId);
49
- if (m == null) {
49
+ if (m?.[1] == null) {
50
50
  return '#';
51
51
  }
52
52
  return decodeURIComponent(m[1]);
package/src/core/type.ts CHANGED
@@ -142,7 +142,7 @@ export async function loadPlugin(
142
142
  }
143
143
 
144
144
  const mod = (await import(name)) as { default?: Plugin };
145
- if (!mod?.default) {
145
+ if (!mod.default) {
146
146
  console.warn(
147
147
  `The plugin (${name}) is invalid module. That is not default export format.`
148
148
  );
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
  import { ScriptTarget } from 'typescript';
2
3
 
3
4
  // via. https://github.com/microsoft/TypeScript/blob/v3.7.5/src/services/codefixes/importFixes.ts#L689
@@ -752,7 +753,7 @@ const unicodeESNextIdentifierPart = [
752
753
 
753
754
  function lookupInUnicodeMap(code: number, map: readonly number[]): boolean {
754
755
  // Bail out quickly if it couldn't possibly be in the map.
755
- if (code < map[0]) {
756
+ if (code < map[0]!) {
756
757
  return false;
757
758
  }
758
759
 
@@ -765,11 +766,11 @@ function lookupInUnicodeMap(code: number, map: readonly number[]): boolean {
765
766
  mid = lo + (hi - lo) / 2;
766
767
  // mid has to be even to catch a range's beginning
767
768
  mid -= mid % 2;
768
- if (map[mid] <= code && code <= map[mid + 1]) {
769
+ if (map[mid]! <= code && code <= map[mid + 1]!) {
769
770
  return true;
770
771
  }
771
772
 
772
- if (code < map[mid]) {
773
+ if (code < map[mid]!) {
773
774
  hi = mid;
774
775
  } else {
775
776
  lo = mid + 2;
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-assignment */
3
3
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
4
  /* eslint-disable @typescript-eslint/no-unsafe-return */
@@ -12,9 +12,9 @@ export function get(
12
12
  return obj;
13
13
  }
14
14
  let o = obj;
15
- const lastKey = path[path.length - 1];
15
+ const lastKey = path[path.length - 1]!;
16
16
  for (let i = 0; i < path.length - 1; i++) {
17
- const key = path[i];
17
+ const key = path[i]!;
18
18
  let next = o[key];
19
19
  if (next == null) {
20
20
  if (isCreateOnNotExists) {
@@ -34,9 +34,9 @@ export function set(obj: any, path: string[], value: any): void {
34
34
  return;
35
35
  }
36
36
  let o = obj;
37
- const lastKey = path[path.length - 1];
37
+ const lastKey = path[path.length - 1]!;
38
38
  for (let i = 0; i < path.length - 1; i++) {
39
- const key = path[i];
39
+ const key = path[i]!;
40
40
  let next = o[key];
41
41
  if (next == null) {
42
42
  next = {};
@@ -48,7 +48,7 @@ export function set(obj: any, path: string[], value: any): void {
48
48
  }
49
49
 
50
50
  export function parse(s: string): string[] {
51
- if (/^#/.test(s)) {
51
+ if (s.startsWith('#')) {
52
52
  s = s.substring(1);
53
53
  }
54
54
  const path = s.split('/');
package/src/utils.ts CHANGED
@@ -4,7 +4,7 @@ import glob from 'glob';
4
4
  import proxy from 'https-proxy-agent';
5
5
  import { ScriptTarget } from 'typescript';
6
6
  import { CommandOptions, defaultConfigFile } from './commandOptions';
7
- import { Config } from './core/config';
7
+ import { PartialConfig } from './core/config';
8
8
 
9
9
  export function readStream(
10
10
  stream: NodeJS.ReadableStream,
@@ -78,8 +78,8 @@ export function globFiles(
78
78
  });
79
79
  }
80
80
 
81
- export function readConfig(options: CommandOptions): Partial<Config> {
82
- let pc: Partial<Config> = {};
81
+ export function readConfig(options: CommandOptions): PartialConfig {
82
+ let pc: PartialConfig = {};
83
83
  const configFile = options.configFile ?? defaultConfigFile;
84
84
  try {
85
85
  pc = loadJSON(configFile);
@@ -109,7 +109,13 @@ export function readConfig(options: CommandOptions): Partial<Config> {
109
109
  } else if (pc.input.urls == null) {
110
110
  pc.input.urls = [];
111
111
  }
112
- pc.input.stdin = options.isReadFromStdin();
112
+ if (options.stdin != null) {
113
+ pc.input.stdin = options.stdin;
114
+ } else {
115
+ pc.input.stdin =
116
+ pc.input.stdin ||
117
+ (pc.input.files.length === 0 && pc.input.urls.length === 0);
118
+ }
113
119
 
114
120
  if (options.out != null) {
115
121
  pc.outputFile = options.out;
@@ -122,9 +128,9 @@ export function readConfig(options: CommandOptions): Partial<Config> {
122
128
  pc.outputAST = !!options.outputAST;
123
129
  return pc;
124
130
  }
125
- function loadJSON(file: string): Partial<Config> {
131
+ function loadJSON(file: string): PartialConfig {
126
132
  const content = fs.readFileSync(file, 'utf-8');
127
- return JSON.parse(content) as Partial<Config>;
133
+ return JSON.parse(content) as PartialConfig;
128
134
  }
129
135
  function convertToScriptTarget(target: string): ScriptTarget {
130
136
  switch (target.trim().toLowerCase()) {
package/tsconfig.json CHANGED
@@ -37,6 +37,7 @@
37
37
  "noUnusedParameters": true, /* Report errors on unused parameters. */
38
38
  "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39
39
  "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40
+ "noUncheckedIndexedAccess": true,
40
41
 
41
42
  /* Module Resolution Options */
42
43
  // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */