|
| 1 | +import {test} from 'uvu' |
| 2 | +import * as assert from 'uvu/assert' |
| 3 | +import {toValidIdentifierName} from '../lib/util/to-valid-identifier-name.js' |
| 4 | + |
| 5 | +/** @param {string} invalidChar */ |
| 6 | +function toTestFailureMessage(invalidChar) { |
| 7 | + return `Invalid characters like "${invalidChar}" should be converted to underscores "_"` |
| 8 | +} |
| 9 | + |
| 10 | +test('toValidIdentifierName', () => { |
| 11 | + // Valid strings left untouched |
| 12 | + assert.equal(toValidIdentifierName('test'), 'test') |
| 13 | + assert.equal(toValidIdentifierName('camelCase'), 'camelCase') |
| 14 | + // Invalid cont character -> underscore |
| 15 | + assert.equal( |
| 16 | + toValidIdentifierName('custom-element'), |
| 17 | + 'custom_element', |
| 18 | + toTestFailureMessage('-') |
| 19 | + ) |
| 20 | + assert.equal( |
| 21 | + toValidIdentifierName('custom element'), |
| 22 | + 'custom_element', |
| 23 | + toTestFailureMessage(' ') |
| 24 | + ) |
| 25 | + // Invalid starting character -> underscore |
| 26 | + assert.equal( |
| 27 | + toValidIdentifierName('-badStarting'), |
| 28 | + '_badStarting', |
| 29 | + toTestFailureMessage('-') |
| 30 | + ) |
| 31 | + assert.equal( |
| 32 | + toValidIdentifierName('1badStarting'), |
| 33 | + '_badStarting', |
| 34 | + toTestFailureMessage('1') |
| 35 | + ) |
| 36 | + assert.equal( |
| 37 | + toValidIdentifierName(' badStarting'), |
| 38 | + '_badStarting', |
| 39 | + toTestFailureMessage(' ') |
| 40 | + ) |
| 41 | + // Empty string -> underscore |
| 42 | + assert.equal('_', toValidIdentifierName('')) |
| 43 | +}) |
| 44 | + |
| 45 | +test.run() |
0 commit comments