UNPKG

3.44 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const ts = require("typescript");
17const model_1 = require("../model/model");
18const typescript_document_1 = require("./typescript-document");
19/**
20 * A TypeScript parser that only parses a single file, not imported files.
21 * This parser is suitable for parsing ES6 as well.
22 *
23 * This parser uses a TypeScript CompilerHost that resolves all imported
24 * modules to null, and resolve the standard library to an empty file.
25 * Type checking against the result will be riddled with errors, but the
26 * parsed AST can be used to find imports.
27 *
28 * This parser may eventually be replaced with a lightweight parser that
29 * can find import statements, but due to the addition of the import()
30 * function, it could be that a full parse is needed anyway.
31 */
32class TypeScriptPreparser {
33 parse(contents, url, _urlResolver, inlineInfo) {
34 const isInline = !!inlineInfo;
35 inlineInfo = inlineInfo || {};
36 const sourceFile = ts.createSourceFile(url, contents, ts.ScriptTarget.ES2016, true);
37 // TODO(justinfagnani): where does `parseDiagnostics` come from? Private
38 // property?
39 const sourceFileMaybeWithDiagnostics = sourceFile;
40 const diagnostics = sourceFileMaybeWithDiagnostics.parseDiagnostics || [];
41 const parseError = diagnostics.find((d) => d.category === ts.DiagnosticCategory.Error);
42 const result = new typescript_document_1.ParsedTypeScriptDocument({
43 url,
44 contents,
45 baseUrl: inlineInfo.baseUrl,
46 ast: sourceFile,
47 locationOffset: inlineInfo.locationOffset,
48 astNode: inlineInfo.astNode,
49 isInline,
50 });
51 if (parseError) {
52 if (parseError.start && parseError.length) {
53 const start = sourceFile.getLineAndCharacterOfPosition(parseError.start);
54 const end = sourceFile.getLineAndCharacterOfPosition(parseError.start + parseError.length);
55 throw new model_1.WarningCarryingException(new model_1.Warning({
56 code: 'parse-error',
57 severity: model_1.Severity.ERROR,
58 message: ts.flattenDiagnosticMessageText(parseError.messageText, '\n'),
59 sourceRange: model_1.correctSourceRange({
60 file: url,
61 start: { column: start.character, line: start.line },
62 end: { column: end.character, line: end.line }
63 }, inlineInfo.locationOffset),
64 parsedDocument: result,
65 }));
66 }
67 throw new Error(ts.flattenDiagnosticMessageText(parseError.messageText, '\n'));
68 }
69 return result;
70 }
71}
72exports.TypeScriptPreparser = TypeScriptPreparser;
73//# sourceMappingURL=typescript-preparser.js.map
\No newline at end of file