Skip to content

Commit

Permalink
ESLint updates and linting cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-dean committed Apr 2, 2023
1 parent 180d676 commit 5e64d5b
Show file tree
Hide file tree
Showing 59 changed files with 315 additions and 329 deletions.
50 changes: 50 additions & 0 deletions packages/less/.eslintrc.js
@@ -0,0 +1,50 @@
module.exports = {
'parser': '@typescript-eslint/parser',
'extends': 'eslint:recommended',
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module'
},
'plugins': ['@typescript-eslint'],
'env': {
'browser': true,
'node': true,
'mocha': true
},
'globals': {},
'rules': {
indent: ['error', 4, {
SwitchCase: 1
}],
'no-empty': ['error', { 'allowEmptyCatch': true }],
quotes: ['error', 'single', {
avoidEscape: true
}],
/**
* The codebase uses some while(true) statements.
* Refactor to remove this rule.
*/
'no-constant-condition': 0,
/**
* Less combines assignments with conditionals sometimes
*/
'no-cond-assign': 0,
/**
* @todo - remove when some kind of code style (XO?) is added
*/
'no-multiple-empty-lines': 'error'
},
'overrides': [
{
files: ['*.ts'],
extends: ['plugin:@typescript-eslint/recommended'],
rules: {
/**
* Suppress until Less has better-defined types
* @see https://github.com/less/less.js/discussions/3786
*/
'@typescript-eslint/no-explicit-any': 0
}
}
]
}
71 changes: 0 additions & 71 deletions packages/less/.eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/less/Gruntfile.js
Expand Up @@ -283,7 +283,7 @@ module.exports = function(grunt) {
"!test/less/errors/plugin/plugin-error.js"
],
options: {
configFile: ".eslintrc.json",
configFile: ".eslintrc.js",
fix: true
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/less/package.json
Expand Up @@ -37,6 +37,7 @@
"scripts": {
"test": "grunt test",
"grunt": "grunt",
"lint": "eslint '**/*.{ts,js}'",
"build": "npm-run-all clean compile",
"clean": "shx rm -rf ./lib tsconfig.tsbuildinfo",
"compile": "tsc -p tsconfig.json",
Expand Down
4 changes: 1 addition & 3 deletions packages/less/src/less-browser/bootstrap.js
Expand Up @@ -3,8 +3,6 @@
* used in the browser distributed version of less
* to kick-start less using the browser api
*/
/* global window, document */

import defaultOptions from '../less/default-options';
import addDefaultOptions from './add-default-options';
import root from './index';
Expand All @@ -13,7 +11,7 @@ const options = defaultOptions();

if (window.less) {
for (const key in window.less) {
if (window.less.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(window.less, key)) {
options[key] = window.less[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/less/src/less-browser/cache.js
Expand Up @@ -29,7 +29,7 @@ export default (window, options, logger) => {
let vars = cache && cache.getItem(`${path}:vars`);

modifyVars = modifyVars || {};
vars = vars || "{}"; // if not set, treat as the JSON representation of an empty object
vars = vars || '{}'; // if not set, treat as the JSON representation of an empty object

if (timestamp && webInfo.lastModified &&
(new Date(webInfo.lastModified).valueOf() ===
Expand Down
4 changes: 2 additions & 2 deletions packages/less/src/less-browser/error-reporting.js
Expand Up @@ -11,7 +11,7 @@ export default (window, less, options) => {
let content;
const errors = [];
const filename = e.filename || rootHref;
const filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
const filenameNoPath = filename.match(/([^/]+(\?.*)?)$/)[1];

elem.id = id;
elem.className = 'less-error-message';
Expand Down Expand Up @@ -113,7 +113,7 @@ export default (window, less, options) => {
}
}

function removeErrorConsole(path) {
function removeErrorConsole() {
// no action
}

Expand Down
4 changes: 1 addition & 3 deletions packages/less/src/less-browser/file-manager.js
@@ -1,5 +1,3 @@
/* global window, XMLHttpRequest */

import AbstractFileManager from '../less/environment/abstract-file-manager.js';

let options;
Expand Down Expand Up @@ -66,7 +64,7 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {
fileCache = {};
},

loadFile(filename, currentDirectory, options, environment) {
loadFile(filename, currentDirectory, options) {
// TODO: Add prefix support like less-node?
// What about multiple paths?

Expand Down
10 changes: 7 additions & 3 deletions packages/less/src/less-browser/index.js
Expand Up @@ -39,7 +39,7 @@ export default (window, options) => {
function clone(obj) {
const cloned = {};
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
cloned[prop] = obj[prop];
}
}
Expand Down Expand Up @@ -159,6 +159,10 @@ export default (window, options) => {
less.watchTimer = setInterval(() => {
if (less.watchMode) {
fileManager.clearFileCache();
/**
* @todo remove when this is typed with JSDoc
*/
// eslint-disable-next-line no-unused-vars
loadStyleSheets((e, css, _, sheet, webInfo) => {
if (e) {
errors.add(e, e.href || sheet.href);
Expand All @@ -174,7 +178,7 @@ export default (window, options) => {
//
// Watch mode
//
less.watch = function () {
less.watch = function () {
if (!less.watchMode ) {
less.env = 'development';
initRunningMode();
Expand Down Expand Up @@ -205,7 +209,7 @@ export default (window, options) => {
// Asynchronously get all <link> tags with the 'rel' attribute set to
// "stylesheet/less", returning a Promise.
//
less.registerStylesheets = () => new Promise((resolve, reject) => {
less.registerStylesheets = () => new Promise((resolve) => {
less.registerStylesheetsImmediately();
resolve();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/less/src/less-browser/plugin-loader.js
@@ -1,6 +1,6 @@
// TODO: Add tests for browser @plugin
/* global window */

/**
* @todo Add tests for browser `@plugin`
*/
import AbstractPluginLoader from '../less/environment/abstract-plugin-loader.js';

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/less/src/less-browser/utils.js
@@ -1,17 +1,17 @@

export function extractId(href) {
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '') // Remove protocol & domain
.replace(/[\?\&]livereload=\w+/, '') // Remove LiveReload cachebuster
return href.replace(/^[a-z-]+:\/+?[^/]+/, '') // Remove protocol & domain
.replace(/[?&]livereload=\w+/, '') // Remove LiveReload cachebuster
.replace(/^\//, '') // Remove root /
.replace(/\.[a-zA-Z]+$/, '') // Remove simple extension
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
.replace(/[^.\w-]+/g, '-') // Replace illegal characters
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
}

export function addDataAttr(options, tag) {
if (!tag) {return;} // in case of tag is null or undefined
for (const opt in tag.dataset) {
if (tag.dataset.hasOwnProperty(opt)) {
if (Object.prototype.hasOwnProperty.call(tag.dataset, opt)) {
if (opt === 'env' || opt === 'dumpLineNumbers' || opt === 'rootpath' || opt === 'errorReporting') {
options[opt] = tag.dataset[opt];
} else {
Expand Down
37 changes: 18 additions & 19 deletions packages/less/src/less-node/file-manager.js
Expand Up @@ -61,6 +61,23 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {

function getFileData(fulfill, reject) {
(function tryPathIndex(i) {
function tryWithExtension() {
const extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;

if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
try {
fullFilename = require.resolve(extFilename);
isNodeModule = true;
}
catch (e) {
filenamesTried.push(npmPrefix + extFilename);
fullFilename = extFilename;
}
}
else {
fullFilename = extFilename;
}
}
if (i < paths.length) {
(function tryPrefix(j) {
if (j < prefixes.length) {
Expand All @@ -83,25 +100,7 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {
}
else {
tryWithExtension();
}

function tryWithExtension() {
const extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;

if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
try {
fullFilename = require.resolve(extFilename);
isNodeModule = true;
}
catch (e) {
filenamesTried.push(npmPrefix + extFilename);
fullFilename = extFilename;
}
}
else {
fullFilename = extFilename;
}
}
}

const readFileArgs = [fullFilename];
if (!options.rawBuffer) {
Expand Down
1 change: 1 addition & 0 deletions packages/less/src/less-node/lessc-helper.js
Expand Up @@ -88,4 +88,5 @@ const lessc_helper = {
};

// Exports helper functions
// eslint-disable-next-line no-prototype-builtins
for (const h in lessc_helper) { if (lessc_helper.hasOwnProperty(h)) { exports[h] = lessc_helper[h]; }}
5 changes: 5 additions & 0 deletions packages/less/src/less-node/url-file-manager.js
@@ -1,3 +1,8 @@
/* eslint-disable no-unused-vars */
/**
* @todo - remove top eslint rule when FileManagers have JSDoc type
* and are TS-type-checked
*/
const isUrlRe = /^(?:https?:)?\/\//i;
import url from 'url';
let request;
Expand Down
4 changes: 2 additions & 2 deletions packages/less/src/less/contexts.js
Expand Up @@ -6,7 +6,7 @@ const copyFromOriginal = function copyFromOriginal(original, destination, proper
if (!original) { return; }

for (let i = 0; i < propertiesToCopy.length; i++) {
if (original.hasOwnProperty(propertiesToCopy[i])) {
if (Object.prototype.hasOwnProperty.call(original, propertiesToCopy[i])) {
destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ contexts.Eval.prototype.pathRequiresRewrite = function (path) {
contexts.Eval.prototype.rewritePath = function (path, rootpath) {
let newPath;

rootpath = rootpath || '';
rootpath = rootpath || '';
newPath = this.normalizePath(rootpath + path);

// If a path was explicit relative and the rootpath was not an absolute path
Expand Down
12 changes: 9 additions & 3 deletions packages/less/src/less/environment/abstract-file-manager.js
Expand Up @@ -15,7 +15,7 @@ class AbstractFileManager {
}

tryAppendExtension(path, ext) {
return /(\.[a-z]*$)|([\?;].*)$/.test(path) ? path : path + ext;
return /(\.[a-z]*$)|([?;].*)$/.test(path) ? path : path + ext;
}

tryAppendLessExtension(path) {
Expand Down Expand Up @@ -71,15 +71,21 @@ class AbstractFileManager {
return diff;
}

// helper function, not part of API
/**
* Helper function, not part of API.
* This should be replaceable by newer Node / Browser APIs
*
* @param {string} url
* @param {string} baseUrl
*/
extractUrlParts(url, baseUrl) {
// urlParts[1] = protocol://hostname/ OR /
// urlParts[2] = / if path relative to host base
// urlParts[3] = directories
// urlParts[4] = filename
// urlParts[5] = parameters

const urlPartsRegex = /^((?:[a-z-]+:)?\/{2}(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/i;
const urlPartsRegex = /^((?:[a-z-]+:)?\/{2}(?:[^/?#]*\/)|([/\\]))?((?:[^/\\?#]*[/\\])*)([^/\\?#]*)([#?].*)?$/i;

const urlParts = url.match(urlPartsRegex);
const returner = {};
Expand Down

0 comments on commit 5e64d5b

Please sign in to comment.