Skip to content

Commit 4d3189c

Browse files
authored
Fixes #3787 -- ESLint updates and linting cleanup (#3790)
* ESLint updates and linting cleanup * Exclude test files from some linting rules for now * Fix some TypeScript and build issues
1 parent a917965 commit 4d3189c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+449
-442
lines changed

Diff for: packages/less/.eslintrc.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = {
2+
'parser': '@typescript-eslint/parser',
3+
'extends': 'eslint:recommended',
4+
'parserOptions': {
5+
'ecmaVersion': 2018,
6+
'sourceType': 'module'
7+
},
8+
'plugins': ['@typescript-eslint'],
9+
'env': {
10+
'browser': true,
11+
'node': true,
12+
'mocha': true
13+
},
14+
'globals': {},
15+
'rules': {
16+
indent: ['error', 4, {
17+
SwitchCase: 1
18+
}],
19+
'no-empty': ['error', { 'allowEmptyCatch': true }],
20+
quotes: ['error', 'single', {
21+
avoidEscape: true
22+
}],
23+
/**
24+
* The codebase uses some while(true) statements.
25+
* Refactor to remove this rule.
26+
*/
27+
'no-constant-condition': 0,
28+
/**
29+
* Less combines assignments with conditionals sometimes
30+
*/
31+
'no-cond-assign': 0,
32+
/**
33+
* @todo - remove when some kind of code style (XO?) is added
34+
*/
35+
'no-multiple-empty-lines': 'error'
36+
},
37+
'overrides': [
38+
{
39+
files: ['*.ts'],
40+
extends: ['plugin:@typescript-eslint/recommended'],
41+
rules: {
42+
/**
43+
* Suppress until Less has better-defined types
44+
* @see https://github.com/less/less.js/discussions/3786
45+
*/
46+
'@typescript-eslint/no-explicit-any': 0
47+
}
48+
},
49+
{
50+
files: ['test/**/*.{js,ts}', 'benchmark/index.js'],
51+
/**
52+
* @todo - fix later
53+
*/
54+
rules: {
55+
'no-undef': 0,
56+
'no-useless-escape': 0,
57+
'no-unused-vars': 0,
58+
'no-redeclare': 0,
59+
'@typescript-eslint/no-unused-vars': 0
60+
}
61+
},
62+
]
63+
}

Diff for: packages/less/.eslintrc.json

-71
This file was deleted.

Diff for: packages/less/Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ module.exports = function(grunt) {
283283
"!test/less/errors/plugin/plugin-error.js"
284284
],
285285
options: {
286-
configFile: ".eslintrc.json",
286+
configFile: ".eslintrc.js",
287287
fix: true
288288
}
289289
},

Diff for: packages/less/benchmark/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var path = require('path'),
22
fs = require('fs'),
3-
now = require("performance-now");
3+
now = require('performance-now');
44

