TS-ify spaces.js and deindent.js, so they get included in prettier task

pull/618/head
Rich Harris 8 years ago
parent b0a1616e31
commit 4b1f1e8392

@ -1,5 +1,5 @@
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../../utils/CodeBuilder';
import deindent from '../../utils/deindent.js'; import deindent from '../../utils/deindent';
import { DomGenerator } from './index'; import { DomGenerator } from './index';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';

@ -3,7 +3,7 @@ import { parseExpressionAt } from 'acorn';
import annotateWithScopes from '../../utils/annotateWithScopes'; import annotateWithScopes from '../../utils/annotateWithScopes';
import isReference from '../../utils/isReference'; import isReference from '../../utils/isReference';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import deindent from '../../utils/deindent.js'; import deindent from '../../utils/deindent';
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../../utils/CodeBuilder';
import visit from './visit'; import visit from './visit';
import shared from './shared'; import shared from './shared';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import flattenReference from '../../../../utils/flattenReference'; import flattenReference from '../../../../utils/flattenReference';
import getSetter from '../shared/binding/getSetter'; import getSetter from '../shared/binding/getSetter';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import CodeBuilder from '../../../../utils/CodeBuilder'; import CodeBuilder from '../../../../utils/CodeBuilder';
import visit from '../../visit'; import visit from '../../visit';
import visitAttribute from './Attribute'; import visitAttribute from './Attribute';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';
import { Node } from '../../../../interfaces'; import { Node } from '../../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';
import { Node } from '../../../../interfaces'; import { Node } from '../../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent';
import visit from '../visit'; import visit from '../visit';
import { DomGenerator } from '../index'; import { DomGenerator } from '../index';
import Block from '../Block'; import Block from '../Block';

@ -1,5 +1,5 @@
import attributeLookup from './lookup'; import attributeLookup from './lookup';
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import getStaticAttributeValue from './getStaticAttributeValue'; import getStaticAttributeValue from './getStaticAttributeValue';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import flattenReference from '../../../../utils/flattenReference'; import flattenReference from '../../../../utils/flattenReference';
import getSetter from '../shared/binding/getSetter'; import getSetter from '../shared/binding/getSetter';
import getStaticAttributeValue from './getStaticAttributeValue'; import getStaticAttributeValue from './getStaticAttributeValue';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import visit from '../../visit'; import visit from '../../visit';
import visitComponent from '../Component/Component'; import visitComponent from '../Component/Component';
import visitWindow from './meta/Window'; import visitWindow from './meta/Window';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import flattenReference from '../../../../utils/flattenReference'; import flattenReference from '../../../../utils/flattenReference';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';
import { Node } from '../../../../interfaces'; import { Node } from '../../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../../utils/deindent.js'; import deindent from '../../../../utils/deindent';
import { DomGenerator } from '../../index'; import { DomGenerator } from '../../index';
import Block from '../../Block'; import Block from '../../Block';
import { Node } from '../../../../interfaces'; import { Node } from '../../../../interfaces';

@ -1,5 +1,5 @@
import flattenReference from '../../../../../utils/flattenReference'; import flattenReference from '../../../../../utils/flattenReference';
import deindent from '../../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent';
import { DomGenerator } from '../../../index'; import { DomGenerator } from '../../../index';
import Block from '../../../Block'; import Block from '../../../Block';
import { Node } from '../../../../../interfaces'; import { Node } from '../../../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent';
import visit from '../visit'; import visit from '../visit';
import { DomGenerator } from '../index'; import { DomGenerator } from '../index';
import Block from '../Block'; import Block from '../Block';

