<commit_before>/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.gradle.spotless;

import java.util.Collections;
import java.util.Map;

import org.gradle.api.Project;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.npm.TsFmtFormatterStep;

public class TypescriptExtension extends FormatExtension {

	static final String NAME = "typescript";

	public TypescriptExtension(SpotlessExtension root) {
		super(root);
	}

	public TypescriptFormatExtension tsfmt() {
		TypescriptFormatExtension tsfmt = new TypescriptFormatExtension();
		addStep(tsfmt.createStep());
		return tsfmt;
	}

	public class TypescriptFormatExtension extends NpmStepConfig<TypescriptFormatExtension> {

		protected Map<String, Object> tsFmtConfig = Collections.emptyMap();

		public TypescriptFormatExtension config(Map<String, Object> config) {
			this.tsFmtConfig = config;
			replaceStep(createStep());
			return this;
		}

		public FormatterStep createStep() {
			final Project project = getProject();
			return TsFmtFormatterStep.create(
					GradleProvisioner.fromProject(project),
					project.getBuildDir(),
					npmFileOrNull(),
					tsFmtConfig);
		}
	}

	@Override
	public PrettierConfig prettier() {
		PrettierConfig prettierConfig = new TypescriptPrettierConfig();
		addStep(prettierConfig.createStep());
		return prettierConfig;
	}

	/**
	 * Overrides the parser to be set to typescript, no matter what the user's config says.
	 */
	public class TypescriptPrettierConfig extends PrettierConfig {
		@Override
		FormatterStep createStep() {
			fixParserToTypescript();
			return super.createStep();<commit_msg>provide default basedir<commit_after>/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.gradle.spotless;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import org.gradle.api.Project;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.npm.TsFmtFormatterStep;

import static java.util.Objects.requireNonNull;

public class TypescriptExtension extends FormatExtension {

	static final String NAME = "typescript";

	public TypescriptExtension(SpotlessExtension root) {
		super(root);
	}

	public TypescriptFormatExtension tsfmt() {
		TypescriptFormatExtension tsfmt = new TypescriptFormatExtension();
		addStep(tsfmt.createStep());
		return tsfmt;
	}

	public class TypescriptFormatExtension extends NpmStepConfig<TypescriptFormatExtension> {

		protected Map<String, Object> tsFmtConfig = Collections.emptyMap();

		public TypescriptFormatExtension config(Map<String, Object> config) {
			this.tsFmtConfig = new TreeMap<>(requireNonNull(config));
			replaceStep(createStep());
			return this;
		}

		public FormatterStep createStep() {
			final Project project = getProject();
			Map<String, Object> config = new TreeMap<>(this.tsFmtConfig);
			if (!config.containsKey("basedir")) {
				config.put("basedir", project.getRootDir().getAbsolutePath()); //by default we use our project dir
			}
			return TsFmtFormatterStep.create(
					GradleProvisioner.fromProject(project),
					project.getBuildDir(),
					npmFileOrNull(),
					tsFmtConfig);
		}
	}

	@Override
	public PrettierConfig prettier() {
		PrettierConfig prettierConfig = new TypescriptPrettierConfig();
		addStep(prettierConfig.createStep());
		return prettierConfig;
	}

	/**
	 * Overrides the parser to be set to typescript, no matter what the user's config says.
	 */
	public class TypescriptPrettierConfig extends PrettierConfig {
		@Override
		FormatterStep createStep() {
			fixParserToTypescript();
			return super.createStep();
{'fraction_non_alphanumeric': 0.0880476393024245, 'fraction_numerical': 0.0034028073160357296, 'mean_word_length': 4.721411192214112, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3231926', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1300, 'n_words': 399}