UNPKG

3.98 kBJavaScriptView Raw
1/* eslint max-len: 0 */
2
3"use strict";
4
5var _interopRequireWildcard = require("babel-runtime/helpers/interop-require-wildcard")["default"];
6
7exports.__esModule = true;
8exports.get = get;
9exports.parseArgs = parseArgs;
10
11var _util = require("util");
12
13var util = _interopRequireWildcard(_util);
14
15/**
16 * Mapping of messages to be used in Babel.
17 * Messages can include $0-style placeholders.
18 */
19
20var MESSAGES = {
21 tailCallReassignmentDeopt: "Function reference has been reassigned, so it will probably be dereferenced, therefore we can't optimise this with confidence",
22 classesIllegalBareSuper: "Illegal use of bare super",
23 classesIllegalSuperCall: "Direct super call is illegal in non-constructor, use super.$1() instead",
24 scopeDuplicateDeclaration: "Duplicate declaration $1",
25 settersNoRest: "Setters aren't allowed to have a rest",
26 noAssignmentsInForHead: "No assignments allowed in for-in/of head",
27 expectedMemberExpressionOrIdentifier: "Expected type MemberExpression or Identifier",
28 invalidParentForThisNode: "We don't know how to handle this node within the current parent - please open an issue",
29 readOnly: "$1 is read-only",
30 unknownForHead: "Unknown node type $1 in ForStatement",
31 didYouMean: "Did you mean $1?",
32 codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2.",
33 missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues",
34 unsupportedOutputType: "Unsupported output type $1",
35 illegalMethodName: "Illegal method name $1",
36 lostTrackNodePath: "We lost track of this node's position, likely because the AST was directly manipulated",
37
38 modulesIllegalExportName: "Illegal export $1",
39 modulesDuplicateDeclarations: "Duplicate module declarations with the same source but in different scopes",
40
41 undeclaredVariable: "Reference to undeclared variable $1",
42 undeclaredVariableType: "Referencing a type alias outside of a type annotation",
43 undeclaredVariableSuggestion: "Reference to undeclared variable $1 - did you mean $2?",
44
45 traverseNeedsParent: "You must pass a scope and parentPath unless traversing a Program/File. Instead of that you tried to traverse a $1 node without passing scope and parentPath.",
46 traverseVerifyRootFunction: "You passed `traverse()` a function when it expected a visitor object, are you sure you didn't mean `{ enter: Function }`?",
47 traverseVerifyVisitorProperty: "You passed `traverse()` a visitor object with the property $1 that has the invalid property $2",
48 traverseVerifyNodeType: "You gave us a visitor for the node type $1 but it's not a valid type",
49
50 pluginNotObject: "Plugin $2 specified in $1 was expected to return an object when invoked but returned $3",
51 pluginNotFunction: "Plugin $2 specified in $1 was expected to return a function but returned $3",
52 pluginUnknown: "Unknown plugin $1 specified in $2 at $3, attempted to resolve relative to $4",
53 pluginInvalidProperty: "Plugin $2 specified in $1 provided an invalid property of $3"
54};
55
56exports.MESSAGES = MESSAGES;
57/**
58 * Get a message with $0 placeholders replaced by arguments.
59 */
60
61function get(key) {
62 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
63 args[_key - 1] = arguments[_key];
64 }
65
66 var msg = MESSAGES[key];
67 if (!msg) throw new ReferenceError("Unknown message " + JSON.stringify(key));
68
69 // stringify args
70 args = parseArgs(args);
71
72 // replace $0 placeholders with args
73 return msg.replace(/\$(\d+)/g, function (str, i) {
74 return args[i - 1];
75 });
76}
77
78/**
79 * Stingify arguments to be used inside messages.
80 */
81
82function parseArgs(args) {
83 return args.map(function (val) {
84 if (val != null && val.inspect) {
85 return val.inspect();
86 } else {
87 try {
88 return JSON.stringify(val) || val + "";
89 } catch (e) {
90 return util.inspect(val);
91 }
92 }
93 });
94}
\No newline at end of file