How to use scss-parser - 6 common examples

To help you get started, we’ve selected a few scss-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github qodesmith / purgecss-whitelister / index.js View on Github external
const deepArray = filenames.reduce((acc, filename) => {
    // Do nothing for non-style files.
    const ext = filename.split('.').pop()
    if (!exts.includes(ext)) return acc

    // File contents.
    const fileContents = readFileSync(filename, 'utf-8')

    // 1st try `scss-parser`.
    try {
      const parsedData = parse(fileContents).value
      const selectors = parseStyleAST(parsedData)
      return acc.concat(selectors)

    // 2nd try `gonzales-pe`.
    } catch(e) {
      const parsed = parse2(fileContents, { syntax: ext })
      const nodes = []

      // Built-in traversal method, no need to recursively
      // traverse the tree, cherry pick, and flatten the results!
      parsed.traverse(node => {
        if (shouldKeep2.includes(node.type)) {
          const thing = node.content.find(({ type }) => type === 'ident')
          if (thing) nodes.push(thing.content)
        }
      })
github learningequality / kolibri / packages / kolibri-tools / lib / apiSpecExportTools.js View on Github external
function parseScssDependencies(sourceContents, destinationFolder, sourceFolder) {
    const sourceTree = scssParser.parse(sourceContents);
    const scssWrapper = createQueryWrapper(sourceTree);
    const importNodes = scssWrapper('atrule')
      .has(wrapper => wrapper.node.value === 'import')
      .children('string_single').nodes;
    importNodes.forEach(node => {
      // This should be the path for the import statement
      const importPath = node.node.value;
      // Prefix any resolved files with an underscore as they are not part of the core spec
      const replacePath = resolveDependenciesAndCopy({
        sourcePath: importPath,
        destinationFolder,
        sourceFolder,
        prefix: '_',
      });
      sourceContents.replace(importPath, replacePath);
    });
github tree-sitter / tree-sitter-cli / lib / api / properties.js View on Github external
function parseProperties(source, baseDirectory) {
  // Get the raw SCSS concrete syntax tree.
  const rawTree = parseCSS(source);
  removeWhitespace(rawTree);

  // Convert the concrete syntax tree into a more convenient AST.
  let schema
  const rootRules = []
  for (const rule of rawTree.value) {
    if (rule.type === 'atrule') {
      removeWhitespace(rule);
      const [keyword, argument] = rule.value;
      if (keyword.value === 'schema') {
        schema = visitSchema(argument, baseDirectory)
      } else if (keyword.value === 'import') {
        rootRules.push(...visitImport(argument, baseDirectory))
      }
    } else {
      rootRules.push(visitRule(rule, false))
github M6Web / bemlinter / src / lint.js View on Github external
function bemLintFileData(filePath, data, result, blockList, options) {
  const fileOptions = options.getFileOptions(filePath);
  const bem = createBem(fileOptions);
  const moduleName = fileOptions.name;
  const blockName = bem.getBlockNameFromFile(filePath);
  const isIsolatedBlock = getIsIsolatedBlock(fileOptions, blockName);
  if (isIsolatedBlock) {
    result.addBlock(moduleName, blockName);
  }
  const ast = parse(data);
  const $ = createQueryAst(ast);

  // Checker
  function checkInternalClassName() {
    eachClassName($, (className, wrapper) => {
      if (!bem.isBlockName(className, blockName)) {
        if (bem.isClassPrefixMissing(className, blockName)) {
          result.addError(`".${className}" should have a block prefix, ".${bem.getBaseClassFromBlockName(blockName)}" expected.`, filePath, moduleName, blockName, wrapper);
        } else if (isClassFollowedByAPseudoClass($(wrapper))) {
          result.addWarning(`".${className}" is only tolerated in this stylesheet.`, filePath, moduleName, blockName, wrapper);
        } else {
          result.addError(`".${className}" is incoherent with the file name, ".${bem.getBaseClassFromBlockName(blockName)}" expected.`, filePath, moduleName, blockName, wrapper);
        }
      }
    });
  }
github jgranstrom / sass-extract / src / parse.js View on Github external
export function parseDeclarations(data) {
  const ast = parse(data);
  const $ast = createQueryWrapper(ast);

  const implicitGlobalDeclarations = $ast('declaration').hasParent('stylesheet');
  const explicitGlobalDeclarations = $ast('declaration').hasParent('block')
  .filter(node => isExplicitGlobalDeclaration($ast, node));

  let implicitGlobals = implicitGlobalDeclarations.map(declaration => parseDeclaration($ast, declaration, SCOPE_IMPICIT));
  let explicitGlobals = explicitGlobalDeclarations.map(declaration => parseDeclaration($ast, declaration, SCOPE_EXPLICIT));  

  return { explicitGlobals, implicitGlobals };
}
github jgranstrom / sass-extract / src / parse.js View on Github external
function parseExpression($ast, declaration) {
  let flagsReached = false;
  
  return stringify($ast(declaration)
  .children('value')
  .get(0))
  .trim();
}

scss-parser

A library to parse/stringify SCSS

BSD-3-Clause
Latest version published 2 years ago

Package Health Score

58 / 100
Full package analysis

Popular scss-parser functions