55
var less = require('../.');
66
var file = path.join(__dirname, 'benchmark.less');
@@ -10,12 +10,12 @@ if (process.argv[2]) { file = path.join(process.cwd(), process.argv[2]) }
1010
fs.readFile(file, 'utf8', function (e, data) {
1111
var start, total;
1212

13-
console.log("Benchmarking...\n", path.basename(file) + " (" +
14-
parseInt(data.length / 1024) + " KB)", "");
13+
console.log('Benchmarking...\n', path.basename(file) + ' (' +
14+
parseInt(data.length / 1024) + ' KB)', '');
1515

1616
var renderBenchmark = []
17-
, parserBenchmark = []
18-
, evalBenchmark = [];
17+
, parserBenchmark = []
18+
, evalBenchmark = [];
1919

2020
var totalruns = 30;
2121
var ignoreruns = 5;
@@ -74,13 +74,13 @@ fs.readFile(file, 'utf8', function (e, data) {
7474
var variation = maxtime - mintime;
7575
var variationperc = (variation / avgtime) * 100;
7676

77-
console.log("Min. Time: " + Math.round(mintime) + " ms");
78-
console.log("Max. Time: " + Math.round(maxtime) + " ms");
79-
console.log("Total Average Time: " + Math.round(avgtime) + " ms (" +
77+
console.log('Min. Time: ' + Math.round(mintime) + ' ms');
78+
console.log('Max. Time: ' + Math.round(maxtime) + ' ms');
79+
console.log('Total Average Time: ' + Math.round(avgtime) + ' ms (' +
8080
parseInt(1000 / avgtime *
81-
data.length / 1024) + " KB\/s)");
82-
console.log("+/- " + Math.round(variationperc) + "%");
83-
console.log("");
81+
data.length / 1024) + ' KB\/s)');
82+
console.log('+/- ' + Math.round(variationperc) + '%');
83+
console.log('');
8484
}
8585

8686
analyze('Parsing', parserBenchmark);

Diff for: packages/less/build/rollup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async function buildBrowser() {
4747
include: [/^.+\.min\.js$/],
4848
output: {
4949
comments: function(node, comment) {
50-
if (comment.type == "comment2") {
50+
if (comment.type == 'comment2') {
5151
// preserve banner
5252
return /@license/i.test(comment.value);
5353
}

Diff for: packages/less/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
"scripts": {
3838
"test": "grunt test",
3939
"grunt": "grunt",
40+
"lint": "eslint '**/*.{ts,js}'",
41+
"lint:fix": "eslint '**/*.{ts,js}' --fix",
4042
"build": "npm-run-all clean compile",
4143
"clean": "shx rm -rf ./lib tsconfig.tsbuildinfo",
42-
"compile": "tsc -p tsconfig.json",
44+
"compile": "tsc -p tsconfig.build.json",
4345
"copy:root": "shx cp -rf ./dist ../../",
44-
"dev": "tsc -p tsconfig.json -w",
46+
"dev": "tsc -p tsconfig.build.json -w",
4547
"prepublishOnly": "grunt dist"
4648
},
4749
"optionalDependencies": {

Diff for: packages/less/src/less-browser/bootstrap.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* used in the browser distributed version of less
44
* to kick-start less using the browser api
55
*/
6-
/* global window, document */
7-
86
import defaultOptions from '../less/default-options';
97
import addDefaultOptions from './add-default-options';
108
import root from './index';
@@ -13,7 +11,7 @@ const options = defaultOptions();
1311

1412
if (window.less) {
1513
for (const key in window.less) {
16-
if (window.less.hasOwnProperty(key)) {
14+
if (Object.prototype.hasOwnProperty.call(window.less, key)) {
1715
options[key] = window.less[key];
1816
}
1917
}

Diff for: packages/less/src/less-browser/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default (window, options, logger) => {
2929
let vars = cache && cache.getItem(`${path}:vars`);
3030

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

3434
if (timestamp && webInfo.lastModified &&
3535
(new Date(webInfo.lastModified).valueOf() ===

Diff for: packages/less/src/less-browser/error-reporting.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default (window, less, options) => {
1111
let content;
1212
const errors = [];
1313
const filename = e.filename || rootHref;
14-
const filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
14+
const filenameNoPath = filename.match(/([^/]+(\?.*)?)$/)[1];
1515

1616
elem.id = id;
1717
elem.className = 'less-error-message';
@@ -113,7 +113,7 @@ export default (window, less, options) => {
113113
}
114114
}
115115

116-
function removeErrorConsole(path) {
116+
function removeErrorConsole() {
117117
// no action
118118
}
119119

Diff for: packages/less/src/less-browser/file-manager.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* global window, XMLHttpRequest */
2-
31
import AbstractFileManager from '../less/environment/abstract-file-manager.js';
42

53
let options;
@@ -66,7 +64,7 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {
6664
fileCache = {};
6765
},
6866

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

Diff for: packages/less/src/less-browser/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default (window, options) => {
3939
function clone(obj) {
4040
const cloned = {};
4141
for (const prop in obj) {
42-
if (obj.hasOwnProperty(prop)) {
42+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
4343
cloned[prop] = obj[prop];
4444
}
4545
}
@@ -159,6 +159,10 @@ export default (window, options) => {
159159
less.watchTimer = setInterval(() => {
160160
if (less.watchMode) {
161161
fileManager.clearFileCache();
162+
/**
163+
* @todo remove when this is typed with JSDoc
164+
*/
165+
// eslint-disable-next-line no-unused-vars
162166
loadStyleSheets((e, css, _, sheet, webInfo) => {
163167
if (e) {
164168
errors.add(e, e.href || sheet.href);
@@ -174,7 +178,7 @@ export default (window, options) => {
174178
//
175179
// Watch mode
176180
//
177-
less.watch = function () {
181+
less.watch = function () {
178182
if (!less.watchMode ) {
179183
less.env = 'development';
180184
initRunningMode();
@@ -205,7 +209,7 @@ export default (window, options) => {
205209
// Asynchronously get all <link> tags with the 'rel' attribute set to
206210
// "stylesheet/less", returning a Promise.
207211
//
208-
less.registerStylesheets = () => new Promise((resolve, reject) => {
212+
less.registerStylesheets = () => new Promise((resolve) => {
209213
less.registerStylesheetsImmediately();
210214
resolve();
211215
});

Diff for: packages/less/src/less-browser/plugin-loader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// TODO: Add tests for browser @plugin
2-
/* global window */
3-
1+
/**
2+
* @todo Add tests for browser `@plugin`
3+
*/
44
import AbstractPluginLoader from '../less/environment/abstract-plugin-loader.js';
55

66
/**

Diff for: packages/less/src/less-browser/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

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

1111
export function addDataAttr(options, tag) {
1212
if (!tag) {return;} // in case of tag is null or undefined
1313
for (const opt in tag.dataset) {
14-
if (tag.dataset.hasOwnProperty(opt)) {
14+
if (Object.prototype.hasOwnProperty.call(tag.dataset, opt)) {
1515
if (opt === 'env' || opt === 'dumpLineNumbers' || opt === 'rootpath' || opt === 'errorReporting') {
1616
options[opt] = tag.dataset[opt];
1717
} else {

Diff for: packages/less/src/less-node/file-manager.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {
6161

6262
function getFileData(fulfill, reject) {
6363
(function tryPathIndex(i) {
64+
function tryWithExtension() {
65+
const extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;
66+
67+
if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
68+
try {
69+
fullFilename = require.resolve(extFilename);
70+
isNodeModule = true;
71+
}
72+
catch (e) {
73+
filenamesTried.push(npmPrefix + extFilename);
74+
fullFilename = extFilename;
75+
}
76+
}
77+
else {
78+
fullFilename = extFilename;
79+
}
80+
}
6481
if (i < paths.length) {
6582
(function tryPrefix(j) {
6683
if (j < prefixes.length) {
@@ -83,25 +100,7 @@ FileManager.prototype = Object.assign(new AbstractFileManager(), {
83100
}
84101
else {
85102
tryWithExtension();
86-
}
87-
88-
function tryWithExtension() {
89-
const extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;
90-
91-
if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
92-
try {
93-
fullFilename = require.resolve(extFilename);
94-
isNodeModule = true;
95-
}
96-
catch (e) {
97-
filenamesTried.push(npmPrefix + extFilename);
98-
fullFilename = extFilename;
99-
}
100-
}
101-
else {
102-
fullFilename = extFilename;
103-
}
104-
}
103+
}
105104

106105
const readFileArgs = [fullFilename];
107106
if (!options.rawBuffer) {

0 commit comments

Comments
 (0)