-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from bholloway/feature/engine-next
postcss multi-value engine
- Loading branch information
Showing
13 changed files
with
945 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* MIT License http://opensource.org/licenses/MIT | ||
* Author: Ben Holloway @bholloway | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Given a sourcemap position create a new maybeObject with only line and column properties. | ||
* | ||
* @param {*|{line: number, column: number}} maybeObj Possible location hash | ||
* @returns {{line: number, column: number}} Location hash with possible NaN values | ||
*/ | ||
function sanitise(maybeObj) { | ||
var obj = !!maybeObj && typeof maybeObj === 'object' && maybeObj || {}; | ||
return { | ||
line: isNaN(obj.line) ? NaN : obj.line, | ||
column: isNaN(obj.column) ? NaN : obj.column | ||
}; | ||
} | ||
|
||
exports.sanitise = sanitise; | ||
|
||
/** | ||
* Infer a line and position delta based on the linebreaks in the given string. | ||
* | ||
* @param candidate {string} A string with possible linebreaks | ||
* @returns {{line: number, column: number}} A position object where line and column are deltas | ||
*/ | ||
function strToOffset(candidate) { | ||
var split = candidate.split(/\r\n|\n/g); | ||
var last = split[split.length - 1]; | ||
return { | ||
line: split.length - 1, | ||
column: last.length | ||
}; | ||
} | ||
|
||
exports.strToOffset = strToOffset; | ||
|
||
/** | ||
* Add together a list of position elements. | ||
* | ||
* Lines are added. If the new line is zero the column is added otherwise it is overwritten. | ||
* | ||
* @param {{line: number, column: number}[]} list One or more sourcemap position elements to add | ||
* @returns {{line: number, column: number}} Resultant position element | ||
*/ | ||
function add(list) { | ||
return list | ||
.slice(1) | ||
.reduce( | ||
function (accumulator, element) { | ||
return { | ||
line: accumulator.line + element.line, | ||
column: element.line > 0 ? element.column : accumulator.column + element.column, | ||
}; | ||
}, | ||
list[0] | ||
); | ||
} | ||
|
||
exports.add = add; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* MIT License http://opensource.org/licenses/MIT | ||
* Author: Ben Holloway @bholloway | ||
*/ | ||
'use strict'; | ||
|
||
const {basename} = require('path'); | ||
const tape = require('blue-tape'); | ||
|
||
const {sanitise, strToOffset, add} = require('./position-algerbra'); | ||
|
||
const json = (strings, ...substitutions) => | ||
String.raw( | ||
strings, | ||
...substitutions.map(v => JSON.stringify(v, (_, vv) => Number.isNaN(vv) ? 'NaN' : vv)) | ||
); | ||
|
||
tape( | ||
basename(require.resolve('./position-algerbra')), | ||
({name, test, end: end1, equal}) => { | ||
test(`${name} / sanitise()`, ({ end: end2 }) => { | ||
[ | ||
[undefined, {line: NaN, column: NaN}], | ||
[null, {line: NaN, column: NaN}], | ||
[false, {line: NaN, column: NaN}], | ||
[true, {line: NaN, column: NaN}], | ||
[12, {line: NaN, column: NaN}], | ||
[{}, {line: NaN, column: NaN}], | ||
[{line: 1}, {line: 1, column: NaN}], | ||
[{column: 1}, {line: NaN, column: 1}], | ||
].forEach(([input, expected]) => | ||
equal( | ||
json`${sanitise(input)}`, | ||
json`${expected}`, | ||
json`input ${input} should sanitise to ${expected}` | ||
) | ||
); | ||
|
||
end2(); | ||
}); | ||
|
||
test(`${name} / strToOffset()`, ({ end: end2 }) => { | ||
[ | ||
['', {line: 0, column: 0}], | ||
['something', {line: 0, column: 9}], | ||
['another\nthing', {line: 1, column: 5}], | ||
['a\r\nthird\nthingie', {line: 2, column: 7}], | ||
['a\r\nthird\rthingie', {line: 1, column: 13}], | ||
].forEach(([input, expected]) => | ||
equal( | ||
json`${strToOffset(input)}`, | ||
json`${expected}`, | ||
json`input ${input} should convert to ${expected}` | ||
) | ||
); | ||
|
||
end2(); | ||
}); | ||
|
||
test(`${name} / add()`, ({ end: end2 }) => { | ||
[ | ||
[[{line: 0, column: 2}, {line: 0, column: 3}], {line: 0, column: 5}], | ||
[[{line: 0, column: 2}, {line: 1, column: 3}], {line: 1, column: 3}], | ||
[[{line: 1, column: 2}, {line: 0, column: 3}], {line: 1, column: 5}], | ||
[[{line: 1, column: 2}, {line: 2, column: 3}], {line: 3, column: 3}], | ||
[[{line: NaN, column: NaN}, {line: 0, column: 3}], {line: NaN, column: NaN}], | ||
[[{line: NaN, column: NaN}, {line: 2, column: 3}], {line: NaN, column: 3}] | ||
].forEach(([input, expected]) => | ||
equal( | ||
json`${add(input)}`, | ||
json`${expected}`, | ||
json`input ${input} should add to give ${expected}` | ||
) | ||
); | ||
|
||
end2(); | ||
}); | ||
|
||
end1(); | ||
} | ||
); |
Oops, something went wrong.