Skip to content

Commit 7491578

Browse files
broofaedhgoose
andauthored
feat: add disablePluginRule flag for render() options (#3701)
Co-authored-by: Edward Hartwell Goose <[email protected]>
1 parent 1eafc06 commit 7491578

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

packages/less/bin/lessc

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env node
2+
3+
/* eslint indent: [2, 2, {"SwitchCase": 1}] */
4+
25
"use strict";
36

47
var path = require('path');
@@ -87,6 +90,12 @@ function render() {
8790
output = path.resolve(process.cwd(), output);
8891
}
8992

93+
if (options.disablePluginRule && queuePlugins.length > 0) {
94+
console.error('--plugin and --disable-plugin-rule may not be used at the same time');
95+
process.exitCode = 1;
96+
return;
97+
}
98+
9099
if (options.sourceMap) {
91100
sourceMapOptions.sourceMapInputFilename = input;
92101

@@ -113,6 +122,7 @@ function render() {
113122
var mapDir = path.dirname(mapFilename);
114123
var outputDir = path.dirname(output); // find the path from the map to the output file
115124

125+
// eslint-disable-next-line max-len
116126
sourceMapOptions.sourceMapOutputFilename = path.join(path.relative(mapDir, outputDir), path.basename(output)); // make the sourcemap filename point to the sourcemap relative to the css file output directory
117127

118128
sourceMapOptions.sourceMapFilename = path.join(path.relative(outputDir, mapDir), path.basename(sourceMapOptions.sourceMapFullFilename));
@@ -180,8 +190,8 @@ function render() {
180190
var filename = sourceMapOptions.sourceMapFullFilename;
181191
ensureDirectory(filename);
182192

183-
//To fix https://github.com/less/less.js/issues/3646
184-
output=output.toString();
193+
// To fix https://github.com/less/less.js/issues/3646
194+
output = output.toString();
185195

186196
fs.writeFile(filename, output, 'utf8', function (err) {
187197
if (err) {
@@ -444,7 +454,8 @@ function processPluginQueue() {
444454
break;
445455

446456
case 'no-js':
447-
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' + 'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
457+
// eslint-disable-next-line max-len
458+
console.error('The "--no-js" argument is deprecated, as inline JavaScript is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
448459
break;
449460

450461
case 'include-path':
@@ -629,6 +640,10 @@ function processPluginQueue() {
629640
});
630641
break;
631642

643+
case 'disable-plugin-rule':
644+
options.disablePluginRule = true;
645+
break;
646+
632647
default:
633648
queuePlugins.push({
634649
name: arg,

packages/less/src/less-browser/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function extractId(href) {
99
}
1010

1111
export function addDataAttr(options, tag) {
12-
if(!tag) return; // in case of tag is null or undefined
12+
if (!tag) {return;} // in case of tag is null or undefined
1313
for (const opt in tag.dataset) {
1414
if (tag.dataset.hasOwnProperty(opt)) {
1515
if (opt === 'env' || opt === 'dumpLineNumbers' || opt === 'rootpath' || opt === 'errorReporting') {

packages/less/src/less-node/lessc-helper.js

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const lessc_helper = {
6767
console.log(' --plugin=less-plugin-clean-css or just --clean-css');
6868
console.log(' specify options afterwards e.g. --plugin=less-plugin-clean-css="advanced"');
6969
console.log(' or --clean-css="advanced"');
70+
console.log(' --disable-plugin-rule Disallow @plugin statements');
7071
console.log('');
7172
console.log('-------------------------- Deprecated ----------------');
7273
console.log(' -sm=on|off Legacy parens-only math. Use --math');

packages/less/src/less/parser/parser.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,22 @@ const Parser = function Parser(context, imports, fileInfo) {
149149
//
150150
parse: function (str, callback, additionalData) {
151151
let root;
152-
let error = null;
152+
let err = null;
153153
let globalVars;
154154
let modifyVars;
155155
let ignored;
156156
let preText = '';
157157

158+
// Optionally disable @plugin parsing
159+
if (additionalData && additionalData.disablePluginRule) {
160+
parsers.plugin = function() {
161+
var dir = parserInput.$re(/^@plugin?\s+/);
162+
if (dir) {
163+
error('@plugin statements are not allowed when disablePluginRule is set to true');
164+
}
165+
}
166+
};
167+
158168
globalVars = (additionalData && additionalData.globalVars) ? `${Parser.serializeVars(additionalData.globalVars)}\n` : '';
159169
modifyVars = (additionalData && additionalData.modifyVars) ? `\n${Parser.serializeVars(additionalData.modifyVars)}` : '';
160170

@@ -226,7 +236,7 @@ const Parser = function Parser(context, imports, fileInfo) {
226236
}
227237
}
228238

229-
error = new LessError({
239+
err = new LessError({
230240
type: 'Parse',
231241
message,
232242
index: endInfo.furthest,
@@ -235,7 +245,7 @@ const Parser = function Parser(context, imports, fileInfo) {
235245
}
236246

237247
const finish = e => {
238-
e = error || e || imports.error;
248+
e = err || e || imports.error;
239249

240250
if (e) {
241251
if (!(e instanceof LessError)) {

packages/less/test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ lessTester.testSyncronous({syncImport: true}, '_main/import');
8888
lessTester.testSyncronous({syncImport: true}, '_main/plugin');
8989
lessTester.testSyncronous({syncImport: true}, 'math/strict/css');
9090
lessTester.testNoOptions();
91+
lessTester.testDisablePluginRule();
9192
lessTester.testJSImport();
9293
lessTester.finished();
9394

packages/less/test/less-test.js

+18
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,23 @@ module.exports = function() {
563563
};
564564
}
565565

566+
function testDisablePluginRule() {
567+
less.render(
568+
'@plugin "../../plugin/some_plugin";',
569+
{disablePluginRule: true},
570+
function(err) {
571+
// TODO: Need a better way of identifing exactly which error is thrown. Checking
572+
// text like this tends to be rather brittle.
573+
const EXPECTED = '@plugin statements are not allowed when disablePluginRule is set to true';
574+
if (!err || String(err).indexOf(EXPECTED) < 0) {
575+
fail('ERROR: Expected "' + EXPECTED + '" error');
576+
return;
577+
}
578+
ok(stylize('OK\n', 'green'));
579+
}
580+
);
581+
}
582+
566583
return {
567584
runTestSet: runTestSet,
568585
runTestSetNormalOnly: runTestSetNormalOnly,
@@ -575,6 +592,7 @@ module.exports = function() {
575592
testImportRedirect: testImportRedirect,
576593
testEmptySourcemap: testEmptySourcemap,
577594
testNoOptions: testNoOptions,
595+
testDisablePluginRule: testDisablePluginRule,
578596
testJSImport: testJSImport,
579597
finished: finished
580598
};

0 commit comments

Comments
 (0)