@tsslint/core 1.4.5 → 1.5.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.
Files changed (3) hide show
  1. package/index.d.ts +8 -3
  2. package/index.js +57 -4
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './lib/build';
2
2
  export * from './lib/watch';
3
- import type { Config, ProjectContext, Rule, Rules } from '@tsslint/types';
3
+ import type { Config, ProjectContext, Rule, Rules, FormattingProcess } from '@tsslint/types';
4
4
  import type * as ts from 'typescript';
5
5
  export type FileLintCache = [
6
6
  mtime: number,
@@ -8,10 +8,14 @@ export type FileLintCache = [
8
8
  hasFix: boolean,
9
9
  diagnostics: ts.DiagnosticWithLocation[]
10
10
  ]>,
11
- minimatchResult: Record<string, boolean>
11
+ minimatchResult: Record<string, boolean>,
12
+ formated: boolean
12
13
  ];
13
14
  export type Linter = ReturnType<typeof createLinter>;
14
- export declare function createLinter(ctx: ProjectContext, config: Config | Config[], mode: 'cli' | 'typescript-plugin', syntaxOnlyLanguageService?: ts.LanguageService): {
15
+ export declare function createLinter(ctx: ProjectContext, rootDir: string, config: Config | Config[], mode: 'cli' | 'typescript-plugin', syntaxOnlyLanguageService?: ts.LanguageService & {
16
+ getNonBoundSourceFile?(fileName: string): ts.SourceFile;
17
+ }): {
18
+ format(sourceFile: ts.SourceFile, minimatchCache?: FileLintCache[2]): ts.TextChange[];
15
19
  lint(fileName: string, cache?: FileLintCache): ts.DiagnosticWithLocation[];
16
20
  hasCodeFixes(fileName: string): boolean;
17
21
  getCodeFixes(fileName: string, start: number, end: number, diagnostics?: ts.Diagnostic[], minimatchCache?: FileLintCache[2]): ts.CodeFixAction[];
@@ -22,6 +26,7 @@ export declare function createLinter(ctx: ProjectContext, config: Config | Confi
22
26
  include: string[];
23
27
  exclude: string[];
24
28
  rules: Rules;
29
+ formatting: FormattingProcess[] | undefined;
25
30
  plugins: import("@tsslint/types").PluginInstance[];
26
31
  }[];
27
32
  };
package/index.js CHANGED
@@ -23,24 +23,64 @@ __exportStar(require("./lib/watch"), exports);
23
23
  const ErrorStackParser = require("error-stack-parser");
24
24
  const path = require("path");
25
25
  const minimatch = require("minimatch");
26
- function createLinter(ctx, config, mode, syntaxOnlyLanguageService) {
26
+ function createLinter(ctx, rootDir, config, mode, syntaxOnlyLanguageService) {
27
27
  const ts = ctx.typescript;
28
28
  const fileRules = new Map();
29
+ const fileFmtProcesses = new Map();
29
30
  const fileConfigs = new Map();
30
31
  const lintResults = new Map();
31
- const basePath = path.dirname(ctx.configFile);
32
32
  const configs = (Array.isArray(config) ? config : [config])
33
33
  .map(config => ({
34
34
  include: config.include ?? [],
35
35
  exclude: config.exclude ?? [],
36
36
  rules: config.rules ?? {},
37
+ formatting: config.formatting,
37
38
  plugins: (config.plugins ?? []).map(plugin => plugin(ctx)),
38
39
  }));
39
40
  const normalizedPath = new Map();
40
41
  const rule2Mode = new Map();
41
- const getNonBoundSourceFile = syntaxOnlyLanguageService.getNonBoundSourceFile;
42
+ const getNonBoundSourceFile = syntaxOnlyLanguageService?.getNonBoundSourceFile;
42
43
  let shouldEnableTypeAware = false;
43
44
  return {
45
+ format(sourceFile, minimatchCache) {
46
+ const preprocess = getFileFmtProcesses(sourceFile.fileName, minimatchCache);
47
+ const changes = [];
48
+ const tmpChanges = [];
49
+ const fmtCtx = {
50
+ typescript: ts,
51
+ sourceFile,
52
+ insert(pos, text) {
53
+ tmpChanges.push({ span: { start: pos, length: 0 }, newText: text });
54
+ },
55
+ remove(start, end) {
56
+ tmpChanges.push({ span: { start, length: end - start }, newText: '' });
57
+ },
58
+ replace(start, end, text) {
59
+ tmpChanges.push({ span: { start, length: end - start }, newText: text });
60
+ },
61
+ };
62
+ for (const process of preprocess) {
63
+ process(fmtCtx);
64
+ if (tmpChanges.every(a => {
65
+ const aStart = a.span.start;
66
+ const aEnd = aStart + a.span.length;
67
+ for (const b of changes) {
68
+ const bStart = b.span.start;
69
+ const bEnd = bStart + b.span.length;
70
+ if ((bStart >= aEnd && bStart > aStart)
71
+ || (bEnd <= aStart && bEnd < aEnd)) {
72
+ continue;
73
+ }
74
+ return false;
75
+ }
76
+ return true;
77
+ })) {
78
+ changes.push(...tmpChanges);
79
+ }
80
+ tmpChanges.length = 0;
81
+ }
82
+ return changes;
83
+ },
44
84
  lint(fileName, cache) {
45
85
  let currentRuleId;
46
86
  let shouldRetry = false;
@@ -315,6 +355,19 @@ function createLinter(ctx, config, mode, syntaxOnlyLanguageService) {
315
355
  getRules: getFileRules,
316
356
  getConfigs: getFileConfigs,
317
357
  };
358
+ function getFileFmtProcesses(fileName, minimatchCache) {
359
+ if (!fileFmtProcesses.has(fileName)) {
360
+ const allPreprocess = [];
361
+ const configs = getFileConfigs(fileName, minimatchCache);
362
+ for (const { formatting } of configs) {
363
+ if (formatting) {
364
+ allPreprocess.push(...formatting);
365
+ }
366
+ }
367
+ fileFmtProcesses.set(fileName, allPreprocess);
368
+ }
369
+ return fileFmtProcesses.get(fileName);
370
+ }
318
371
  function getFileRules(fileName, minimatchCache) {
319
372
  let rules = fileRules.get(fileName);
320
373
  if (!rules) {
@@ -364,7 +417,7 @@ function createLinter(ctx, config, mode, syntaxOnlyLanguageService) {
364
417
  }
365
418
  let normalized = normalizedPath.get(pattern);
366
419
  if (!normalized) {
367
- normalized = ts.server.toNormalizedPath(path.resolve(basePath, pattern));
420
+ normalized = ts.server.toNormalizedPath(path.resolve(rootDir, pattern));
368
421
  normalizedPath.set(pattern, normalized);
369
422
  }
370
423
  const res = minimatch.minimatch(fileName, normalized);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/core",
3
- "version": "1.4.5",
3
+ "version": "1.5.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/core"
13
13
  },
14
14
  "dependencies": {
15
- "@tsslint/types": "1.4.5",
15
+ "@tsslint/types": "1.5.0",
16
16
  "error-stack-parser": "^2.1.4",
17
17
  "esbuild": ">=0.17.0",
18
18
  "minimatch": "^10.0.1"
@@ -23,5 +23,5 @@
23
23
  "scripts": {
24
24
  "postinstall": "node scripts/cleanCache.js"
25
25
  },
26
- "gitHead": "e8f0b027ff4e15d2633efd15c08bf2054896cf1b"
26
+ "gitHead": "6585740b4ecbc8251163923b4e8c7978fe9cb12c"
27
27
  }