@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
79 lines (77 loc) • 3.6 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createLintRuleWithTypedConfig = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ajv = _interopRequireDefault(require("ajv"));
var _getRuleUrl = require("./get-rule-url");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
* A single shared Ajv instance is reused across every rule instead of constructing
* a new one each time a default config is needed. Ajv's constructor is expensive
* (it compiles its own meta-schemas), so we only ever want to pay for it once.
*/
var ajv = new _ajv.default({
useDefaults: true
});
var createLintRuleWithTypedConfig = exports.createLintRuleWithTypedConfig = function createLintRuleWithTypedConfig(rule) {
/**
* Lazily-computed, cached default config.
*
* The default config is derived purely from the rule's (static) schema, so it is
* constant for the life of the process. Compiling the schema with Ajv involves
* runtime code generation (`new Function`), which is expensive — previously this
* ran on every `getDefaultConfig()` call, i.e. once per linted file. We now compute
* it at most once per rule and reuse the result.
*/
var defaultConfig;
/**
* Gets the default config by running the same validator
* ESLint does against an empty config object with
* `useDefaults` option enabled.
*
* Any properties with a `default` property set in the schema
* will be populated with the specified default value.
*/
function getDefaultConfig() {
if (defaultConfig === undefined) {
var validate = ajv.compile(rule.meta.schema);
var _config = {};
/**
* The `validate()` function mutates the config object.
*/
validate(_config);
defaultConfig = _config;
}
return defaultConfig;
}
return _objectSpread(_objectSpread({}, rule), {}, {
meta: _objectSpread(_objectSpread({}, rule.meta), {}, {
docs: _objectSpread(_objectSpread({}, rule.meta.docs), {}, {
url: (0, _getRuleUrl.getRuleUrl)(rule.meta.name)
}),
schema: [rule.meta.schema]
}),
create: function create(context) {
/**
* The config the user has passed.
*
* If they passed a config object, even if it is missing properties,
* ESLint will have added any default values defined using the
* `default` property in the schema.
*
* https://github.com/eslint/eslint/pull/11288
*/
var userConfig = context.options[0];
/**
* If the user passed no config object at all we need to obtain
* the default ourselves.
*/
var config = userConfig !== null && userConfig !== void 0 ? userConfig : getDefaultConfig();
return rule.create(context, config);
}
});
};