@marko/compiler 5.38.4 → 5.39.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.
@@ -0,0 +1,194 @@
1
+ "use strict";exports.__esModule = true;exports.computeNode = computeNode; /**
2
+ * @param {import("@babel/types").Node} node
3
+ */
4
+ function computeNode(node) {
5
+ switch (node.type) {
6
+ case "StringLiteral":
7
+ case "NumericLiteral":
8
+ case "BooleanLiteral":
9
+ return { value: node.value };
10
+ case "RegExpLiteral":
11
+ return { value: new RegExp(node.pattern, node.flags) };
12
+ case "NullLiteral":
13
+ return { value: null };
14
+ case "Identifier":
15
+ switch (node.name) {
16
+ case "undefined":
17
+ return { value: undefined };
18
+ case "NaN":
19
+ return { value: NaN };
20
+ case "Infinity":
21
+ return { value: Infinity };
22
+ default:
23
+ return;
24
+ }
25
+ case "BigIntLiteral":
26
+ return { value: BigInt(node.value) };
27
+ case "BinaryExpression":{
28
+ const left = computeNode(node.left);
29
+ if (!left) return;
30
+ const right = computeNode(node.right);
31
+ if (!right) return;
32
+ switch (node.operator) {
33
+ case "+":
34
+ return { value: left.value + right.value };
35
+ case "-":
36
+ return { value: left.value - right.value };
37
+ case "*":
38
+ return { value: left.value * right.value };
39
+ case "/":
40
+ return { value: left.value / right.value };
41
+ case "%":
42
+ return { value: left.value % right.value };
43
+ case "**":
44
+ return { value: left.value ** right.value };
45
+ case "|":
46
+ return { value: left.value | right.value };
47
+ case "&":
48
+ return { value: left.value & right.value };
49
+ case "^":
50
+ return { value: left.value ^ right.value };
51
+ case "<<":
52
+ return { value: left.value << right.value };
53
+ case ">>":
54
+ return { value: left.value >> right.value };
55
+ case ">>>":
56
+ return { value: left.value >>> right.value };
57
+ case "==":
58
+ return { value: left.value == right.value };
59
+ case "!=":
60
+ return { value: left.value != right.value };
61
+ case "===":
62
+ return { value: left.value === right.value };
63
+ case "!==":
64
+ return { value: left.value !== right.value };
65
+ case "<":
66
+ return { value: left.value < right.value };
67
+ case "<=":
68
+ return { value: left.value <= right.value };
69
+ case ">":
70
+ return { value: left.value > right.value };
71
+ case ">=":
72
+ return { value: left.value >= right.value };
73
+ default:
74
+ return;
75
+ }
76
+ }
77
+ case "UnaryExpression":{
78
+ const arg = computeNode(node.argument);
79
+ if (!arg) return;
80
+ switch (node.operator) {
81
+ case "+":
82
+ return { value: +arg.value };
83
+ case "-":
84
+ return { value: -arg.value };
85
+ case "~":
86
+ return { value: ~arg.value };
87
+ case "!":
88
+ return { value: !arg.value };
89
+ case "typeof":
90
+ return { value: typeof arg.value };
91
+ case "void":
92
+ return { value: void arg.value };
93
+ default:
94
+ return;
95
+ }
96
+ }
97
+ case "LogicalExpression":{
98
+ const left = computeNode(node.left);
99
+ if (!left) return;
100
+ const right = computeNode(node.right);
101
+ if (!right) return;
102
+ switch (node.operator) {
103
+ case "&&":
104
+ return { value: left.value && right.value };
105
+ case "||":
106
+ return { value: left.value || right.value };
107
+ case "??":
108
+ return { value: left.value ?? right.value };
109
+ default:
110
+ return;
111
+ }
112
+ }
113
+ case "ConditionalExpression":{
114
+ const test = computeNode(node.test);
115
+ if (!test) return;
116
+ const consequent = computeNode(node.consequent);
117
+ if (!consequent) return;
118
+ const alternate = computeNode(node.alternate);
119
+ if (!alternate) return;
120
+ return { value: test.value ? consequent.value : alternate.value };
121
+ }
122
+ case "TemplateLiteral":{
123
+ let value = node.quasis[0].value.cooked;
124
+ for (let i = 0; i < node.expressions.length; i++) {
125
+ const expr = computeNode(node.expressions[i]);
126
+ if (!expr) return;
127
+ value += expr.value + node.quasis[i + 1].value.cooked;
128
+ }
129
+ return { value };
130
+ }
131
+ case "ObjectExpression":{
132
+ const value = {};
133
+ for (const prop of node.properties) {
134
+ if (prop.decorators) return;
135
+ switch (prop.type) {
136
+ case "ObjectProperty":{
137
+ let key;
138
+ if (prop.computed) {
139
+ const keyNode = computeNode(prop.key);
140
+ if (!keyNode) return;
141
+ key = keyNode.value + "";
142
+ } else {
143
+ switch (prop.key.type) {
144
+ case "Identifier":
145
+ key = prop.key.name;
146
+ break;
147
+ case "StringLiteral":
148
+ key = prop.key.value;
149
+ break;
150
+ default:
151
+ return;
152
+ }
153
+ }
154
+
155
+ const propValue = computeNode(prop.value);
156
+ if (!propValue) return;
157
+ value[key] = propValue.value;
158
+ break;
159
+ }
160
+ case "SpreadElement":{
161
+ const arg = computeNode(prop.argument);
162
+ if (!arg) return;
163
+ Object.assign(value, arg.value);
164
+ break;
165
+ }
166
+ }
167
+ }
168
+
169
+ return { value };
170
+ }
171
+ case "ArrayExpression":{
172
+ const value = [];
173
+ for (const elem of node.elements) {
174
+ if (elem) {
175
+ if (elem.type === "SpreadElement") {
176
+ const arg = computeNode(elem.argument);
177
+ if (typeof arg?.value?.[Symbol.iterator] !== "function") return;
178
+ for (const item of arg.value) {
179
+ value.push(item);
180
+ }
181
+ } else {
182
+ const elemValue = computeNode(elem);
183
+ if (!elemValue) return;
184
+ value.push(elemValue.value);
185
+ }
186
+ } else {
187
+ value.length++;
188
+ }
189
+ }
190
+
191
+ return { value };
192
+ }
193
+ }
194
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";exports.__esModule = true;exports.DiagnosticType = void 0;exports.diagnosticDeprecate = diagnosticDeprecate;exports.diagnosticError = diagnosticError;exports.diagnosticSuggest = diagnosticSuggest;exports.diagnosticWarn = diagnosticWarn;const DiagnosticType = exports.DiagnosticType = {
2
+ Error: "error",
3
+ Warning: "warning",
4
+ Deprecation: "deprecation",
5
+ Suggestion: "suggestion"
6
+ };
7
+
8
+ function diagnosticError(path, options) {
9
+ add(DiagnosticType.Error, path, options);
10
+ }
11
+
12
+ function diagnosticWarn(path, options) {
13
+ add(DiagnosticType.Warning, path, options);
14
+ }
15
+
16
+ function diagnosticDeprecate(path, options) {
17
+ add(DiagnosticType.Deprecation, path, options);
18
+ }
19
+
20
+ function diagnosticSuggest(path, options) {
21
+ add(DiagnosticType.Suggestion, path, options);
22
+ }
23
+
24
+ function add(type, path, options) {
25
+ const { file } = path.hub;
26
+ const { diagnostics } = file.metadata.marko;
27
+ const { label, fix: rawFix, loc = path.node.loc } = options;
28
+ let fix = false;
29
+
30
+ if (rawFix) {
31
+ switch (file.___compileStage) {
32
+ case "parse":
33
+ case "migrate":
34
+ break;
35
+ default:
36
+ throw new Error(
37
+ "Diagnostic fixes can only be registered up to and including the migrate stage."
38
+ );
39
+ }
40
+
41
+ const { applyFixes } = file.markoOpts;
42
+ let apply;
43
+
44
+ if (typeof rawFix === "function") {
45
+ apply = rawFix;
46
+ fix = true;
47
+ } else {
48
+ // strip off the apply function.
49
+ ({ apply, ...fix } = rawFix);
50
+ }
51
+
52
+ if (applyFixes) {
53
+ const i = diagnostics.length;
54
+ if (applyFixes.has(i)) {
55
+ apply(applyFixes.get(i));
56
+ }
57
+ } else {
58
+ apply(undefined);
59
+ }
60
+ }
61
+
62
+ diagnostics.push({ type, label, loc, fix });
63
+ }
@@ -0,0 +1,136 @@
1
+ "use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule = true;exports.importDefault = importDefault;exports.importNamed = importNamed;exports.importStar = importStar;exports.resolveRelativePath = resolveRelativePath;var _compiler = require("@marko/compiler");
2
+ var _path = _interopRequireDefault(require("path"));
3
+ var _relativeImportPath = require("relative-import-path");
4
+
5
+ const IMPORTS_KEY = Symbol();
6
+ const FS_START = _path.default.sep === "/" ? _path.default.sep : /^(.*?:)/.exec(process.cwd())[1];
7
+
8
+ function resolveRelativePath(file, request) {
9
+ if (request.startsWith(FS_START)) {
10
+ request = (0, _relativeImportPath.relativeImportPath)(file.opts.filename, request);
11
+ }
12
+
13
+ if (file.markoOpts.optimize) {
14
+ request = request.replace(
15
+ /(^|\/node-modules\/)marko\/src\//,
16
+ "$1marko/dist/"
17
+ );
18
+ }
19
+
20
+ return request;
21
+ }
22
+
23
+ function importDefault(file, request, nameHint) {
24
+ const imports = getImports(file);
25
+ request = resolveRelativePath(file, request);
26
+ let importDeclaration = imports.get(request);
27
+
28
+ if (!importDeclaration) {
29
+ imports.set(
30
+ request,
31
+ importDeclaration = file.path.pushContainer(
32
+ "body",
33
+ _compiler.types.importDeclaration([], _compiler.types.stringLiteral(request))
34
+ )[0]
35
+ );
36
+ }
37
+
38
+ if (!nameHint) {
39
+ return;
40
+ }
41
+
42
+ const specifiers = importDeclaration.get("specifiers");
43
+ const specifier = specifiers.find((specifier) =>
44
+ specifier.isImportDefaultSpecifier()
45
+ );
46
+
47
+ if (!specifier) {
48
+ const identifier = file.scope.generateUidIdentifier(nameHint);
49
+ importDeclaration.pushContainer(
50
+ "specifiers",
51
+ _compiler.types.importDefaultSpecifier(identifier)
52
+ );
53
+ return identifier;
54
+ }
55
+
56
+ return _compiler.types.identifier(specifier.node.local.name);
57
+ }
58
+
59
+ function importNamed(file, request, name, nameHint = name) {
60
+ request = resolveRelativePath(file, request);
61
+ const imports = getImports(file);
62
+ let importDeclaration = imports.get(request);
63
+
64
+ if (!importDeclaration) {
65
+ imports.set(
66
+ request,
67
+ importDeclaration = file.path.pushContainer(
68
+ "body",
69
+ _compiler.types.importDeclaration([], _compiler.types.stringLiteral(request))
70
+ )[0]
71
+ );
72
+ }
73
+
74
+ const specifiers = importDeclaration.get("specifiers");
75
+ const specifier = specifiers.find(
76
+ (specifier) =>
77
+ specifier.isImportSpecifier() && specifier.node.imported.name === name
78
+ );
79
+
80
+ if (!specifier) {
81
+ const identifier = file.scope.generateUidIdentifier(nameHint);
82
+ importDeclaration.pushContainer(
83
+ "specifiers",
84
+ _compiler.types.importSpecifier(identifier, _compiler.types.identifier(name))
85
+ );
86
+ return identifier;
87
+ }
88
+
89
+ return _compiler.types.identifier(specifier.node.local.name);
90
+ }
91
+
92
+ function importStar(file, request, nameHint) {
93
+ const imports = getImports(file);
94
+ request = resolveRelativePath(file, request);
95
+ let importDeclaration = imports.get(request);
96
+
97
+ if (!importDeclaration) {
98
+ imports.set(
99
+ request,
100
+ importDeclaration = file.path.pushContainer(
101
+ "body",
102
+ _compiler.types.importDeclaration([], _compiler.types.stringLiteral(request))
103
+ )[0]
104
+ );
105
+ }
106
+
107
+ if (!nameHint) {
108
+ return;
109
+ }
110
+
111
+ const specifiers = importDeclaration.get("specifiers");
112
+ const specifier = specifiers.find((specifier) =>
113
+ specifier.isImportNamespaceSpecifier()
114
+ );
115
+
116
+ if (!specifier) {
117
+ const identifier = file.scope.generateUidIdentifier(nameHint);
118
+ importDeclaration.pushContainer(
119
+ "specifiers",
120
+ _compiler.types.importNamespaceSpecifier(identifier)
121
+ );
122
+ return identifier;
123
+ }
124
+
125
+ return _compiler.types.identifier(specifier.node.local.name);
126
+ }
127
+
128
+ function getImports(file) {
129
+ let imports = file.metadata.marko[IMPORTS_KEY];
130
+
131
+ if (!imports) {
132
+ imports = file.metadata.marko[IMPORTS_KEY] = new Map();
133
+ }
134
+
135
+ return imports;
136
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";exports.__esModule = true;exports.computeNode = exports.assertNoVar = exports.assertNoParams = exports.assertNoAttributes = exports.assertNoAttributeTags = exports.assertNoArgs = exports.assertAttributesOrSingleArg = exports.assertAttributesOrArgs = exports.assertAllowedAttributes = exports.DiagnosticType = void 0;exports.defineTag = defineTag;exports.withLoc = exports.resolveTagImport = exports.resolveRelativePath = exports.registerMacro = exports.parseVar = exports.parseTypeParams = exports.parseTypeArgs = exports.parseTemplateLiteral = exports.parseStatements = exports.parseParams = exports.parseExpression = exports.parseArgs = exports.normalizeTemplateString = exports.loadFileForTag = exports.loadFileForImport = exports.isTransparentTag = exports.isNativeTag = exports.isMacroTag = exports.isLoopTag = exports.isDynamicTag = exports.isAttributeTag = exports.importStar = exports.importNamed = exports.importDefault = exports.hasMacro = exports.getTemplateId = exports.getTaglibLookup = exports.getTagTemplate = exports.getTagDefForTagName = exports.getTagDef = exports.getStart = exports.getMacroIdentifierForName = exports.getMacroIdentifier = exports.getLocRange = exports.getLoc = exports.getFullyResolvedTagName = exports.getEnd = exports.getArgOrSequence = exports.findParentTag = exports.findAttributeTags = exports.diagnosticWarn = exports.diagnosticSuggest = exports.diagnosticError = exports.diagnosticDeprecate = void 0;var _assert = require("./assert");exports.assertAllowedAttributes = _assert.assertAllowedAttributes;exports.assertAttributesOrArgs = _assert.assertAttributesOrArgs;exports.assertAttributesOrSingleArg = _assert.assertAttributesOrSingleArg;exports.assertNoArgs = _assert.assertNoArgs;exports.assertNoAttributes = _assert.assertNoAttributes;exports.assertNoAttributeTags = _assert.assertNoAttributeTags;exports.assertNoParams = _assert.assertNoParams;exports.assertNoVar = _assert.assertNoVar;
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ var _compute = require("./compute");exports.computeNode = _compute.computeNode;
12
+ var _diagnostics = require("./diagnostics");exports.diagnosticDeprecate = _diagnostics.diagnosticDeprecate;exports.diagnosticError = _diagnostics.diagnosticError;exports.diagnosticSuggest = _diagnostics.diagnosticSuggest;exports.DiagnosticType = _diagnostics.DiagnosticType;exports.diagnosticWarn = _diagnostics.diagnosticWarn;
13
+
14
+
15
+
16
+
17
+
18
+
19
+ var _imports = require("./imports");exports.importDefault = _imports.importDefault;exports.importNamed = _imports.importNamed;exports.importStar = _imports.importStar;exports.resolveRelativePath = _imports.resolveRelativePath;
20
+
21
+
22
+
23
+
24
+
25
+ var _loc = require("./loc");exports.getEnd = _loc.getEnd;exports.getLoc = _loc.getLoc;exports.getLocRange = _loc.getLocRange;exports.getStart = _loc.getStart;exports.withLoc = _loc.withLoc;
26
+ var _parse = require("./parse");exports.parseArgs = _parse.parseArgs;exports.parseExpression = _parse.parseExpression;exports.parseParams = _parse.parseParams;exports.parseStatements = _parse.parseStatements;exports.parseTemplateLiteral = _parse.parseTemplateLiteral;exports.parseTypeArgs = _parse.parseTypeArgs;exports.parseTypeParams = _parse.parseTypeParams;exports.parseVar = _parse.parseVar;
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+ var _taglib = require("./taglib");exports.getTagDefForTagName = _taglib.getTagDefForTagName;exports.getTaglibLookup = _taglib.getTaglibLookup;
37
+ var _tags = require("./tags");exports.findAttributeTags = _tags.findAttributeTags;exports.findParentTag = _tags.findParentTag;exports.getArgOrSequence = _tags.getArgOrSequence;exports.getFullyResolvedTagName = _tags.getFullyResolvedTagName;exports.getMacroIdentifier = _tags.getMacroIdentifier;exports.getMacroIdentifierForName = _tags.getMacroIdentifierForName;exports.getTagDef = _tags.getTagDef;exports.getTagTemplate = _tags.getTagTemplate;exports.getTemplateId = _tags.getTemplateId;exports.hasMacro = _tags.hasMacro;exports.isAttributeTag = _tags.isAttributeTag;exports.isDynamicTag = _tags.isDynamicTag;exports.isLoopTag = _tags.isLoopTag;exports.isMacroTag = _tags.isMacroTag;exports.isNativeTag = _tags.isNativeTag;exports.isTransparentTag = _tags.isTransparentTag;exports.loadFileForImport = _tags.loadFileForImport;exports.loadFileForTag = _tags.loadFileForTag;exports.registerMacro = _tags.registerMacro;exports.resolveTagImport = _tags.resolveTagImport;
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+ var _templateString = require("./template-string");exports.normalizeTemplateString = _templateString.normalizeTemplateString;
60
+
61
+ function defineTag(tag) {
62
+ return tag;
63
+ } // just used for adding types for compiler plugins.
@@ -0,0 +1,107 @@
1
+ "use strict";exports.__esModule = true;exports.getEnd = getEnd;exports.getLoc = getLoc;exports.getLocRange = getLocRange;exports.getStart = getStart;exports.withLoc = withLoc;const LINE_INDEX_KEY = Symbol();
2
+
3
+ function getLoc(file, index) {
4
+ return findLoc(getLineIndexes(file), 0, index);
5
+ }
6
+
7
+ function getLocRange(file, start, end) {
8
+ const lineIndexes = getLineIndexes(file);
9
+ const startLoc = findLoc(lineIndexes, 0, start);
10
+
11
+ if (startLoc) {
12
+ const endLoc =
13
+ start === end ? startLoc : findLoc(lineIndexes, startLoc.line - 1, end);
14
+
15
+ return {
16
+ start: startLoc,
17
+ end: endLoc
18
+ };
19
+ }
20
+ }
21
+
22
+ function withLoc(file, node, start, end) {
23
+ node.loc = getLocRange(file, start, end);
24
+ node.start = start;
25
+ node.end = end;
26
+ return node;
27
+ }
28
+
29
+ function getStart(file, node) {
30
+ // Restore if merged: https://github.com/babel/babel/pull/16849
31
+ // if (node.start != null) {
32
+ // return node.start;
33
+ // }
34
+
35
+ if (node.loc) {
36
+ return locToIndex(file, node.loc.start);
37
+ }
38
+
39
+ return null;
40
+ }
41
+
42
+ function getEnd(file, node) {
43
+ // Restore if merged: https://github.com/babel/babel/pull/16849
44
+ // if (node.end != null) {
45
+ // return node.end;
46
+ // }
47
+
48
+ if (node.loc) {
49
+ return locToIndex(file, node.loc.end);
50
+ }
51
+
52
+ return null;
53
+ }
54
+
55
+ function locToIndex(file, loc) {
56
+ const { line, column } = loc;
57
+ return line === 1 ? column : getLineIndexes(file)[line - 1] + column + 1;
58
+ }
59
+
60
+ function getLineIndexes(file) {
61
+ let lineIndexes = file.metadata.marko[LINE_INDEX_KEY];
62
+
63
+ if (!lineIndexes) {
64
+ lineIndexes = [-1];
65
+ for (let i = 0; i < file.code.length; i++) {
66
+ if (file.code[i] === "\n") {
67
+ lineIndexes.push(i);
68
+ }
69
+ }
70
+
71
+ file.metadata.marko[LINE_INDEX_KEY] = lineIndexes;
72
+ }
73
+
74
+ return lineIndexes;
75
+ }
76
+
77
+ function findLoc(lineIndexes, startLine, index) {
78
+ const endLine = lineIndexes.length - 1;
79
+ let max = endLine;
80
+ let line = startLine;
81
+
82
+ while (line < max) {
83
+ const mid = line + max >>> 1;
84
+ if (lineIndexes[mid] < index) {
85
+ line = mid + 1;
86
+ } else {
87
+ max = mid;
88
+ }
89
+ }
90
+
91
+ let lineIndex = lineIndexes[line];
92
+ if (lineIndex >= index) {
93
+ lineIndex = lineIndexes[--line];
94
+ }
95
+
96
+ if (line === 0) {
97
+ return {
98
+ line: 1,
99
+ column: index
100
+ };
101
+ }
102
+
103
+ return {
104
+ line: line + 1,
105
+ column: index - lineIndex - 1
106
+ };
107
+ }