Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Apr 29, 2024
1 parent 6e8474a commit b840d05
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
5 changes: 4 additions & 1 deletion packages/tailwindcss/src/ast.ts
Expand Up @@ -118,11 +118,14 @@ function span(value: string, location: Location) {

let start = { line: line + 1, column: column + 1 }

for (let i = 0; i < value.length; ++i) {
// Skip the first character as it's already accounted for by the current value
// of `location`
for (let i = 1; i < value.length; ++i) {
if (value.charCodeAt(i) === 0x0a) {
// Add the number of lines the comment spans
line += 1
column = 0
i += 1 // Skip the first character of the next line
} else {
// Keep track of the column for accurate end locations
column += 1
Expand Down
23 changes: 5 additions & 18 deletions packages/tailwindcss/src/css-parser.ts
Expand Up @@ -115,8 +115,6 @@ export function parse(input: string, track?: boolean) {
else if (currentChar === SLASH && input.charCodeAt(i + 1) === ASTERISK) {
let start = i

// TODO: Track source ranges

for (let j = i + 2; j < input.length; j++) {
peekChar = input.charCodeAt(j)

Expand All @@ -129,13 +127,6 @@ export function parse(input: string, track?: boolean) {
else if (peekChar === LINE_BREAK) {
line += 1
lineStart = j + 1

if (buffer.length === 0) {
sourceStartLine = line
sourceStartColumn = 0
sourceEndLine = line
sourceEndColumn = 0
}
}

// End of the comment
Expand All @@ -153,10 +144,13 @@ export function parse(input: string, track?: boolean) {
let node = comment(commentString.slice(2, -2))
licenseComments.push(node)

for (let i = 0; i < commentString.length; ++i) {
// Skip the first character of the comment as it's already included in
// the source range.
for (let i = 1; i < commentString.length; ++i) {
if (commentString.charCodeAt(i) === LINE_BREAK) {
sourceEndLine += 1
sourceEndColumn = 0
i += 1 // Skip the first character of the next line
} else {
sourceEndColumn += 1
}
Expand Down Expand Up @@ -284,13 +278,6 @@ export function parse(input: string, track?: boolean) {
else if (peekChar === LINE_BREAK) {
line += 1
lineStart = j + 1

if (buffer.length === 0) {
sourceStartLine = line
sourceStartColumn = 0
sourceEndLine = line
sourceEndColumn = 0
}
}

// End of the comment
Expand Down Expand Up @@ -469,7 +456,7 @@ export function parse(input: string, track?: boolean) {

// Track the source location for source maps
sourceEndLine = line
sourceEndColumn = i - lineStart
sourceEndColumn = i - lineStart - 1
node.source = sourceRange()!

// Reset the state for the next node.
Expand Down
32 changes: 15 additions & 17 deletions packages/tailwindcss/src/source-maps.test.ts
Expand Up @@ -56,11 +56,11 @@ test('source locations are tracked during parsing and serializing', async () =>
decl: ast[0].nodes[0].destination[0],
})

expect(src.selector).toEqual('1:1-1:6')
expect(dst.selector).toEqual('1:1-1:6')
expect(src.selector).toEqual('1:1-1:5')
expect(dst.selector).toEqual('1:1-1:5')

expect(src.decl).toEqual('1:8-1:18')
expect(dst.decl).toEqual('2:3-2:14')
expect(dst.decl).toEqual('2:3-2:13')
})

test('utilities have source maps pointing to the utilities node', async () => {
Expand All @@ -76,8 +76,8 @@ test('utilities have source maps pointing to the utilities node', async () => {

expect(annotations).toEqual([
//
'1:1-12 <- 1:1-20',
'2:3-35 <- 1:1-20',
'1:1-11 <- 1:1-20',
'2:3-34 <- 1:1-20',
])
})

Expand All @@ -94,15 +94,14 @@ test('@apply generates source maps', async () => {
expect(sources).toEqual(['source.css'])
expect(sources.length).toBe(1)

// TODO: Some numbers are off by one (too large?) in the destination
expect(annotations).toEqual([
'1:1-6 <- 1:1-6',
'2:3-15 <- 2:3-14',
'3:3-15 <- 3:3-39',
'4:3-11 <- 3:3-39',
'5:5-17 <- 3:3-39',
'7:3-35 <- 4:3-19',
'8:3-14 <- 5:3-13',
'1:1-5 <- 1:1-5',
'2:3-14 <- 2:3-14',
'3:3-14 <- 3:3-39',
'4:3-10 <- 3:3-39',
'5:5-16 <- 3:3-39',
'7:3-34 <- 4:3-19',
'8:3-13 <- 5:3-13',
])
})

Expand All @@ -114,19 +113,18 @@ test('license comments preserve source locations', async () => {
expect(sources).toEqual(['source.css'])
expect(sources.length).toBe(1)

expect(annotations).toEqual(['1:1-20 <- 1:1-20'])
expect(annotations).toEqual(['1:1-19 <- 1:1-19'])
})

test('license comments with new lines preserve source locations', async () => {
let { sources, annotations } = run(`/*! some \n comment */`)
let { sources, annotations, css } = run(`/*! some \n comment */`)

// All CSS generated by Tailwind CSS should be annotated with source maps
// And always be able to point to the original source file
expect(sources).toEqual(['source.css'])
expect(sources.length).toBe(1)

// Ideally we'd write this as this instead `1:0-2:11 <- 1:0-2:11`
expect(annotations).toEqual(['1:1 <- 2:1', '2:12 <- 3:12'])
expect(annotations).toEqual(['1:1 <- 1:1', '2:11 <- 2:11'])
})

/**
Expand Down

0 comments on commit b840d05

Please sign in to comment.