1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const babylon = require("babylon");
|
17 | const model_1 = require("../model/model");
|
18 | const document_1 = require("../parser/document");
|
19 | const javascript_document_1 = require("./javascript-document");
|
20 | const baseParseOptions = {
|
21 | plugins: [
|
22 | 'asyncGenerators',
|
23 | 'dynamicImport',
|
24 |
|
25 | 'importMeta',
|
26 | 'objectRestSpread',
|
27 | ],
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 | baseParseOptions['ranges'] = true;
|
33 | class JavaScriptParser {
|
34 | parse(contents, url, _urlResolver, inlineInfo) {
|
35 | const isInline = !!inlineInfo;
|
36 | inlineInfo = inlineInfo || {};
|
37 | const result = parseJs(contents, url, inlineInfo.locationOffset, undefined, this.sourceType);
|
38 | if (result.type === 'failure') {
|
39 |
|
40 | const minimalDocument = new document_1.UnparsableParsedDocument(url, contents);
|
41 | throw new model_1.WarningCarryingException(new model_1.Warning(Object.assign({ parsedDocument: minimalDocument }, result.warningish)));
|
42 | }
|
43 | return new javascript_document_1.JavaScriptDocument({
|
44 | url,
|
45 | contents,
|
46 | baseUrl: inlineInfo.baseUrl,
|
47 | ast: result.parsedFile,
|
48 | locationOffset: inlineInfo.locationOffset,
|
49 | astNode: inlineInfo.astNode,
|
50 | isInline,
|
51 | parsedAsSourceType: result.sourceType,
|
52 | });
|
53 | }
|
54 | }
|
55 | exports.JavaScriptParser = JavaScriptParser;
|
56 | class JavaScriptModuleParser extends JavaScriptParser {
|
57 | constructor() {
|
58 | super(...arguments);
|
59 | this.sourceType = 'module';
|
60 | }
|
61 | }
|
62 | exports.JavaScriptModuleParser = JavaScriptModuleParser;
|
63 | class JavaScriptScriptParser extends JavaScriptParser {
|
64 | constructor() {
|
65 | super(...arguments);
|
66 | this.sourceType = 'script';
|
67 | }
|
68 | }
|
69 | exports.JavaScriptScriptParser = JavaScriptScriptParser;
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function parseJs(contents, file, locationOffset, warningCode, sourceType) {
|
76 | if (!warningCode) {
|
77 | warningCode = 'parse-error';
|
78 | }
|
79 | let parsedFile;
|
80 | const parseOptions = Object.assign({ sourceFilename: file }, baseParseOptions);
|
81 | try {
|
82 |
|
83 |
|
84 |
|
85 | if (!sourceType) {
|
86 | try {
|
87 | sourceType = 'script';
|
88 | parsedFile = babylon.parse(contents, Object.assign({ sourceType }, parseOptions));
|
89 | }
|
90 | catch (_ignored) {
|
91 | sourceType = 'module';
|
92 | parsedFile = babylon.parse(contents, Object.assign({ sourceType }, parseOptions));
|
93 | }
|
94 | }
|
95 | else {
|
96 | parsedFile = babylon.parse(contents, Object.assign({ sourceType }, parseOptions));
|
97 | }
|
98 | return {
|
99 | type: 'success',
|
100 | sourceType: sourceType,
|
101 | parsedFile,
|
102 | };
|
103 | }
|
104 | catch (err) {
|
105 | if (err instanceof SyntaxError) {
|
106 | updateLineNumberAndColumnForError(err);
|
107 | return {
|
108 | type: 'failure',
|
109 | warningish: {
|
110 | message: err.message.split('\n')[0],
|
111 | severity: model_1.Severity.ERROR,
|
112 | code: warningCode,
|
113 | sourceRange: model_1.correctSourceRange({
|
114 | file,
|
115 | start: { line: err.lineNumber - 1, column: err.column - 1 },
|
116 | end: { line: err.lineNumber - 1, column: err.column - 1 }
|
117 | }, locationOffset),
|
118 | }
|
119 | };
|
120 | }
|
121 | throw err;
|
122 | }
|
123 | }
|
124 | exports.parseJs = parseJs;
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function updateLineNumberAndColumnForError(err) {
|
131 | if (typeof err.lineNumber === 'number' && typeof err.column === 'number') {
|
132 | return;
|
133 | }
|
134 | if (!err.message) {
|
135 | return;
|
136 | }
|
137 | const lineAndColumnMatch = err.message.match(/(Unexpected token.*)\(([0-9]+):([0-9]+)\)/);
|
138 | if (!lineAndColumnMatch) {
|
139 | return;
|
140 | }
|
141 | err.lineNumber = parseInt(lineAndColumnMatch[2], 10);
|
142 | err.column = parseInt(lineAndColumnMatch[3], 10) + 1;
|
143 | }
|
144 |
|
\ | No newline at end of file |