@atlaspack/validator-typescript 2.14.5-canary.5 → 2.14.5-canary.500
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/CHANGELOG.md +601 -0
- package/dist/TypeScriptValidator.js +138 -0
- package/lib/TypeScriptValidator.js +7 -4
- package/lib/types/TypeScriptValidator.d.ts +3 -0
- package/package.json +14 -10
- package/src/{TypeScriptValidator.js → TypeScriptValidator.ts} +24 -26
- package/tsconfig.json +24 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const diagnostic_1 = require("@atlaspack/diagnostic");
|
|
9
|
+
const utils_1 = require("@atlaspack/utils");
|
|
10
|
+
const plugin_1 = require("@atlaspack/plugin");
|
|
11
|
+
const ts_utils_1 = require("@atlaspack/ts-utils");
|
|
12
|
+
let langServiceCache = {};
|
|
13
|
+
exports.default = new plugin_1.Validator({
|
|
14
|
+
async validateAll({ assets, options, resolveConfigWithPath, }) {
|
|
15
|
+
// Build a collection that of all the LanguageServices related to files that just changed.
|
|
16
|
+
let servicesToValidate = new Set();
|
|
17
|
+
await Promise.all(assets.map(async (asset) => {
|
|
18
|
+
let config = await getConfig(asset, options, resolveConfigWithPath);
|
|
19
|
+
let { configHash } = config;
|
|
20
|
+
// Create a languageService/host in the cache for the configuration if it doesn't already exist.
|
|
21
|
+
tryCreateLanguageService(config, asset, options);
|
|
22
|
+
if (!langServiceCache[configHash])
|
|
23
|
+
return;
|
|
24
|
+
// Invalidate the file with the LanguageServiceHost so Typescript knows it has changed.
|
|
25
|
+
langServiceCache[configHash].host.invalidate(asset.filePath);
|
|
26
|
+
servicesToValidate.add(configHash);
|
|
27
|
+
}));
|
|
28
|
+
// Ask typescript to analyze all changed programs and translate the results into ValidatorResult objects.
|
|
29
|
+
let validatorResults = [];
|
|
30
|
+
servicesToValidate.forEach((configHash) => {
|
|
31
|
+
// Make sure that the filesystem being used by the LanguageServiceHost and ParseConfigHost is up-to-date.
|
|
32
|
+
// (This could change in the context of re-running tests, and probably also for other reasons).
|
|
33
|
+
langServiceCache[configHash].host.fs = options.inputFS;
|
|
34
|
+
langServiceCache[configHash].configHost.fs = options.inputFS;
|
|
35
|
+
let program = langServiceCache[configHash].service.getProgram();
|
|
36
|
+
if (!program)
|
|
37
|
+
return;
|
|
38
|
+
let filesToCheck = program.getSourceFiles();
|
|
39
|
+
filesToCheck.forEach((sourceFile) => {
|
|
40
|
+
let diagnostics = program.getSemanticDiagnostics(sourceFile);
|
|
41
|
+
validatorResults.push(getValidateResultFromDiagnostics(sourceFile.fileName, diagnostics));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
return validatorResults;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
async function getConfig(asset, options,
|
|
48
|
+
// @ts-expect-error TS2304
|
|
49
|
+
resolveConfigWithPath) {
|
|
50
|
+
let configNames = ['tsconfig.json'];
|
|
51
|
+
let tsconfig = await (0, utils_1.loadConfig)(asset.fs, asset.filePath, configNames, options.projectRoot);
|
|
52
|
+
let configPath = await resolveConfigWithPath(configNames, asset.filePath);
|
|
53
|
+
let baseDir = configPath ? path_1.default.dirname(configPath) : options.projectRoot;
|
|
54
|
+
let configHash = (tsconfig ? (0, utils_1.hashObject)(tsconfig.config) : '') + '-' + baseDir;
|
|
55
|
+
return {
|
|
56
|
+
filepath: configPath,
|
|
57
|
+
baseDir,
|
|
58
|
+
configHash,
|
|
59
|
+
tsconfig: tsconfig?.config,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Tries to create a typescript language service instance in the cache if it doesn't already exist. */
|
|
63
|
+
function tryCreateLanguageService(config, asset, options) {
|
|
64
|
+
if (config.tsconfig && !langServiceCache[config.configHash]) {
|
|
65
|
+
// In order to prevent race conditions where we accidentally create two language services for the same config,
|
|
66
|
+
// we need to re-check the cache to see if a service has been created while we were awaiting 'ts'.
|
|
67
|
+
if (!langServiceCache[config.configHash]) {
|
|
68
|
+
let configHost = new ts_utils_1.ParseConfigHost(options.inputFS, typescript_1.default);
|
|
69
|
+
let parsedCommandLine = typescript_1.default.parseJsonConfigFileContent(config.tsconfig, configHost, config.baseDir);
|
|
70
|
+
const host = new ts_utils_1.LanguageServiceHost(options.inputFS, typescript_1.default, parsedCommandLine);
|
|
71
|
+
langServiceCache[config.configHash] = {
|
|
72
|
+
configHost,
|
|
73
|
+
host,
|
|
74
|
+
service: typescript_1.default.createLanguageService(host, typescript_1.default.createDocumentRegistry()),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Translates semantic diagnostics (from TypeScript) into a ValidateResult that Parcel understands. */
|
|
80
|
+
function getValidateResultFromDiagnostics(filePath, diagnostics) {
|
|
81
|
+
let validatorResult = {
|
|
82
|
+
warnings: [],
|
|
83
|
+
errors: [],
|
|
84
|
+
};
|
|
85
|
+
if (diagnostics.length > 0) {
|
|
86
|
+
for (let diagnostic of diagnostics) {
|
|
87
|
+
let filename = filePath;
|
|
88
|
+
let { file } = diagnostic;
|
|
89
|
+
let diagnosticMessage = (0, diagnostic_1.escapeMarkdown)(typeof diagnostic.messageText === 'string'
|
|
90
|
+
? diagnostic.messageText
|
|
91
|
+
: diagnostic.messageText.messageText);
|
|
92
|
+
let codeframe;
|
|
93
|
+
if (file != null && diagnostic.start != null) {
|
|
94
|
+
let source = file.text || diagnostic.source;
|
|
95
|
+
if (file.fileName) {
|
|
96
|
+
filename = file.fileName;
|
|
97
|
+
}
|
|
98
|
+
if (source) {
|
|
99
|
+
let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
100
|
+
let start = {
|
|
101
|
+
line: lineChar.line + 1,
|
|
102
|
+
column: lineChar.character + 1,
|
|
103
|
+
};
|
|
104
|
+
let end = {
|
|
105
|
+
line: start.line,
|
|
106
|
+
column: start.column + 1,
|
|
107
|
+
};
|
|
108
|
+
if (typeof diagnostic.start === 'number' &&
|
|
109
|
+
typeof diagnostic.length === 'number') {
|
|
110
|
+
let endCharPosition = file.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length);
|
|
111
|
+
end = {
|
|
112
|
+
line: endCharPosition.line + 1,
|
|
113
|
+
column: endCharPosition.character + 1,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
codeframe = {
|
|
117
|
+
filePath: filename,
|
|
118
|
+
code: source,
|
|
119
|
+
codeHighlights: [
|
|
120
|
+
{
|
|
121
|
+
start,
|
|
122
|
+
end,
|
|
123
|
+
message: diagnosticMessage,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// @ts-expect-error TS2345
|
|
130
|
+
validatorResult.errors.push({
|
|
131
|
+
origin: '@atlaspack/validator-typescript',
|
|
132
|
+
message: diagnosticMessage,
|
|
133
|
+
codeFrames: codeframe ? [codeframe] : undefined,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return validatorResult;
|
|
138
|
+
}
|
|
@@ -46,8 +46,9 @@ function _tsUtils() {
|
|
|
46
46
|
};
|
|
47
47
|
return data;
|
|
48
48
|
}
|
|
49
|
-
function _interopRequireDefault(
|
|
49
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
50
50
|
// eslint-disable-line import/no-extraneous-dependencies
|
|
51
|
+
|
|
51
52
|
let langServiceCache = {};
|
|
52
53
|
var _default = exports.default = new (_plugin().Validator)({
|
|
53
54
|
async validateAll({
|
|
@@ -90,7 +91,9 @@ var _default = exports.default = new (_plugin().Validator)({
|
|
|
90
91
|
return validatorResults;
|
|
91
92
|
}
|
|
92
93
|
});
|
|
93
|
-
async function getConfig(asset, options,
|
|
94
|
+
async function getConfig(asset, options,
|
|
95
|
+
// @ts-expect-error TS2304
|
|
96
|
+
resolveConfigWithPath) {
|
|
94
97
|
let configNames = ['tsconfig.json'];
|
|
95
98
|
let tsconfig = await (0, _utils().loadConfig)(asset.fs, asset.filePath, configNames, options.projectRoot);
|
|
96
99
|
let configPath = await resolveConfigWithPath(configNames, asset.filePath);
|
|
@@ -116,8 +119,6 @@ function tryCreateLanguageService(config, asset, options) {
|
|
|
116
119
|
langServiceCache[config.configHash] = {
|
|
117
120
|
configHost,
|
|
118
121
|
host,
|
|
119
|
-
// $FlowFixMe[incompatible-variance]
|
|
120
|
-
// $FlowFixMe[incompatible-call]
|
|
121
122
|
service: _typescript().default.createLanguageService(host, _typescript().default.createDocumentRegistry())
|
|
122
123
|
};
|
|
123
124
|
}
|
|
@@ -171,6 +172,8 @@ function getValidateResultFromDiagnostics(filePath, diagnostics) {
|
|
|
171
172
|
};
|
|
172
173
|
}
|
|
173
174
|
}
|
|
175
|
+
|
|
176
|
+
// @ts-expect-error TS2345
|
|
174
177
|
validatorResult.errors.push({
|
|
175
178
|
origin: '@atlaspack/validator-typescript',
|
|
176
179
|
message: diagnosticMessage,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/validator-typescript",
|
|
3
|
-
"version": "2.14.5-canary.
|
|
3
|
+
"version": "2.14.5-canary.500+cd4f5c39d",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -9,17 +9,18 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
11
11
|
},
|
|
12
|
-
"main": "lib/TypeScriptValidator.js",
|
|
13
|
-
"source": "src/TypeScriptValidator.
|
|
12
|
+
"main": "./lib/TypeScriptValidator.js",
|
|
13
|
+
"source": "./src/TypeScriptValidator.ts",
|
|
14
|
+
"types": "./lib/types/TypeScriptValidator.d.ts",
|
|
14
15
|
"engines": {
|
|
15
16
|
"node": ">= 16.0.0"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@atlaspack/diagnostic": "2.14.1-canary.
|
|
19
|
-
"@atlaspack/plugin": "2.14.5-canary.
|
|
20
|
-
"@atlaspack/ts-utils": "2.14.1-canary.
|
|
21
|
-
"@atlaspack/types": "2.14.5-canary.
|
|
22
|
-
"@atlaspack/utils": "2.14.5-canary.
|
|
19
|
+
"@atlaspack/diagnostic": "2.14.1-canary.568+cd4f5c39d",
|
|
20
|
+
"@atlaspack/plugin": "2.14.5-canary.500+cd4f5c39d",
|
|
21
|
+
"@atlaspack/ts-utils": "2.14.1-canary.568+cd4f5c39d",
|
|
22
|
+
"@atlaspack/types": "2.14.5-canary.500+cd4f5c39d",
|
|
23
|
+
"@atlaspack/utils": "2.14.5-canary.500+cd4f5c39d"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"typescript": ">=3.0.0"
|
|
@@ -28,5 +29,8 @@
|
|
|
28
29
|
"typescript": ">=3.0.0"
|
|
29
30
|
},
|
|
30
31
|
"type": "commonjs",
|
|
31
|
-
"
|
|
32
|
-
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "cd4f5c39de5781b1441a36efcaf4f105f9fe54b8"
|
|
36
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import type {
|
|
3
2
|
Asset,
|
|
4
3
|
ConfigResult,
|
|
@@ -9,33 +8,32 @@ import type {LanguageService, Diagnostic} from 'typescript'; // eslint-disable-l
|
|
|
9
8
|
|
|
10
9
|
import path from 'path';
|
|
11
10
|
import ts from 'typescript';
|
|
12
|
-
import {
|
|
11
|
+
import {DiagnosticCodeFrame, escapeMarkdown} from '@atlaspack/diagnostic';
|
|
13
12
|
import {hashObject, loadConfig} from '@atlaspack/utils';
|
|
14
13
|
import {Validator} from '@atlaspack/plugin';
|
|
15
14
|
import {LanguageServiceHost, ParseConfigHost} from '@atlaspack/ts-utils';
|
|
16
15
|
|
|
17
16
|
let langServiceCache: {
|
|
18
|
-
[configHash: string]: {
|
|
19
|
-
configHost: ParseConfigHost
|
|
20
|
-
host: LanguageServiceHost
|
|
21
|
-
service: LanguageService
|
|
22
|
-
|
|
23
|
-
...
|
|
17
|
+
[configHash: string]: {
|
|
18
|
+
configHost: ParseConfigHost;
|
|
19
|
+
host: LanguageServiceHost;
|
|
20
|
+
service: LanguageService;
|
|
21
|
+
};
|
|
24
22
|
} = {};
|
|
25
23
|
|
|
26
|
-
type TSValidatorConfig = {
|
|
27
|
-
filepath:
|
|
28
|
-
baseDir: string
|
|
29
|
-
configHash: string
|
|
30
|
-
tsconfig: ConfigResult | null
|
|
31
|
-
|
|
24
|
+
type TSValidatorConfig = {
|
|
25
|
+
filepath: string | null | undefined;
|
|
26
|
+
baseDir: string;
|
|
27
|
+
configHash: string;
|
|
28
|
+
tsconfig: ConfigResult | null;
|
|
29
|
+
};
|
|
32
30
|
|
|
33
|
-
export default
|
|
31
|
+
export default new Validator({
|
|
34
32
|
async validateAll({
|
|
35
33
|
assets,
|
|
36
34
|
options,
|
|
37
35
|
resolveConfigWithPath,
|
|
38
|
-
}): Promise<Array
|
|
36
|
+
}): Promise<Array<ValidateResult | null | undefined>> {
|
|
39
37
|
// Build a collection that of all the LanguageServices related to files that just changed.
|
|
40
38
|
let servicesToValidate: Set<string> = new Set();
|
|
41
39
|
await Promise.all(
|
|
@@ -55,7 +53,7 @@ export default (new Validator({
|
|
|
55
53
|
);
|
|
56
54
|
|
|
57
55
|
// Ask typescript to analyze all changed programs and translate the results into ValidatorResult objects.
|
|
58
|
-
let validatorResults: Array
|
|
56
|
+
let validatorResults: Array<ValidateResult | null | undefined> = [];
|
|
59
57
|
servicesToValidate.forEach((configHash) => {
|
|
60
58
|
// Make sure that the filesystem being used by the LanguageServiceHost and ParseConfigHost is up-to-date.
|
|
61
59
|
// (This could change in the context of re-running tests, and probably also for other reasons).
|
|
@@ -76,12 +74,13 @@ export default (new Validator({
|
|
|
76
74
|
|
|
77
75
|
return validatorResults;
|
|
78
76
|
},
|
|
79
|
-
})
|
|
77
|
+
}) as Validator;
|
|
80
78
|
|
|
81
79
|
async function getConfig(
|
|
82
|
-
asset,
|
|
83
|
-
options,
|
|
84
|
-
|
|
80
|
+
asset: Asset,
|
|
81
|
+
options: PluginOptions,
|
|
82
|
+
// @ts-expect-error TS2304
|
|
83
|
+
resolveConfigWithPath: ResolveConfigWithPathFn,
|
|
85
84
|
): Promise<TSValidatorConfig> {
|
|
86
85
|
let configNames = ['tsconfig.json'];
|
|
87
86
|
let tsconfig = await loadConfig(
|
|
@@ -90,7 +89,7 @@ async function getConfig(
|
|
|
90
89
|
configNames,
|
|
91
90
|
options.projectRoot,
|
|
92
91
|
);
|
|
93
|
-
let configPath:
|
|
92
|
+
let configPath: string | null | undefined = await resolveConfigWithPath(
|
|
94
93
|
configNames,
|
|
95
94
|
asset.filePath,
|
|
96
95
|
);
|
|
@@ -130,8 +129,6 @@ function tryCreateLanguageService(
|
|
|
130
129
|
langServiceCache[config.configHash] = {
|
|
131
130
|
configHost,
|
|
132
131
|
host,
|
|
133
|
-
// $FlowFixMe[incompatible-variance]
|
|
134
|
-
// $FlowFixMe[incompatible-call]
|
|
135
132
|
service: ts.createLanguageService(host, ts.createDocumentRegistry()),
|
|
136
133
|
};
|
|
137
134
|
}
|
|
@@ -141,7 +138,7 @@ function tryCreateLanguageService(
|
|
|
141
138
|
/** Translates semantic diagnostics (from TypeScript) into a ValidateResult that Parcel understands. */
|
|
142
139
|
function getValidateResultFromDiagnostics(
|
|
143
140
|
filePath: string,
|
|
144
|
-
diagnostics:
|
|
141
|
+
diagnostics: ReadonlyArray<Diagnostic>,
|
|
145
142
|
): ValidateResult {
|
|
146
143
|
let validatorResult = {
|
|
147
144
|
warnings: [],
|
|
@@ -159,7 +156,7 @@ function getValidateResultFromDiagnostics(
|
|
|
159
156
|
: diagnostic.messageText.messageText,
|
|
160
157
|
);
|
|
161
158
|
|
|
162
|
-
let codeframe:
|
|
159
|
+
let codeframe: DiagnosticCodeFrame | null | undefined;
|
|
163
160
|
if (file != null && diagnostic.start != null) {
|
|
164
161
|
let source = file.text || diagnostic.source;
|
|
165
162
|
if (file.fileName) {
|
|
@@ -205,6 +202,7 @@ function getValidateResultFromDiagnostics(
|
|
|
205
202
|
}
|
|
206
203
|
}
|
|
207
204
|
|
|
205
|
+
// @ts-expect-error TS2345
|
|
208
206
|
validatorResult.errors.push({
|
|
209
207
|
origin: '@atlaspack/validator-typescript',
|
|
210
208
|
message: diagnosticMessage,
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"include": ["src"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"composite": true
|
|
6
|
+
},
|
|
7
|
+
"references": [
|
|
8
|
+
{
|
|
9
|
+
"path": "../../core/diagnostic/tsconfig.json"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"path": "../../core/plugin/tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"path": "../../core/types/tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "../../core/utils/tsconfig.json"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"path": "../../utils/ts-utils/tsconfig.json"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|