@ -1,4 +1,4 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent';
import { DomGenerator } from '../index'; import { DomGenerator } from '../index';
import Block from '../Block'; import Block from '../Block';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent';
import { DomGenerator } from '../index'; import { DomGenerator } from '../index';
import Block from '../Block'; import Block from '../Block';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../../../../utils/deindent.js'; import deindent from '../../../../../utils/deindent';
export default function getSetter({ export default function getSetter({
block, block,

@ -1,4 +1,4 @@
import deindent from '../../utils/deindent.js'; import deindent from '../../utils/deindent';
import flattenReference from '../../utils/flattenReference'; import flattenReference from '../../utils/flattenReference';
import { SsrGenerator } from './index'; import { SsrGenerator } from './index';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';

@ -1,4 +1,4 @@
import deindent from '../../utils/deindent.js'; import deindent from '../../utils/deindent';
import Generator from '../Generator'; import Generator from '../Generator';
import Block from './Block'; import Block from './Block';
import visit from './visit'; import visit from './visit';

@ -1,4 +1,4 @@
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent';
import getGlobals, { Globals } from './getGlobals'; import getGlobals, { Globals } from './getGlobals';
export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval'; export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval';

@ -1,5 +1,5 @@
import { parseExpressionAt } from 'acorn'; import { parseExpressionAt } from 'acorn';
import spaces from '../../utils/spaces.js'; import spaces from '../../utils/spaces';
import { Parser } from '../index'; import { Parser } from '../index';
function readExpression(parser: Parser, start: number, quoteMark) { function readExpression(parser: Parser, start: number, quoteMark) {

@ -1,5 +1,5 @@
import { parse } from 'acorn'; import { parse } from 'acorn';
import spaces from '../../utils/spaces.js'; import spaces from '../../utils/spaces';
import { Parser } from '../index'; import { Parser } from '../index';
const scriptClosingTag = '</script>'; const scriptClosingTag = '</script>';

@ -1,5 +1,5 @@
import * as assert from 'assert'; import * as assert from 'assert';
import deindent from './deindent.js'; import deindent from './deindent';
import CodeBuilder from './CodeBuilder'; import CodeBuilder from './CodeBuilder';
describe('deindent', () => { describe('deindent', () => {

@ -1,6 +1,6 @@
const start = /\n(\t+)/; const start = /\n(\t+)/;
export default function deindent(strings, ...values) { export default function deindent(strings: string[], ...values: any[]) {
const indentation = start.exec(strings[0])[1]; const indentation = start.exec(strings[0])[1];
const pattern = new RegExp(`^${indentation}`, 'gm'); const pattern = new RegExp(`^${indentation}`, 'gm');
@ -34,7 +34,7 @@ export default function deindent(strings, ...values) {
return result.trim().replace(/\t+$/gm, ''); return result.trim().replace(/\t+$/gm, '');
} }
function getTrailingIndentation(str) { function getTrailingIndentation(str: string) {
let i = str.length; let i = str.length;
while (str[i - 1] === ' ' || str[i - 1] === '\t') i -= 1; while (str[i - 1] === ' ' || str[i - 1] === '\t') i -= 1;
return str.slice(i, str.length); return str.slice(i, str.length);

@ -1,4 +1,4 @@
import spaces from './spaces.js'; import spaces from './spaces';
function tabsToSpaces(str: string) { function tabsToSpaces(str: string) {
return str.replace(/^\t+/, match => match.split('\t').join(' ')); return str.replace(/^\t+/, match => match.split('\t').join(' '));

@ -1,4 +1,4 @@
export default function spaces(i) { export default function spaces(i: number) {
let result = ''; let result = '';
while (i--) result += ' '; while (i--) result += ' ';
return result; return result;

@ -1,6 +1,5 @@
import deindent from "../../src/utils/deindent.js";
import assert from "assert"; import assert from "assert";
import { svelte } from "../helpers.js"; import { svelte, deindent } from "../helpers.js";
describe("create", () => { describe("create", () => {
it("should return a component constructor", () => { it("should return a component constructor", () => {

@ -1,6 +1,5 @@
import deindent from "../../src/utils/deindent.js";
import assert from "assert"; import assert from "assert";
import { svelte, env, setupHtmlEqual } from "../helpers.js"; import { svelte, deindent, env, setupHtmlEqual } from "../helpers.js";
function testAmd(code, expectedId, dependencies, html) { function testAmd(code, expectedId, dependencies, html) {
const fn = new Function("define", code); const fn = new Function("define", code);

@ -158,3 +158,50 @@ export function addLineNumbers(code) {
}) })
.join("\n"); .join("\n");
} }
const start = /\n(\t+)/;
export function deindent(strings, ...values) {
const indentation = start.exec(strings[0])[1];
const pattern = new RegExp(`^${indentation}`, 'gm');
let result = strings[0].replace(start, '').replace(pattern, '');
let trailingIndentation = getTrailingIndentation(result);
for (let i = 1; i < strings.length; i += 1) {
let expression = values[i - 1];
const string = strings[i].replace(pattern, '');
if (Array.isArray(expression)) {
expression = expression.length ? expression.join('\n') : null;
}
if (expression || expression === '') {
const value = String(expression).replace(
/\n/g,
`\n${trailingIndentation}`
);
result += value + string;
} else {
let c = result.length;
while (/\s/.test(result[c - 1])) c -= 1;
result = result.slice(0, c) + string;
}
trailingIndentation = getTrailingIndentation(result);
}
return result.trim().replace(/\t+$/gm, '');
}
function getTrailingIndentation(str) {
let i = str.length;
while (str[i - 1] === ' ' || str[i - 1] === '\t') i -= 1;
return str.slice(i, str.length);
}
export function spaces(i) {
let result = '';
while (i--) result += ' ';
return result;
}

@ -1,4 +1,3 @@
import spaces from "../../src/utils/spaces.js";
import assert from "assert"; import assert from "assert";
import * as path from "path"; import * as path from "path";
import * as fs from "fs"; import * as fs from "fs";
@ -11,7 +10,8 @@ import {
loadConfig, loadConfig,
loadSvelte, loadSvelte,
env, env,
setupHtmlEqual setupHtmlEqual,
spaces
} from "../helpers.js"; } from "../helpers.js";
let svelte; let svelte;

Loading…
Cancel
Save