Skip to content

Commit d080163

Browse files
committed
rework settings and add svelte3/ignore-styles
1 parent aedfc03 commit d080163

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const undedentCode = (message, { offsets, totalOffsets }) => {
107107

108108
const { compile } = require('svelte/compiler');
109109

110-
let messages, ignore, moduleUnoffsets, moduleOffsets, instanceUnoffsets, instanceOffsets, moduleDedent, instanceDedent;
110+
let messages, ignoreWarnings, moduleUnoffsets, moduleOffsets, instanceUnoffsets, instanceOffsets, moduleDedent, instanceDedent;
111111

112112
// extract scripts to lint from component definition
113113
const preprocess = text => {
@@ -136,7 +136,7 @@ const preprocess = text => {
136136
const reassignedVars = vars.filter(v => v.reassigned || v.export_name);
137137

138138
// convert warnings to eslint messages
139-
messages = warnings.filter(({ code }) => !ignore.includes(code)).map(({ code, message, start, end }) => ({
139+
messages = (ignoreWarnings ? warnings.filter(({ code }) => !ignoreWarnings(code)) : warnings).map(({ code, message, start, end }) => ({
140140
ruleId: code,
141141
severity: 1,
142142
message,
@@ -224,13 +224,16 @@ if (!LinterPath) {
224224
}
225225
const Linter = require(LinterPath);
226226

227-
// get an array-valued setting from ESLint config
228-
const getArraySetting = (config, key, defaultValue) => {
229-
const value = config && config.settings && config.settings[key] || defaultValue;
230-
if (!Array.isArray(value)) {
231-
throw new Error(`Setting ${key} is not an array`);
227+
// get a setting from the ESLint config
228+
const getSettingFunction = (config, key, defaultValue) => {
229+
if (!config || !config.settings || !(key in config.settings)) {
230+
return defaultValue;
232231
}
233-
return value;
232+
const value = config.settings[key];
233+
return typeof value === 'function' ? value :
234+
typeof value === 'boolean' ? () => value :
235+
Array.isArray(value) ? Array.prototype.includes.bind(value) :
236+
v => v === value;
234237
};
235238

236239
// patch Linter#verify
@@ -240,11 +243,22 @@ Linter.prototype.verify = function(code, config, options) {
240243
options = { filename: options };
241244
}
242245
if (options && options.filename) {
243-
const extensions = getArraySetting(config, 'svelte3/extensions', ['.svelte']);
244-
ignore = getArraySetting(config, 'svelte3/ignore', []);
245-
if (extensions.some(extension => options.filename.endsWith(extension))) {
246+
if (getSettingFunction(config, 'svelte3/enabled', n => n.endsWith('.svelte'))(options.filename)) {
246247
// lint this Svelte file
247248
options = Object.assign({}, options, { preprocess, postprocess });
249+
ignoreWarnings = getSettingFunction(config, 'svelte3/ignore-warnings', false);
250+
const ignoreStyles = getSettingFunction(config, 'svelte3/ignore-styles', false);
251+
if (ignoreStyles) {
252+
// wipe the appropriate <style> tags in the file
253+
code = code.replace(/<style([^]*?)>[^]*?<\/style>/gi, (match, attributes) => {
254+
const attrs = {};
255+
attributes.split(/\s+/).filter(Boolean).forEach(attr => {
256+
const [name, value] = attr.split('=');
257+
attrs[name] = value ? /^['"]/.test(value) ? value.slice(1, -1) : value : true;
258+
});
259+
return ignoreStyles(attrs) ? match.replace(/\S/g, ' ') : match;
260+
});
261+
}
248262
}
249263
}
250264

0 commit comments

Comments
 (0)