move get_name to get_name_from_filename and add unit tests

pull/3030/head
Richard Harris 6 years ago
parent 829b12e7fc
commit d36478657c

@ -24,7 +24,7 @@
"types": "types/runtime", "types": "types/runtime",
"scripts": { "scripts": {
"test": "mocha --opts mocha.opts", "test": "mocha --opts mocha.opts",
"test:unit": "mocha --require sucrase/register --recursive ./**/__test__.ts", "test:unit": "mocha --require sucrase/register --recursive src/**/__test__.ts",
"quicktest": "mocha --opts mocha.opts", "quicktest": "mocha --opts mocha.opts",
"precoverage": "c8 mocha --opts mocha.coverage.opts", "precoverage": "c8 mocha --opts mocha.coverage.opts",
"coverage": "c8 report --reporter=text-lcov > coverage.lcov && c8 report --reporter=html", "coverage": "c8 report --reporter=text-lcov > coverage.lcov && c8 report --reporter=html",

@ -6,6 +6,7 @@ import render_ssr from './render-ssr/index';
import { CompileOptions, Warning } from '../interfaces'; import { CompileOptions, Warning } from '../interfaces';
import Component from './Component'; import Component from './Component';
import fuzzymatch from '../utils/fuzzymatch'; import fuzzymatch from '../utils/fuzzymatch';
import { get_name_from_filename } from './utils/get_name_from_filename';
const valid_options = [ const valid_options = [
'format', 'format',
@ -55,33 +56,6 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
} }
} }
function get_name(filename: string) {
if (!filename) return null;
// eslint-disable-next-line no-useless-escape
const parts = filename.split(/[\/\\]/);
if (parts.length > 1) {
const index_match = parts[parts.length - 1].match(/^index(\.\w+)/);
if (index_match) {
parts.pop();
parts[parts.length - 1] += index_match[1];
}
}
const base = parts.pop()
.replace(/\.[^.]+$/, "")
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
if (!base) {
throw new Error(`Could not derive component name from file ${filename}`);
}
return base[0].toUpperCase() + base.slice(1);
}
export default function compile(source: string, options: CompileOptions = {}) { export default function compile(source: string, options: CompileOptions = {}) {
options = assign({ generate: 'dom', dev: false }, options); options = assign({ generate: 'dom', dev: false }, options);
@ -98,7 +72,7 @@ export default function compile(source: string, options: CompileOptions = {}) {
const component = new Component( const component = new Component(
ast, ast,
source, source,
options.name || get_name(options.filename) || 'Component', options.name || get_name_from_filename(options.filename) || 'Component',
options, options,
stats, stats,
warnings warnings

@ -1,6 +1,7 @@
import * as assert from 'assert'; import * as assert from 'assert';
import deindent from './deindent'; import deindent from './deindent';
import CodeBuilder from './CodeBuilder'; import CodeBuilder from './CodeBuilder';
import get_name_from_filename from './get_name_from_filename';
describe('deindent', () => { describe('deindent', () => {
it('deindents a simple string', () => { it('deindents a simple string', () => {
@ -164,3 +165,17 @@ describe('CodeBuilder', () => {
); );
}); });
}); });
describe('get_name_from_filename', () => {
it('uses the basename', () => {
assert.equal(get_name_from_filename('path/to/Widget.svelte'), 'Widget');
});
it('uses the directory name, if basename is index', () => {
assert.equal(get_name_from_filename('path/to/Widget/index.svelte'), 'Widget');
});
it('handles unusual filenames', () => {
assert.equal(get_name_from_filename('path/to/[...parts].svelte'), 'Parts');
});
});

@ -0,0 +1,26 @@
export default function get_name_from_filename(filename: string) {
if (!filename) return null;
// eslint-disable-next-line no-useless-escape
const parts = filename.split(/[\/\\]/);
if (parts.length > 1) {
const index_match = parts[parts.length - 1].match(/^index(\.\w+)/);
if (index_match) {
parts.pop();
parts[parts.length - 1] += index_match[1];
}
}
const base = parts.pop()
.replace(/\.[^.]+$/, "")
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
if (!base) {
throw new Error(`Could not derive component name from file ${filename}`);
}
return base[0].toUpperCase() + base.slice(1);
}
Loading…
Cancel
Save