ttsc 0.2.0 → 0.3.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/bin/tsc CHANGED
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env node
2
- require('../lib/tsc.js')
1
+ #!/usr/bin/env node
2
+ require('../lib/tsc.js')
package/bin/tsserver CHANGED
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env node
2
- require('../lib/tsserver.js')
1
+ #!/usr/bin/env node
2
+ require('../lib/tsserver.js')
@@ -1,67 +1,67 @@
1
- import * as ts from 'typescript';
2
- export interface PluginConfig {
3
- /**
4
- * Language Server TypeScript Plugin name
5
- */
6
- name?: string;
7
- /**
8
- * Path to transformer or transformer module name
9
- */
10
- transform?: string;
11
- /**
12
- * The optional name of the exported transform plugin in the transform module.
13
- */
14
- import?: string;
15
- /**
16
- * Plugin entry point format type, default is program
17
- */
18
- type?: 'ls' | 'program' | 'config' | 'checker' | 'raw' | 'compilerOptions';
19
- /**
20
- * Should transformer applied after all ones
21
- */
22
- after?: boolean;
23
- /**
24
- * Should transformer applied for d.ts files, supports from TS2.9
25
- */
26
- afterDeclarations?: boolean;
27
- }
28
- export interface TransformerBasePlugin {
29
- before?: ts.TransformerFactory<ts.SourceFile>;
30
- after?: ts.TransformerFactory<ts.SourceFile>;
31
- afterDeclarations?: ts.TransformerFactory<ts.SourceFile | ts.Bundle>;
32
- }
33
- export type TransformerList = Required<ts.CustomTransformers>;
34
- export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
35
- export type LSPattern = (ls: ts.LanguageService, config: {}) => TransformerPlugin;
36
- export type ProgramPattern = (program: ts.Program, config: {}, helpers?: {
37
- ts: typeof ts;
38
- addDiagnostic: (diag: ts.Diagnostic) => void;
39
- }) => TransformerPlugin;
40
- export type CompilerOptionsPattern = (compilerOpts: ts.CompilerOptions, config: {}) => TransformerPlugin;
41
- export type ConfigPattern = (config: {}) => TransformerPlugin;
42
- export type TypeCheckerPattern = (checker: ts.TypeChecker, config: {}) => TransformerPlugin;
43
- export type RawPattern = (context: ts.TransformationContext, program: ts.Program, config: {}) => ts.Transformer<ts.SourceFile>;
44
- export type PluginFactory = LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
45
- /**
46
- * @example
47
- *
48
- * new PluginCreator([
49
- * {transform: '@zerollup/ts-transform-paths', someOption: '123'},
50
- * {transform: '@zerollup/ts-transform-paths', type: 'ls', someOption: '123'},
51
- * {transform: '@zerollup/ts-transform-paths', type: 'ls', after: true, someOption: '123'}
52
- * ]).createTransformers({ program })
53
- */
54
- export declare class PluginCreator {
55
- private typescript;
56
- private configs;
57
- private resolveBaseDir;
58
- constructor(typescript: typeof ts, configs: PluginConfig[], resolveBaseDir?: string);
59
- mergeTransformers(into: TransformerList, source: ts.CustomTransformers | TransformerBasePlugin): this;
60
- createTransformers(params: {
61
- program: ts.Program;
62
- } | {
63
- ls: ts.LanguageService;
64
- }, customTransformers?: ts.CustomTransformers): Required<ts.CustomTransformers>;
65
- private resolveFactory;
66
- private validateConfigs;
67
- }
1
+ import * as ts from 'typescript';
2
+ export interface PluginConfig {
3
+ /**
4
+ * Language Server TypeScript Plugin name
5
+ */
6
+ name?: string;
7
+ /**
8
+ * Path to transformer or transformer module name
9
+ */
10
+ transform?: string;
11
+ /**
12
+ * The optional name of the exported transform plugin in the transform module.
13
+ */
14
+ import?: string;
15
+ /**
16
+ * Plugin entry point format type, default is program
17
+ */
18
+ type?: 'ls' | 'program' | 'config' | 'checker' | 'raw' | 'compilerOptions';
19
+ /**
20
+ * Should transformer applied after all ones
21
+ */
22
+ after?: boolean;
23
+ /**
24
+ * Should transformer applied for d.ts files, supports from TS2.9
25
+ */
26
+ afterDeclarations?: boolean;
27
+ }
28
+ export interface TransformerBasePlugin {
29
+ before?: ts.TransformerFactory<ts.SourceFile>;
30
+ after?: ts.TransformerFactory<ts.SourceFile>;
31
+ afterDeclarations?: ts.TransformerFactory<ts.SourceFile | ts.Bundle>;
32
+ }
33
+ export type TransformerList = Required<ts.CustomTransformers>;
34
+ export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
35
+ export type LSPattern = (ls: ts.LanguageService, config: {}) => TransformerPlugin;
36
+ export type ProgramPattern = (program: ts.Program, config: {}, helpers?: {
37
+ ts: typeof ts;
38
+ addDiagnostic: (diag: ts.Diagnostic) => void;
39
+ }) => TransformerPlugin;
40
+ export type CompilerOptionsPattern = (compilerOpts: ts.CompilerOptions, config: {}) => TransformerPlugin;
41
+ export type ConfigPattern = (config: {}) => TransformerPlugin;
42
+ export type TypeCheckerPattern = (checker: ts.TypeChecker, config: {}) => TransformerPlugin;
43
+ export type RawPattern = (context: ts.TransformationContext, program: ts.Program, config: {}) => ts.Transformer<ts.SourceFile>;
44
+ export type PluginFactory = LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
45
+ /**
46
+ * @example
47
+ *
48
+ * new PluginCreator([
49
+ * {transform: '@zerollup/ts-transform-paths', someOption: '123'},
50
+ * {transform: '@zerollup/ts-transform-paths', type: 'ls', someOption: '123'},
51
+ * {transform: '@zerollup/ts-transform-paths', type: 'ls', after: true, someOption: '123'}
52
+ * ]).createTransformers({ program })
53
+ */
54
+ export declare class PluginCreator {
55
+ private typescript;
56
+ private configs;
57
+ private resolveBaseDir;
58
+ constructor(typescript: typeof ts, configs: PluginConfig[], resolveBaseDir?: string);
59
+ mergeTransformers(into: TransformerList, source: ts.CustomTransformers | TransformerBasePlugin): this;
60
+ createTransformers(params: {
61
+ program: ts.Program;
62
+ } | {
63
+ ls: ts.LanguageService;
64
+ }, customTransformers?: ts.CustomTransformers): Required<ts.CustomTransformers>;
65
+ private resolveFactory;
66
+ private validateConfigs;
67
+ }
@@ -1,190 +1,190 @@
1
- "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.PluginCreator = void 0;
15
- var resolve = require("resolve");
16
- var util_1 = require("util");
17
- var patchCreateProgram_1 = require("./patchCreateProgram");
18
- function createTransformerFromPattern(_a) {
19
- var typescript = _a.typescript, factory = _a.factory, config = _a.config, program = _a.program, ls = _a.ls;
20
- var transform = config.transform, after = config.after, afterDeclarations = config.afterDeclarations, name = config.name, type = config.type, cleanConfig = __rest(config, ["transform", "after", "afterDeclarations", "name", "type"]);
21
- if (!transform)
22
- throw new Error('Not a valid config entry: "transform" key not found');
23
- var ret;
24
- switch (config.type) {
25
- case 'ls':
26
- if (!ls)
27
- throw new Error("Plugin ".concat(transform, " need a LanguageService"));
28
- ret = factory(ls, cleanConfig);
29
- break;
30
- case 'config':
31
- ret = factory(cleanConfig);
32
- break;
33
- case 'compilerOptions':
34
- ret = factory(program.getCompilerOptions(), cleanConfig);
35
- break;
36
- case 'checker':
37
- ret = factory(program.getTypeChecker(), cleanConfig);
38
- break;
39
- case undefined:
40
- case 'program':
41
- ret = factory(program, cleanConfig, {
42
- ts: typescript,
43
- addDiagnostic: (0, patchCreateProgram_1.addDiagnosticFactory)(program),
44
- });
45
- break;
46
- case 'raw':
47
- ret = function (ctx) { return factory(ctx, program, cleanConfig); };
48
- break;
49
- default:
50
- return never(config.type);
51
- }
52
- if (typeof ret === 'function') {
53
- if (after)
54
- return { after: ret };
55
- else if (afterDeclarations) {
56
- return { afterDeclarations: ret };
57
- }
58
- else
59
- return { before: ret };
60
- }
61
- return ret;
62
- }
63
- function never(n) {
64
- throw new Error('Unexpected type: ' + n);
65
- }
66
- var tsNodeIncluded = false;
67
- // to fix recursion bug, see usage below
68
- var requireStack = [];
69
- /**
70
- * @example
71
- *
72
- * new PluginCreator([
73
- * {transform: '@zerollup/ts-transform-paths', someOption: '123'},
74
- * {transform: '@zerollup/ts-transform-paths', type: 'ls', someOption: '123'},
75
- * {transform: '@zerollup/ts-transform-paths', type: 'ls', after: true, someOption: '123'}
76
- * ]).createTransformers({ program })
77
- */
78
- var PluginCreator = /** @class */ (function () {
79
- function PluginCreator(typescript, configs, resolveBaseDir) {
80
- if (resolveBaseDir === void 0) { resolveBaseDir = process.cwd(); }
81
- this.typescript = typescript;
82
- this.configs = configs;
83
- this.resolveBaseDir = resolveBaseDir;
84
- this.validateConfigs(configs);
85
- }
86
- PluginCreator.prototype.mergeTransformers = function (into, source) {
87
- var _a, _b, _c;
88
- var slice = function (input) { return (Array.isArray(input) ? input.slice() : [input]); };
89
- if (source.before) {
90
- (_a = into.before).push.apply(_a, slice(source.before));
91
- }
92
- if (source.after) {
93
- (_b = into.after).push.apply(_b, slice(source.after));
94
- }
95
- if (source.afterDeclarations) {
96
- (_c = into.afterDeclarations).push.apply(_c, slice(source.afterDeclarations));
97
- }
98
- return this;
99
- };
100
- PluginCreator.prototype.createTransformers = function (params, customTransformers) {
101
- var chain = {
102
- before: [],
103
- after: [],
104
- afterDeclarations: [],
105
- };
106
- var ls;
107
- var program;
108
- if ('ls' in params) {
109
- ls = params.ls;
110
- program = ls.getProgram();
111
- }
112
- else {
113
- program = params.program;
114
- }
115
- for (var _i = 0, _a = this.configs; _i < _a.length; _i++) {
116
- var config = _a[_i];
117
- if (!config.transform) {
118
- continue;
119
- }
120
- var factory = this.resolveFactory(config.transform, config.import);
121
- // if recursion
122
- if (factory === undefined)
123
- continue;
124
- var transformer = createTransformerFromPattern({
125
- typescript: this.typescript,
126
- factory: factory,
127
- config: config,
128
- program: program,
129
- ls: ls,
130
- });
131
- this.mergeTransformers(chain, transformer);
132
- }
133
- // if we're given some custom transformers, they must be chained at the end
134
- if (customTransformers) {
135
- this.mergeTransformers(chain, customTransformers);
136
- }
137
- return chain;
138
- };
139
- PluginCreator.prototype.resolveFactory = function (transform, importKey) {
140
- if (importKey === void 0) { importKey = 'default'; }
141
- if (!tsNodeIncluded &&
142
- transform.match(/\.tsx?$/) &&
143
- (module.parent.parent === null ||
144
- module.parent.parent.parent === null ||
145
- module.parent.parent.parent.id.split(/[\/\\]/).indexOf('ts-node') === -1)) {
146
- require('ts-node').register({
147
- transpileOnly: true,
148
- skipProject: true,
149
- compilerOptions: {
150
- target: 'ES2018',
151
- jsx: 'react',
152
- esModuleInterop: true,
153
- module: 'commonjs',
154
- },
155
- });
156
- tsNodeIncluded = true;
157
- }
158
- var modulePath = resolve.sync(transform, { basedir: this.resolveBaseDir });
159
- // in ts-node occurs error cause recursion:
160
- // ts-node file.ts -> createTransformers -> require transformer.ts
161
- // -> createTransformers -> require transformer.ts -> ...
162
- // this happens cause ts-node uses to compile transformers the same config included this transformer
163
- // so this stack checks that if we already required this file we are in the reqursion
164
- if (requireStack.indexOf(modulePath) > -1)
165
- return;
166
- requireStack.push(modulePath);
167
- var commonjsModule = require(modulePath);
168
- requireStack.pop();
169
- var factoryModule = typeof commonjsModule === 'function' ? { default: commonjsModule } : commonjsModule;
170
- var factory = factoryModule[importKey];
171
- if (!factory) {
172
- throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" does not have an export \"").concat(importKey, "\": ") +
173
- (0, util_1.inspect)(factoryModule));
174
- }
175
- if (typeof factory !== 'function') {
176
- throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" export \"").concat(importKey, "\" is not a plugin: \"").concat((0, util_1.inspect)(factory), "\""));
177
- }
178
- return factory;
179
- };
180
- PluginCreator.prototype.validateConfigs = function (configs) {
181
- for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
182
- var config = configs_1[_i];
183
- if (!config.name && !config.transform) {
184
- throw new Error('tsconfig.json plugins error: transform must be present');
185
- }
186
- }
187
- };
188
- return PluginCreator;
189
- }());
190
- exports.PluginCreator = PluginCreator;
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.PluginCreator = void 0;
15
+ var resolve = require("resolve");
16
+ var util_1 = require("util");
17
+ var patchCreateProgram_1 = require("./patchCreateProgram");
18
+ function createTransformerFromPattern(_a) {
19
+ var typescript = _a.typescript, factory = _a.factory, config = _a.config, program = _a.program, ls = _a.ls;
20
+ var transform = config.transform, after = config.after, afterDeclarations = config.afterDeclarations, name = config.name, type = config.type, cleanConfig = __rest(config, ["transform", "after", "afterDeclarations", "name", "type"]);
21
+ if (!transform)
22
+ throw new Error('Not a valid config entry: "transform" key not found');
23
+ var ret;
24
+ switch (config.type) {
25
+ case 'ls':
26
+ if (!ls)
27
+ throw new Error("Plugin ".concat(transform, " need a LanguageService"));
28
+ ret = factory(ls, cleanConfig);
29
+ break;
30
+ case 'config':
31
+ ret = factory(cleanConfig);
32
+ break;
33
+ case 'compilerOptions':
34
+ ret = factory(program.getCompilerOptions(), cleanConfig);
35
+ break;
36
+ case 'checker':
37
+ ret = factory(program.getTypeChecker(), cleanConfig);
38
+ break;
39
+ case undefined:
40
+ case 'program':
41
+ ret = factory(program, cleanConfig, {
42
+ ts: typescript,
43
+ addDiagnostic: (0, patchCreateProgram_1.addDiagnosticFactory)(program),
44
+ });
45
+ break;
46
+ case 'raw':
47
+ ret = function (ctx) { return factory(ctx, program, cleanConfig); };
48
+ break;
49
+ default:
50
+ return never(config.type);
51
+ }
52
+ if (typeof ret === 'function') {
53
+ if (after)
54
+ return { after: ret };
55
+ else if (afterDeclarations) {
56
+ return { afterDeclarations: ret };
57
+ }
58
+ else
59
+ return { before: ret };
60
+ }
61
+ return ret;
62
+ }
63
+ function never(n) {
64
+ throw new Error('Unexpected type: ' + n);
65
+ }
66
+ var tsNodeIncluded = false;
67
+ // to fix recursion bug, see usage below
68
+ var requireStack = [];
69
+ /**
70
+ * @example
71
+ *
72
+ * new PluginCreator([
73
+ * {transform: '@zerollup/ts-transform-paths', someOption: '123'},
74
+ * {transform: '@zerollup/ts-transform-paths', type: 'ls', someOption: '123'},
75
+ * {transform: '@zerollup/ts-transform-paths', type: 'ls', after: true, someOption: '123'}
76
+ * ]).createTransformers({ program })
77
+ */
78
+ var PluginCreator = /** @class */ (function () {
79
+ function PluginCreator(typescript, configs, resolveBaseDir) {
80
+ if (resolveBaseDir === void 0) { resolveBaseDir = process.cwd(); }
81
+ this.typescript = typescript;
82
+ this.configs = configs;
83
+ this.resolveBaseDir = resolveBaseDir;
84
+ this.validateConfigs(configs);
85
+ }
86
+ PluginCreator.prototype.mergeTransformers = function (into, source) {
87
+ var _a, _b, _c;
88
+ var slice = function (input) { return (Array.isArray(input) ? input.slice() : [input]); };
89
+ if (source.before) {
90
+ (_a = into.before).push.apply(_a, slice(source.before));
91
+ }
92
+ if (source.after) {
93
+ (_b = into.after).push.apply(_b, slice(source.after));
94
+ }
95
+ if (source.afterDeclarations) {
96
+ (_c = into.afterDeclarations).push.apply(_c, slice(source.afterDeclarations));
97
+ }
98
+ return this;
99
+ };
100
+ PluginCreator.prototype.createTransformers = function (params, customTransformers) {
101
+ var chain = {
102
+ before: [],
103
+ after: [],
104
+ afterDeclarations: [],
105
+ };
106
+ var ls;
107
+ var program;
108
+ if ('ls' in params) {
109
+ ls = params.ls;
110
+ program = ls.getProgram();
111
+ }
112
+ else {
113
+ program = params.program;
114
+ }
115
+ for (var _i = 0, _a = this.configs; _i < _a.length; _i++) {
116
+ var config = _a[_i];
117
+ if (!config.transform) {
118
+ continue;
119
+ }
120
+ var factory = this.resolveFactory(config.transform, config.import);
121
+ // if recursion
122
+ if (factory === undefined)
123
+ continue;
124
+ var transformer = createTransformerFromPattern({
125
+ typescript: this.typescript,
126
+ factory: factory,
127
+ config: config,
128
+ program: program,
129
+ ls: ls,
130
+ });
131
+ this.mergeTransformers(chain, transformer);
132
+ }
133
+ // if we're given some custom transformers, they must be chained at the end
134
+ if (customTransformers) {
135
+ this.mergeTransformers(chain, customTransformers);
136
+ }
137
+ return chain;
138
+ };
139
+ PluginCreator.prototype.resolveFactory = function (transform, importKey) {
140
+ if (importKey === void 0) { importKey = 'default'; }
141
+ if (!tsNodeIncluded &&
142
+ transform.match(/\.tsx?$/) &&
143
+ (module.parent.parent === null ||
144
+ module.parent.parent.parent === null ||
145
+ module.parent.parent.parent.id.split(/[\/\\]/).indexOf('ts-node') === -1)) {
146
+ require('ts-node').register({
147
+ transpileOnly: true,
148
+ skipProject: true,
149
+ compilerOptions: {
150
+ target: 'ES2018',
151
+ jsx: 'react',
152
+ esModuleInterop: true,
153
+ module: 'commonjs',
154
+ },
155
+ });
156
+ tsNodeIncluded = true;
157
+ }
158
+ var modulePath = resolve.sync(transform, { basedir: this.resolveBaseDir });
159
+ // in ts-node occurs error cause recursion:
160
+ // ts-node file.ts -> createTransformers -> require transformer.ts
161
+ // -> createTransformers -> require transformer.ts -> ...
162
+ // this happens cause ts-node uses to compile transformers the same config included this transformer
163
+ // so this stack checks that if we already required this file we are in the reqursion
164
+ if (requireStack.indexOf(modulePath) > -1)
165
+ return;
166
+ requireStack.push(modulePath);
167
+ var commonjsModule = require(modulePath);
168
+ requireStack.pop();
169
+ var factoryModule = typeof commonjsModule === 'function' ? { default: commonjsModule } : commonjsModule;
170
+ var factory = factoryModule[importKey];
171
+ if (!factory) {
172
+ throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" does not have an export \"").concat(importKey, "\": ") +
173
+ (0, util_1.inspect)(factoryModule));
174
+ }
175
+ if (typeof factory !== 'function') {
176
+ throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" export \"").concat(importKey, "\" is not a plugin: \"").concat((0, util_1.inspect)(factory), "\""));
177
+ }
178
+ return factory;
179
+ };
180
+ PluginCreator.prototype.validateConfigs = function (configs) {
181
+ for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
182
+ var config = configs_1[_i];
183
+ if (!config.name && !config.transform) {
184
+ throw new Error('tsconfig.json plugins error: transform must be present');
185
+ }
186
+ }
187
+ };
188
+ return PluginCreator;
189
+ }());
190
+ exports.PluginCreator = PluginCreator;
@@ -1,2 +1,2 @@
1
- "use strict";
2
- module.exports = require('typescript/lib/cancellationToken');
1
+ "use strict";
2
+ module.exports = require('typescript/lib/cancellationToken');
@@ -1,7 +1,7 @@
1
- import * as TS from 'typescript';
2
- type ts = typeof TS;
3
- export declare function loadTypeScript(filename: string, { folder, forceConfigLoad }?: {
4
- folder?: string;
5
- forceConfigLoad?: boolean;
6
- }): ts;
7
- export {};
1
+ import * as TS from 'typescript';
2
+ type ts = typeof TS;
3
+ export declare function loadTypeScript(filename: string, { folder, forceConfigLoad }?: {
4
+ folder?: string;
5
+ forceConfigLoad?: boolean;
6
+ }): ts;
7
+ export {};