Skip to content

Commit

Permalink
Split Less and Sass into two separate extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 14, 2021
1 parent b3ef01f commit e3c7c42
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import isStandardSyntaxDeclaration from 'stylelint/lib/utils/isStandardSyntaxDec
import isStandardSyntaxProperty from 'stylelint/lib/utils/isStandardSyntaxProperty.js';
import isStandardSyntaxValue from 'stylelint/lib/utils/isStandardSyntaxValue.js';
import { fork } from 'css-tree';
import syntaxExtensions from './syntax-extension/index.js';
import { less, sass } from './syntax-extension/index.js';

const { utils, createPlugin } = stylelint;
const csstree = fork(syntaxExtensions);
const csstree = fork(less).fork(sass);
const isRegExp = value => toString.call(value) === '[object RegExp]';
const getRaw = (node, name) => (node.raws && node.raws[name]) || '';

Expand Down
48 changes: 38 additions & 10 deletions lib/syntax-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ class PreprocessorExtensionError {
}
}

export default function extendParser(syntaxConfig) {
export function less(syntaxConfig) {
// new node types
syntaxConfig.node.LessVariableReference = LessVariableReference;
syntaxConfig.node.LessVariable = LessVariable;
syntaxConfig.node.LessEscaping = LessEscaping;
syntaxConfig.node.LessNamespace = LessNamespace;
syntaxConfig.node.SassVariable = SassVariable;
syntaxConfig.node.SassInterpolation = SassInterpolation;
syntaxConfig.node.SassNamespace = SassNamespace;

// extend parser value parser
const originalGetNode = syntaxConfig.scope.Value.getNode;
Expand All @@ -42,12 +39,6 @@ export default function extendParser(syntaxConfig) {
node = this.LessVariable();
break;

case TYPE.Ident:
if (this.isDelim(FULLSTOP, 1)) {
node = this.SassNamespace();
}
break;

case TYPE.Hash: {
let sc = 0;
let tokenType = 0;
Expand Down Expand Up @@ -80,6 +71,43 @@ export default function extendParser(syntaxConfig) {
node = this.LessEscaping();
break;


}

break;
}

// currently we can't validate values that contain less/sass extensions
if (node !== null) {
throw new PreprocessorExtensionError();
}

return originalGetNode.call(this, context);
};

return syntaxConfig;
}

export function sass(syntaxConfig) {
// new node types
syntaxConfig.node.SassVariable = SassVariable;
syntaxConfig.node.SassInterpolation = SassInterpolation;
syntaxConfig.node.SassNamespace = SassNamespace;

// extend parser value parser
const originalGetNode = syntaxConfig.scope.Value.getNode;
syntaxConfig.scope.Value.getNode = function(context) {
let node = null;

switch (this.tokenType) {
case TYPE.Ident:
if (this.isDelim(FULLSTOP, 1)) {
node = this.SassNamespace();
}
break;

case TYPE.Delim:
switch (this.source.charCodeAt(this.tokenStart)) {
case DOLLARSIGN: // sass: $var
node = this.SassVariable();
break;
Expand Down

0 comments on commit e3c7c42

Please sign in to comment.