<commit_before>const path = require('path')
const babylon = require('babylon')
const traverse = require('babel-traverse').default
const isStyled = require('./utils/styled').isStyled
const isHelper = require('./utils/styled').isHelper
const isStyledImport = require('./utils/styled').isStyledImport

const wrapSelector = require('./utils/general').wrapSelector
const wrapKeyframes = require('./utils/general').wrapKeyframes
const fixIndentation = require('./utils/general').fixIndentation

const getTTLContent = require('./utils/tagged-template-literal.js').getTaggedTemplateLiteralContent
const parseImports = require('./utils/parse').parseImports
const getSourceMap = require('./utils/parse').getSourceMap

// TODO Fix ampersand in selectors
// TODO ENFORCE THESE RULES
// value-no-vendor-prefix – don't allow vendor prefixes
// property-no-vendor-prefix – don't allow vendor prefixes

const ignoredRules = [
  // Don't throw if there's no styled-components in a file
  'no-empty-source',
  // We don't care about end-of-source newlines, users cannot control them
  'no-missing-end-of-source-newline',
]

const sourceMapsCorrections = {}

module.exports = (/* options */) => ({
  // Get string for stylelint to lint
  code(input, filepath) {
    const absolutePath = path.resolve(process.cwd(), filepath)
    sourceMapsCorrections[absolutePath] = {}
    const ast = babylon.parse(input, {
      sourceType: 'module',
      plugins: [
        'jsx',
        'flow',
        'objectRestSpread',
        'decorators',
        'classProperties',
        'exportExtensions',
        'asyncGenerators',
        'functionBind',
        'functionSent',
        'dynamicImport',
      ],
    })

    const extractedCSS = []
    let importedNames = {
      default: 'styled',
      css: 'css',
      keyframes: 'keyframes',
      injectGlobal: 'injectGlobal',
    }
    traverse(ast, {
      enter({ node }) {
        if (isStyledImport(node)) {
          importedNames = parseImports(node)
          return
        }

        const helper = isHelper(node, importedNames)

        if (!helper && !isStyled(node, importedNames.default)) return

        const content = getTTLContent(node)
        const fixedContent = fixIndentation(content).text
        const wrapperFn = helper === 'keyframes' ? wrapKeyframes : wrapSelector
        const wrappedContent = wrapperFn(fixedContent)
        extractedCSS.push(wrappedContent)
        // Save source location, merging existing corrections with current corrections
        sourceMapsCorrections[absolutePath] = Object.assign(
          sourceMapsCorrections[absolutePath],
          getSourceMap(extractedCSS.join('\n'), wrappedContent, node.loc.start.line)
        )
      },
    })

    return extractedCSS.join('\n')
  },
  // Fix sourcemaps
  result(stylelintResult, filepath) {
    const lineCorrection = sourceMapsCorrections[filepath]
    const newWarnings = stylelintResult.warnings.reduce((prevWarnings, warning) => {
      if (ignoredRules.includes(warning.rule)) return prevWarnings
      const correctedWarning = Object.assign(warning, {
        // Replace "brace" with "backtick" in warnings, e.g.
        // "Unexpected empty line before closing backtick" (instead of "brace")
        text: warning.text.replace(/brace/, 'backtick'),
        line: lineCorrection[warning.line],
      })
      prevWarnings.push(correctedWarning)
      return prevWarnings
    }, [])

    return Object.assign(stylelintResult, { warnings: newWarnings })
  },
})<commit_msg>Fixed: set `errored` to `false` if `warning` no contains errors.<commit_after>const path = require('path')
const babylon = require('babylon')
const traverse = require('babel-traverse').default
const isStyled = require('./utils/styled').isStyled
const isHelper = require('./utils/styled').isHelper
const isStyledImport = require('./utils/styled').isStyledImport

const wrapSelector = require('./utils/general').wrapSelector
const wrapKeyframes = require('./utils/general').wrapKeyframes
const fixIndentation = require('./utils/general').fixIndentation

const getTTLContent = require('./utils/tagged-template-literal.js').getTaggedTemplateLiteralContent
const parseImports = require('./utils/parse').parseImports
const getSourceMap = require('./utils/parse').getSourceMap

// TODO Fix ampersand in selectors
// TODO ENFORCE THESE RULES
// value-no-vendor-prefix – don't allow vendor prefixes
// property-no-vendor-prefix – don't allow vendor prefixes

const ignoredRules = [
  // Don't throw if there's no styled-components in a file
  'no-empty-source',
  // We don't care about end-of-source newlines, users cannot control them
  'no-missing-end-of-source-newline',
]

const sourceMapsCorrections = {}

module.exports = (/* options */) => ({
  // Get string for stylelint to lint
  code(input, filepath) {
    const absolutePath = path.resolve(process.cwd(), filepath)
    sourceMapsCorrections[absolutePath] = {}
    const ast = babylon.parse(input, {
      sourceType: 'module',
      plugins: [
        'jsx',
        'flow',
        'objectRestSpread',
        'decorators',
        'classProperties',
        'exportExtensions',
        'asyncGenerators',
        'functionBind',
        'functionSent',
        'dynamicImport',
      ],
    })

    const extractedCSS = []
    let importedNames = {
      default: 'styled',
      css: 'css',
      keyframes: 'keyframes',
      injectGlobal: 'injectGlobal',
    }
    traverse(ast, {
      enter({ node }) {
        if (isStyledImport(node)) {
          importedNames = parseImports(node)
          return
        }

        const helper = isHelper(node, importedNames)

        if (!helper && !isStyled(node, importedNames.default)) return

        const content = getTTLContent(node)
        const fixedContent = fixIndentation(content).text
        const wrapperFn = helper === 'keyframes' ? wrapKeyframes : wrapSelector
        const wrappedContent = wrapperFn(fixedContent)
        extractedCSS.push(wrappedContent)
        // Save source location, merging existing corrections with current corrections
        sourceMapsCorrections[absolutePath] = Object.assign(
          sourceMapsCorrections[absolutePath],
          getSourceMap(extractedCSS.join('\n'), wrappedContent, node.loc.start.line)
        )
      },
    })

    return extractedCSS.join('\n')
  },
  // Fix sourcemaps
  result(stylelintResult, filepath) {
    const lineCorrection = sourceMapsCorrections[filepath]
    const newWarnings = stylelintResult.warnings.reduce((prevWarnings, warning) => {
      if (ignoredRules.includes(warning.rule)) return prevWarnings
      const correctedWarning = Object.assign(warning, {
        // Replace "brace" with "backtick" in warnings, e.g.
        // "Unexpected empty line before closing backtick" (instead of "brace")
        text: warning.text.replace(/brace/, 'backtick'),
        line: lineCorrection[warning.line],
      })
      prevWarnings.push(correctedWarning)
      return prevWarnings
    }, [])
    
    if (newWarnings.length === 0) {
      stylelintResult.errored = false
    }

    return Object.assign(stylelintResult, { warnings: newWarnings })
  },
})
{'fraction_non_alphanumeric': 0.12090680100755667, 'fraction_numerical': 0.0001399384270920795, 'mean_word_length': 3.196711685261304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4280876', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1983, 'n_words': 458}