@tsslint/cli 1.2.4 → 1.3.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/index.js CHANGED
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const ts = require("typescript");
4
4
  const path = require("path");
5
5
  const core = require("@tsslint/core");
6
+ const cache = require("./lib/cache");
6
7
  const glob = require("glob");
8
+ const fs = require("fs");
7
9
  (async () => {
8
10
  let hasError = false;
9
11
  let projectVersion = 0;
@@ -72,11 +74,13 @@ const glob = require("glob");
72
74
  async function projectWorker(tsconfigOption) {
73
75
  const tsconfig = await getTsconfigPath(tsconfigOption);
74
76
  const configFile = ts.findConfigFile(path.dirname(tsconfig), ts.sys.fileExists, 'tsslint.config.ts');
75
- log.step(`Project: ${path.relative(process.cwd(), tsconfig)} (${parseCommonLine(tsconfig).fileNames.length} files)`);
76
77
  if (!configFile) {
78
+ log.step(`Project: ${path.relative(process.cwd(), tsconfig)}`);
77
79
  log.error('No tsslint.config.ts file found!');
78
80
  return;
79
81
  }
82
+ parsed = parseCommonLine(tsconfig);
83
+ log.step(`Project: ${path.relative(process.cwd(), tsconfig)} (${parsed.fileNames.length} files)`);
80
84
  if (!configs.has(configFile)) {
81
85
  try {
82
86
  configs.set(configFile, await core.buildConfigFile(configFile, ts.sys.createHash, {
@@ -94,21 +98,42 @@ const glob = require("glob");
94
98
  if (!tsslintConfig) {
95
99
  return;
96
100
  }
97
- parsed = parseCommonLine(tsconfig);
98
101
  if (!parsed.fileNames) {
99
102
  throw new Error('No input files found in tsconfig!');
100
103
  }
101
104
  projectVersion++;
102
105
  typeRootsVersion++;
103
- const linter = core.createLinter({
106
+ const lintCache = process.argv.includes('--force')
107
+ ? {}
108
+ : cache.loadCache(configFile, ts.sys.createHash);
109
+ const projectContext = {
104
110
  configFile,
105
111
  languageService,
106
112
  languageServiceHost,
107
113
  typescript: ts,
108
114
  tsconfig: ts.server.toNormalizedPath(tsconfig),
109
- }, tsslintConfig, false);
115
+ };
116
+ const linter = core.createLinter(projectContext, tsslintConfig, 'cli');
110
117
  let hasFix = false;
118
+ let cached = 0;
111
119
  for (const fileName of parsed.fileNames) {
120
+ const fileMtime = fs.statSync(fileName).mtimeMs;
121
+ let fileCache = lintCache[fileName];
122
+ if (fileCache) {
123
+ if (fileCache[0] !== fileMtime) {
124
+ fileCache[0] = fileMtime;
125
+ fileCache[1] = {};
126
+ fileCache[2].length = 0;
127
+ fileCache[3].length = 0;
128
+ fileCache[4] = {};
129
+ }
130
+ else {
131
+ cached++;
132
+ }
133
+ }
134
+ else {
135
+ lintCache[fileName] = fileCache = [fileMtime, {}, [], [], {}];
136
+ }
112
137
  if (process.argv.includes('--fix')) {
113
138
  let retry = 3;
114
139
  let shouldRetry = true;
@@ -116,8 +141,14 @@ const glob = require("glob");
116
141
  while (shouldRetry && retry) {
117
142
  shouldRetry = false;
118
143
  retry--;
119
- const diagnostics = linter.lint(fileName);
120
- const fixes = linter.getCodeFixes(fileName, 0, Number.MAX_VALUE, diagnostics);
144
+ if (Object.values(fileCache[1]).some(fixes => fixes > 0)) {
145
+ // Reset the cache if there are any fixes applied.
146
+ fileCache[1] = {};
147
+ fileCache[2].length = 0;
148
+ fileCache[3].length = 0;
149
+ }
150
+ const diagnostics = linter.lint(fileName, fileCache);
151
+ const fixes = linter.getCodeFixes(fileName, 0, Number.MAX_VALUE, diagnostics, fileCache);
121
152
  const textChanges = core.combineCodeFixes(fileName, fixes);
122
153
  if (textChanges.length) {
123
154
  const oldSnapshot = snapshots.get(fileName);
@@ -130,10 +161,11 @@ const glob = require("glob");
130
161
  }
131
162
  if (newSnapshot) {
132
163
  ts.sys.writeFile(fileName, newSnapshot.getText(0, newSnapshot.getLength()));
164
+ fileCache[0] = fs.statSync(fileName).mtimeMs;
133
165
  }
134
166
  }
135
167
  else {
136
- const diagnostics = linter.lint(fileName);
168
+ const diagnostics = linter.lint(fileName, fileCache);
137
169
  for (const diagnostic of diagnostics) {
138
170
  if (diagnostic.category === ts.DiagnosticCategory.Suggestion) {
139
171
  continue;
@@ -160,6 +192,10 @@ const glob = require("glob");
160
192
  }
161
193
  }
162
194
  }
195
+ cache.saveCache(configFile, lintCache, ts.sys.createHash);
196
+ if (cached) {
197
+ log.info(`Linted ${parsed.fileNames.length - cached} files. (Cached ${cached} files result, use --force to re-lint all files.)`);
198
+ }
163
199
  if (hasFix) {
164
200
  log.info(`Use --fix to apply fixes.`);
165
201
  }
package/lib/cache.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import core = require('@tsslint/core');
2
+ export type CacheData = Record<string, core.FileLintCache>;
3
+ export declare function loadCache(configFilePath: string, createHash?: (path: string) => string): CacheData;
4
+ export declare function saveCache(configFilePath: string, cache: CacheData, createHash?: (path: string) => string): void;
package/lib/cache.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadCache = loadCache;
4
+ exports.saveCache = saveCache;
5
+ const core = require("@tsslint/core");
6
+ const path = require("path");
7
+ const fs = require("fs");
8
+ function loadCache(configFilePath, createHash = btoa) {
9
+ const outDir = core.getDotTsslintPath(configFilePath);
10
+ const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '.cache.json';
11
+ const cacheFilePath = path.join(outDir, cacheFileName);
12
+ const cacheFileStat = fs.statSync(cacheFilePath, { throwIfNoEntry: false });
13
+ const configFileStat = fs.statSync(configFilePath, { throwIfNoEntry: false });
14
+ if (cacheFileStat?.isFile() && cacheFileStat.mtimeMs > (configFileStat?.mtimeMs ?? 0)) {
15
+ try {
16
+ return require(cacheFilePath);
17
+ }
18
+ catch {
19
+ return {};
20
+ }
21
+ }
22
+ return {};
23
+ }
24
+ function saveCache(configFilePath, cache, createHash = btoa) {
25
+ const outDir = core.getDotTsslintPath(configFilePath);
26
+ const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '.cache.json';
27
+ const cacheFilePath = path.join(outDir, cacheFileName);
28
+ fs.writeFileSync(cacheFilePath, JSON.stringify(cache));
29
+ }
30
+ //# sourceMappingURL=cache.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/cli",
3
- "version": "1.2.4",
3
+ "version": "1.3.1",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "tsslint": "./bin/tsslint.js"
@@ -16,12 +16,12 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@clack/prompts": "^0.7.0",
19
- "@tsslint/config": "1.2.4",
20
- "@tsslint/core": "1.2.4",
19
+ "@tsslint/config": "1.3.1",
20
+ "@tsslint/core": "1.3.1",
21
21
  "glob": "^10.4.1"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "typescript": "*"
25
25
  },
26
- "gitHead": "818bc257e90e431ca8988477862238c70a4757ff"
26
+ "gitHead": "ddc73232699ecd7b4e4bfe5c9b511254c5926986"
27
27
  }