Skip to content

Commit

Permalink
Merge pull request #32 from AdrianGonz97/chore/simplify-config
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored May 29, 2023
2 parents c4a1c19 + edb3089 commit 03dd876
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 178 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-trains-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": patch
---

Preserve the content of `svelte.config.js` when running the `init` command
118 changes: 118 additions & 0 deletions packages/cli/src/astravel/attachComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { defaultTraveler } from './defaultTraveler'

function attachCommentsToNode(
traveler,
state,
parent,
children,
findHeadingComments,
) {
let { index } = state
const { comments } = state
let comment = comments[index]
// Hack to tackle https://github.com/babel/minify/issues/866
let boundComments, trailingComments
if (comment == null) {
return
}
if (children == null || children.length === 0) {
// No children, attach comments to parent
boundComments = parent.comments != null ? parent.comments : []
while (comment != null && comment.end <= parent.end) {
boundComments.push(comment)
comment = comments[++index]
}
state.index = index
if (boundComments.length !== 0 && parent.comments == null) {
parent.comments = boundComments
}
return
}
// Look for heading block comments not immediately followed by a child
if (findHeadingComments) {
boundComments = parent.comments != null ? parent.comments : []
const { start } = children[0]
while (
comment != null &&
(comment.type[0] === 'B' || comment.type[0] === 'M') &&
comment.end <= start
) {
boundComments.push(comment)
comment = comments[++index]
}
if (boundComments.length !== 0 && parent.comments == null)
parent.comments = boundComments
}
// Attach comments to children
for (let i = 0, { length } = children; comment != null && i < length; i++) {
const child = children[i]
boundComments = []
while (comment != null && comment.end <= child.start) {
boundComments.push(comment)
comment = comments[++index]
}
// Check if next comment is line comment and on the same line if location is provided
if (
comment != null &&
comment.loc != null &&
(comment.type[0] === 'L' || comment.type[0] === 'S')
) {
if (comment.loc.start.line === child.loc.end.line) {
boundComments.push(comment)
comment = comments[++index]
}
}
if (boundComments.length !== 0) {
child.comments = boundComments
}
// Travel through child
state.index = index
traveler[child.type](child, state)
index = state.index
comment = comments[index]
}
// Look for remaining comments
trailingComments = []
while (comment != null && comment.end <= parent.end) {
trailingComments.push(comment)
comment = comments[++index]
}
if (trailingComments.length !== 0) {
parent.trailingComments = trailingComments
}
state.index = index
}

function Block(node, state) {
attachCommentsToNode(this, state, node, node.body, true)
}

let traveler = defaultTraveler.makeChild({
Program: Block,
BlockStatement: Block,
ClassBody: Block,
ObjectExpression(node, state) {
attachCommentsToNode(this, state, node, node.properties, true)
},
ArrayExpression(node, state) {
attachCommentsToNode(this, state, node, node.elements, true)
},
SwitchStatement(node, state) {
attachCommentsToNode(this, state, node, node.cases, false)
},
SwitchCase(node, state) {
attachCommentsToNode(this, state, node, node.consequent, false)
},
// TODO: Consider ArrayExpression ?
})

export function attachComments(node, comments) {
/*
Modifies in-place the AST starting at `node` by attaching the provided `comments` and returns that AST.
*/
traveler[node.type](node, {
comments,
index: 0,
})
return node
}
Loading

1 comment on commit 03dd876

@vercel
Copy link

@vercel vercel bot commented on 03dd876 May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.