Skip to content

Commit

Permalink
Update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Aug 14, 2023
1 parent 011024f commit 5453320
Show file tree
Hide file tree
Showing 6 changed files with 532 additions and 298 deletions.
130 changes: 65 additions & 65 deletions lib/scss-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,71 @@ let NestedDeclaration = require('./nested-declaration')
let scssTokenizer = require('./scss-tokenize')

class ScssParser extends Parser {
atrule(token) {
let name = token[1]
let prev = token
while (!this.tokenizer.endOfFile()) {
let next = this.tokenizer.nextToken()
if (next[0] === 'word' && next[2] === prev[3] + 1) {
name += next[1]
prev = next
} else {
this.tokenizer.back(next)
break
}
}

super.atrule(['at-word', name, token[2], prev[3]])
}

comment(token) {
if (token[4] === 'inline') {
let node = new Comment()
this.init(node, token[2])
node.raws.inline = true
let pos = this.input.fromOffset(token[3])
node.source.end = { column: pos.col, line: pos.line, offset: token[3] }

let text = token[1].slice(2)
if (/^\s*$/.test(text)) {
node.text = ''
node.raws.left = text
node.raws.right = ''
} else {
let match = text.match(/^(\s*)([^]*\S)(\s*)$/)
let fixed = match[2].replace(/(\*\/|\/\*)/g, '*//*')
node.text = fixed
node.raws.left = match[1]
node.raws.right = match[3]
node.raws.text = match[2]
}
} else {
super.comment(token)
}
}

createTokenizer() {
this.tokenizer = scssTokenizer(this.input)
}

raw(node, prop, tokens, customProperty) {
super.raw(node, prop, tokens, customProperty)
if (node.raws[prop]) {
let scss = node.raws[prop].raw
node.raws[prop].raw = tokens.reduce((all, i) => {
if (i[0] === 'comment' && i[4] === 'inline') {
let text = i[1].slice(2).replace(/(\*\/|\/\*)/g, '*//*')
return all + '/*' + text + '*/'
} else {
return all + i[1]
}
}, '')
if (scss !== node.raws[prop].raw) {
node.raws[prop].scss = scss
}
}
}

rule(tokens) {
let withColon = false
let brackets = 0
Expand Down Expand Up @@ -45,10 +106,10 @@ class ScssParser extends Parser {
}
if (last[3]) {
let pos = this.input.fromOffset(last[3])
node.source.end = { offset: last[3], line: pos.line, column: pos.col }
node.source.end = { column: pos.col, line: pos.line, offset: last[3] }
} else {
let pos = this.input.fromOffset(last[2])
node.source.end = { offset: last[2], line: pos.line, column: pos.col }
node.source.end = { column: pos.col, line: pos.line, offset: last[2] }
}

while (tokens[0][0] !== 'word') {
Expand All @@ -58,9 +119,9 @@ class ScssParser extends Parser {
if (tokens[0][2]) {
let pos = this.input.fromOffset(tokens[0][2])
node.source.start = {
offset: tokens[0][2],
column: pos.col,
line: pos.line,
column: pos.col
offset: tokens[0][2]
}
}

Expand Down Expand Up @@ -135,67 +196,6 @@ class ScssParser extends Parser {
this.current = node
}
}

comment(token) {
if (token[4] === 'inline') {
let node = new Comment()
this.init(node, token[2])
node.raws.inline = true
let pos = this.input.fromOffset(token[3])
node.source.end = { offset: token[3], line: pos.line, column: pos.col }

let text = token[1].slice(2)
if (/^\s*$/.test(text)) {
node.text = ''
node.raws.left = text
node.raws.right = ''
} else {
let match = text.match(/^(\s*)([^]*\S)(\s*)$/)
let fixed = match[2].replace(/(\*\/|\/\*)/g, '*//*')
node.text = fixed
node.raws.left = match[1]
node.raws.right = match[3]
node.raws.text = match[2]
}
} else {
super.comment(token)
}
}

atrule(token) {
let name = token[1]
let prev = token
while (!this.tokenizer.endOfFile()) {
let next = this.tokenizer.nextToken()
if (next[0] === 'word' && next[2] === prev[3] + 1) {
name += next[1]
prev = next
} else {
this.tokenizer.back(next)
break
}
}

super.atrule(['at-word', name, token[2], prev[3]])
}

raw(node, prop, tokens, customProperty) {
super.raw(node, prop, tokens, customProperty)
if (node.raws[prop]) {
let scss = node.raws[prop].raw
node.raws[prop].raw = tokens.reduce((all, i) => {
if (i[0] === 'comment' && i[4] === 'inline') {
let text = i[1].slice(2).replace(/(\*\/|\/\*)/g, '*//*')
return all + '/*' + text + '*/'
} else {
return all + i[1]
}
}, '')
if (scss !== node.raws[prop].raw) {
node.raws[prop].scss = scss
}
}
}
}

module.exports = ScssParser
2 changes: 1 addition & 1 deletion lib/scss-tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ module.exports = function scssTokenize (input, options = {}) {

return {
back,
nextToken,
endOfFile,
nextToken,
position
}
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@
"postcss": "^8.4.19"
},
"devDependencies": {
"@logux/eslint-config": "^49.0.0",
"clean-publish": "^4.1.1",
"eslint": "^8.36.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"@logux/eslint-config": "^51.0.1",
"clean-publish": "^4.2.0",
"eslint": "^8.47.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-perfectionist": "^1.5.1",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-promise": "^6.1.1",
"postcss": "^8.4.21",
"postcss-parser-tests": "^8.5.2",
"postcss": "^8.4.27",
"postcss-parser-tests": "^8.6.0",
"uvu": "^0.5.6"
},
"prettier": {
Expand Down
Loading

0 comments on commit 5453320

Please sign in to comment.