@tsslint/typescript-plugin 0.0.0 → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Johnson Chu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js CHANGED
@@ -25,48 +25,149 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  const watchConfig_1 = require("./lib/watchConfig");
26
26
  const builtInPlugins_1 = require("./lib/builtInPlugins");
27
27
  const path = __importStar(require("path"));
28
+ const languageServiceDecorators = new WeakMap();
28
29
  const init = (modules) => {
29
30
  const { typescript: ts } = modules;
30
31
  const pluginModule = {
31
32
  create(info) {
32
- const tsconfig = info.project.projectKind === ts.server.ProjectKind.Configured
33
- ? info.project.getProjectName()
34
- : undefined;
35
- if (!tsconfig) {
36
- return info.languageService;
33
+ if (!languageServiceDecorators.has(info.languageService)) {
34
+ const tsconfig = info.project.projectKind === ts.server.ProjectKind.Configured
35
+ ? info.project.getProjectName()
36
+ : undefined;
37
+ if (tsconfig) {
38
+ languageServiceDecorators.set(info.languageService, decorateLanguageService(ts, tsconfig, info));
39
+ }
40
+ }
41
+ languageServiceDecorators.get(info.languageService)?.update(info.config);
42
+ return info.languageService;
43
+ },
44
+ };
45
+ return pluginModule;
46
+ };
47
+ function decorateLanguageService(ts, tsconfig, info) {
48
+ const getCompilerOptionsDiagnostics = info.languageService.getCompilerOptionsDiagnostics;
49
+ const getSyntacticDiagnostics = info.languageService.getSyntacticDiagnostics;
50
+ const getApplicableRefactors = info.languageService.getApplicableRefactors;
51
+ const getEditsForRefactor = info.languageService.getEditsForRefactor;
52
+ let compilerOptionsDiagnostics = [];
53
+ let configFile;
54
+ let configFileBuildContext;
55
+ let config;
56
+ let plugins = [];
57
+ info.languageService.getCompilerOptionsDiagnostics = () => {
58
+ return getCompilerOptionsDiagnostics().concat(compilerOptionsDiagnostics);
59
+ };
60
+ info.languageService.getSyntacticDiagnostics = fileName => {
61
+ let errors = getSyntacticDiagnostics(fileName);
62
+ const sourceFile = info.languageService.getProgram()?.getSourceFile(fileName);
63
+ if (!sourceFile) {
64
+ return errors;
65
+ }
66
+ const token = info.languageServiceHost.getCancellationToken?.();
67
+ for (const plugin of plugins) {
68
+ if (token?.isCancellationRequested()) {
69
+ break;
70
+ }
71
+ if (plugin.lint) {
72
+ let pluginResult = plugin.lint?.(sourceFile, config?.rules ?? {});
73
+ for (const plugin of plugins) {
74
+ if (plugin.resolveResult) {
75
+ pluginResult = plugin.resolveResult(pluginResult);
76
+ }
77
+ }
78
+ errors = errors.concat(pluginResult);
37
79
  }
38
- if (!info.config.configFile) {
39
- return info.languageService;
80
+ }
81
+ return errors;
82
+ };
83
+ info.languageService.getApplicableRefactors = (fileName, positionOrRange, ...rest) => {
84
+ let refactors = getApplicableRefactors(fileName, positionOrRange, ...rest);
85
+ const sourceFile = info.languageService.getProgram()?.getSourceFile(fileName);
86
+ if (!sourceFile) {
87
+ return refactors;
88
+ }
89
+ const token = info.languageServiceHost.getCancellationToken?.();
90
+ for (const plugin of plugins) {
91
+ if (token?.isCancellationRequested()) {
92
+ break;
40
93
  }
41
- let configFile;
42
- try {
43
- configFile = path.resolve(tsconfig, '..', info.config.configFile);
94
+ refactors = refactors.concat(plugin.getFixes?.(sourceFile, positionOrRange) ?? []);
95
+ }
96
+ return refactors;
97
+ };
98
+ info.languageService.getEditsForRefactor = (fileName, formatOptions, positionOrRange, refactorName, actionName, ...rest) => {
99
+ const sourceFile = info.languageService.getProgram()?.getSourceFile(fileName);
100
+ if (!sourceFile) {
101
+ return;
102
+ }
103
+ for (const plugin of plugins) {
104
+ const edits = plugin.fix?.(sourceFile, refactorName, actionName);
105
+ if (edits) {
106
+ return { edits };
44
107
  }
45
- catch (err) {
46
- // TODO: show error in tsconfig.json
108
+ }
109
+ return getEditsForRefactor(fileName, formatOptions, positionOrRange, refactorName, actionName, ...rest);
110
+ };
111
+ return { update };
112
+ async function update(pluginConfig) {
113
+ const jsonConfigFile = ts.readJsonConfigFile(tsconfig, ts.sys.readFile);
114
+ const start = jsonConfigFile.text.indexOf(pluginConfig.configFile) - 1;
115
+ const length = pluginConfig.configFile.length + 2;
116
+ let newConfigFile;
117
+ let configResolveError;
118
+ try {
119
+ newConfigFile = require.resolve(pluginConfig.configFile, { paths: [path.dirname(tsconfig)] });
120
+ }
121
+ catch (err) {
122
+ configResolveError = err;
123
+ }
124
+ if (newConfigFile !== configFile) {
125
+ configFile = newConfigFile;
126
+ config = undefined;
127
+ plugins = [];
128
+ configFileBuildContext?.dispose();
129
+ compilerOptionsDiagnostics = [];
130
+ if (configResolveError) {
131
+ compilerOptionsDiagnostics.push({
132
+ category: ts.DiagnosticCategory.Error,
133
+ code: 0,
134
+ messageText: String(configResolveError),
135
+ file: jsonConfigFile,
136
+ start: start,
137
+ length: length,
138
+ });
47
139
  }
48
140
  if (!configFile) {
49
- return info.languageService;
141
+ return;
50
142
  }
51
- let config;
52
- let plugins = [];
53
- const languageServiceHost = info.languageServiceHost;
54
- const languageService = info.languageService;
55
143
  const projectContext = {
56
144
  configFile,
57
145
  tsconfig,
58
- languageServiceHost,
59
- languageService,
146
+ languageServiceHost: info.languageServiceHost,
147
+ languageService: info.languageService,
60
148
  typescript: ts,
61
149
  };
62
- decorateLanguageService();
63
- (0, watchConfig_1.watchConfig)(configFile, async (_config, result) => {
150
+ configFileBuildContext = await (0, watchConfig_1.watchConfig)(configFile, async (_config, { errors, warnings }) => {
64
151
  config = _config;
152
+ compilerOptionsDiagnostics = [
153
+ ...errors.map(error => [error, ts.DiagnosticCategory.Error]),
154
+ ...warnings.map(error => [error, ts.DiagnosticCategory.Warning]),
155
+ ].map(([error, category]) => {
156
+ const diag = {
157
+ category,
158
+ code: 0,
159
+ messageText: error.text,
160
+ file: jsonConfigFile,
161
+ start: start,
162
+ length: length,
163
+ };
164
+ return diag;
165
+ });
65
166
  if (config) {
66
167
  plugins = await Promise.all([
67
168
  ...builtInPlugins_1.builtInPlugins,
68
169
  ...config.plugins ?? []
69
- ].map(plugin => plugin(projectContext, result)));
170
+ ].map(plugin => plugin(projectContext)));
70
171
  for (const plugin of plugins) {
71
172
  if (plugin.resolveRules) {
72
173
  config.rules = plugin.resolveRules(config.rules ?? {});
@@ -75,66 +176,8 @@ const init = (modules) => {
75
176
  }
76
177
  info.project.refreshDiagnostics();
77
178
  });
78
- return languageService;
79
- function decorateLanguageService() {
80
- const getSyntacticDiagnostics = languageService.getSyntacticDiagnostics;
81
- const getApplicableRefactors = languageService.getApplicableRefactors;
82
- const getEditsForRefactor = languageService.getEditsForRefactor;
83
- languageService.getSyntacticDiagnostics = fileName => {
84
- let errors = getSyntacticDiagnostics(fileName);
85
- const sourceFile = languageService.getProgram()?.getSourceFile(fileName);
86
- if (!sourceFile) {
87
- return errors;
88
- }
89
- const token = languageServiceHost.getCancellationToken?.();
90
- for (const plugin of plugins) {
91
- if (token?.isCancellationRequested()) {
92
- break;
93
- }
94
- if (plugin.lint) {
95
- let pluginResult = plugin.lint?.(sourceFile, config?.rules ?? {});
96
- for (const plugin of plugins) {
97
- if (plugin.resolveResult) {
98
- pluginResult = plugin.resolveResult(pluginResult);
99
- }
100
- }
101
- errors = errors.concat(pluginResult);
102
- }
103
- }
104
- return errors;
105
- };
106
- languageService.getApplicableRefactors = (fileName, positionOrRange, ...rest) => {
107
- let refactors = getApplicableRefactors(fileName, positionOrRange, ...rest);
108
- const sourceFile = languageService.getProgram()?.getSourceFile(fileName);
109
- if (!sourceFile) {
110
- return refactors;
111
- }
112
- const token = languageServiceHost.getCancellationToken?.();
113
- for (const plugin of plugins) {
114
- if (token?.isCancellationRequested()) {
115
- break;
116
- }
117
- refactors = refactors.concat(plugin.getFixes?.(sourceFile, positionOrRange) ?? []);
118
- }
119
- return refactors;
120
- };
121
- languageService.getEditsForRefactor = (fileName, formatOptions, positionOrRange, refactorName, actionName, ...rest) => {
122
- const sourceFile = languageService.getProgram()?.getSourceFile(fileName);
123
- if (!sourceFile) {
124
- return;
125
- }
126
- for (const plugin of plugins) {
127
- const edits = plugin.fix?.(sourceFile, refactorName, actionName);
128
- if (edits) {
129
- return { edits };
130
- }
131
- }
132
- return getEditsForRefactor(fileName, formatOptions, positionOrRange, refactorName, actionName, ...rest);
133
- };
134
- }
135
- },
136
- };
137
- return pluginModule;
138
- };
179
+ }
180
+ }
181
+ }
139
182
  module.exports = init;
140
183
  //# sourceMappingURL=index.js.map
@@ -25,33 +25,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.builtInPlugins = void 0;
27
27
  const ErrorStackParser = __importStar(require("error-stack-parser"));
28
+ require('source-map-support').install();
28
29
  exports.builtInPlugins = [
29
- (ctx, { warnings, errors }) => {
30
- const ts = ctx.typescript;
31
- return {
32
- lint(sourceFile) {
33
- if (sourceFile.fileName !== ctx.configFile) {
34
- return [];
35
- }
36
- return [
37
- ...errors.map(error => [error, ts.DiagnosticCategory.Error]),
38
- ...warnings.map(error => [error, ts.DiagnosticCategory.Warning]),
39
- ].map(([error, category]) => {
40
- const diag = {
41
- category,
42
- source: 'tsslint-esbuild',
43
- code: error.id,
44
- messageText: JSON.stringify(error, null, 2),
45
- file: sourceFile,
46
- start: 0,
47
- length: 0,
48
- };
49
- // TODO: parse error.notes for relatedInformation
50
- return diag;
51
- });
52
- },
53
- };
54
- },
55
30
  ctx => {
56
31
  const ts = ctx.typescript;
57
32
  const fileFixes = new Map();
@@ -78,16 +53,16 @@ exports.builtInPlugins = [
78
53
  rule(rulesContext);
79
54
  }
80
55
  return result;
81
- function reportError(message, start, end) {
82
- return report(ts.DiagnosticCategory.Error, message, start, end);
56
+ function reportError(message, start, end, trace = true) {
57
+ return report(ts.DiagnosticCategory.Error, message, start, end, trace);
83
58
  }
84
- function reportWarning(message, start, end) {
85
- return report(ts.DiagnosticCategory.Warning, message, start, end);
59
+ function reportWarning(message, start, end, trace = true) {
60
+ return report(ts.DiagnosticCategory.Warning, message, start, end, trace);
86
61
  }
87
- function reportSuggestion(message, start, end) {
88
- return report(ts.DiagnosticCategory.Suggestion, message, start, end);
62
+ function reportSuggestion(message, start, end, trace = true) {
63
+ return report(ts.DiagnosticCategory.Suggestion, message, start, end, trace);
89
64
  }
90
- function report(category, message, start, end) {
65
+ function report(category, message, start, end, trace) {
91
66
  const error = {
92
67
  category,
93
68
  code: currentRuleId,
@@ -98,7 +73,7 @@ exports.builtInPlugins = [
98
73
  source: 'tsslint',
99
74
  relatedInformation: [],
100
75
  };
101
- const stacks = ErrorStackParser.parse(new Error());
76
+ const stacks = trace ? ErrorStackParser.parse(new Error()) : [];
102
77
  if (stacks.length >= 3) {
103
78
  const stack = stacks[2];
104
79
  if (stack.fileName && stack.lineNumber !== undefined && stack.columnNumber !== undefined) {
@@ -107,7 +82,8 @@ exports.builtInPlugins = [
107
82
  fileName = fileName.substring('file://'.length);
108
83
  }
109
84
  if (!sourceFiles.has(fileName)) {
110
- sourceFiles.set(fileName, ts.createSourceFile(fileName, ctx.languageServiceHost.readFile(fileName), ts.ScriptTarget.Latest, true));
85
+ const text = ctx.languageServiceHost.readFile(fileName) ?? '';
86
+ sourceFiles.set(fileName, ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true));
111
87
  }
112
88
  const stackFile = sourceFiles.get(fileName);
113
89
  const pos = stackFile?.getPositionOfLineAndCharacter(stack.lineNumber - 1, stack.columnNumber - 1);
@@ -3,6 +3,7 @@ import esbuild = require('esbuild');
3
3
  export declare function watchConfig(tsConfigPath: string, onBuild: (config: Config | undefined, result: esbuild.BuildResult) => void): Promise<esbuild.BuildContext<{
4
4
  entryPoints: string[];
5
5
  bundle: true;
6
+ sourcemap: true;
6
7
  outfile: string;
7
8
  format: "cjs";
8
9
  platform: "node";
@@ -4,16 +4,13 @@ exports.watchConfig = void 0;
4
4
  const esbuild = require("esbuild");
5
5
  const path = require("path");
6
6
  async function watchConfig(tsConfigPath, onBuild) {
7
- const outDir = path.resolve(path.dirname(require.resolve('@tsslint/typescript-plugin/package.json')), '..', '.tsslint');
8
- const outFileName = path
9
- .relative(outDir, tsConfigPath)
10
- .replace(/\.\./g, '__up')
11
- .replace(/\//g, '__slash')
12
- + '.cjs';
7
+ const outDir = path.resolve(path.dirname(require.resolve('@tsslint/typescript-plugin/package.json')), '..', '..', '.tsslint');
8
+ const outFileName = btoa(path.relative(outDir, tsConfigPath)) + '.cjs';
13
9
  const outFile = path.join(outDir, outFileName);
14
10
  const ctx = await esbuild.context({
15
11
  entryPoints: [tsConfigPath],
16
12
  bundle: true,
13
+ sourcemap: true,
17
14
  outfile: outFile,
18
15
  format: 'cjs',
19
16
  platform: 'node',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/typescript-plugin",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,8 +12,10 @@
12
12
  "directory": "packages/typescript-plugin"
13
13
  },
14
14
  "dependencies": {
15
- "@tsslint/config": "0.0.0",
15
+ "@tsslint/config": "0.0.1",
16
16
  "error-stack-parser": "^2.1.4",
17
- "esbuild": "^0.19.9"
18
- }
17
+ "esbuild": "^0.19.9",
18
+ "source-map-support": "^0.5.19"
19
+ },
20
+ "gitHead": "8a11cfe2ad196e8df0566d798c71bb40e3a2a6a0"
19
21
  }